WordPress 網站快速搬家遷移至 WordOps

WordPress.ORG

如何將存放於各平台【舊伺服器】的 WordPress 網站,快速搬家遷移至 WordOps 的【新伺服器】。WordOps 能快速及簡化 WordPress 網站的安裝流程,並自動配置網站伺服器 Nginx 最佳化,帶有 fastcgi_cache 或 Redis 等快取的 WordPress 網站,且還能透過 WordOps 網頁後台即時監控伺服器效能。

DNS 修改設定

修改類型 A 的值,設定新網站伺服器主機的 IP 。

舊網站伺服器

轉存備份 WordPress 資料庫

如果您的伺服器未安裝 WP-CLI,請先執行以下指令來取得它:

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

查看 WP-CLI 是否安裝成功:

wp --info
OS:     Linux 4.18.0-147.el8.x86_64 #1 SMP Wed Dec 4 21:51:45 UTC 2019 x86_64
Shell:  /bin/bash
PHP binary:     /usr/bin/php
PHP version:    7.4.5
php.ini used:   /etc/php.ini
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /home/jacky
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.4.0

進入 WordPress 網站根目錄:

cd /var/www/www.footmark.com.tw/

使用 WP-CLI 轉儲備份 WordPress 資料庫:

wp db export --allow-root
Success: Exported to 'www_footmark_com_tw-2021-02-28-c4124fd.sql'.

新網站伺服器

請先安裝 WordOps,請參考 WordOps 快速建立高效能 WordPress 和管理教學 - MIS 腳印

建立與舊網站相同網址的 WordPress 網站,並帶有 Nginx 快取 fastcgi_cache:

wo site create footmark.com.tw --wpfc
Running pre-update checks       [OK]
Setting up NGINX configuration  [Done]
Setting up webroot              [Done]
Downloading WordPress           [Done]
Setting up database             [Done]
Configuring WordPress           [OK]
Installing WordPress            [OK]
Installing plugin nginx-helper  [OK]
Setting plugin nginx-helper     [OK]
Testing Nginx configuration     [OK]
Reloading Nginx                 [OK]
WordPress admin user : fooxxxrk
WordPress admin password : xxxxmh0fsQxxxxv7dPSFxxxx
Nginx-Helper configuration :    http://footmark.com.tw/wp-admin/options-general.php?page=nginx
Successfully created site http://footmark.com.tw

刪除預設產生的 WordPress 檔案與資料庫:

sudo -u www-data -H wp db clean --yes --path=/var/www/footmark.com.tw/htdocs
Success: Tables dropped.

刪除預設產生的 WordPress 檔案:

sudo rm -rf /var/www/footmark.com.tw/htdocs/*

Let's Encrypt (更新 HTTPS 憑證)

將現有 WordPress 網站,使用 DNS API 方式更新為 HTTPS:

wo site update footmark.com.tw --wp --letsencrypt=wildcard --dns=dns_gd

設置 SSH 無密碼的訪問

在新伺服器建立 SSH 密鑰 (只需一直 Enter 即可):

ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/jacky/.ssh/id_ed25519):
Created directory '/home/jacky/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/jacky/.ssh/id_ed25519
Your public key has been saved in /home/jacky/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:xxxxxxxxxxAwuF1UC6XxxxxxXid9Dk5ikrxxxxxxxxxx jacky@www.footmark.com.tw
The key's randomart image is:
+--[ED25519 256]--+
|            o+*oB|
|            ++.Oo|
|         ..+.o*o=|
|         o*=+ooBO|
|        Soo=+.o=O|
|            =o .o|
|           . o   |
|            . +  |
|           E . . |
+----[SHA256]-----+

將上述產生的 SSH 公鑰增加到舊伺服器:

ssh-copy-id root@61.216.180.58
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jacky/.ssh/id_ed25519.pub"
The authenticity of host '61.216.xx.xx (61.216.xx.xx)' can't be established.
ECDSA key fingerprint is SHA256:8+eKCXlAGMP3xREXB/wiNJ+yyZF+RdLj31nycPtsMWk.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@61.216.xx.xx's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@61.216.xx.xx'"
and check to make sure that only the key(s) you wanted were added.

查看舊伺服器 .ssh 目錄就會看到 authorized_keys 檔案:

ls -l .ssh/
總計 8
-rw-------. 1 root root 107  2月 28 18:34 authorized_keys
-rw-r--r--. 1 root root 175  2月 21 19:01 known_hosts

在新伺服器即可 SSH 無密碼登入舊主機:

 ssh root@61.216.xx.xx
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Sun Feb 28 18:23:08 2021

將舊主機 WordPress 傳輸到新主機

將舊主機 WordPress 根目錄所有資料,傳輸到新主機的 WordPress 目錄 /var/www/site/htdocs/

sudo rsync -avzh --progress --ignore-existing root@61.216.xx.xx:/var/www/www.footmark.com.tw/ /var/www/footmark.com.tw/htdocs/
... 以上省略 ...

sent 121.37K bytes  received 148.85M bytes  4.73M bytes/sec
total size is 225.86M  speedup is 1.52
jacky@www:~$ sudo rsync -avzh --progress --ignore-existing root@61.216.xx.xx:/var/www/www.footmark.com.tw/ /var/www/footmark.com.tw/htdocs/

如果舊有 WordPress 根目錄內有 wp-config.php 檔案須更名,否則會衝突:

sudo mv /var/www/footmark.com.tw/htdocs/wp-config.php /var/www/footmark.com.tw/htdocs/wp_config.php.bak

還原 WordPress 資料庫

切換到網站根目錄:

cd /var/www/footmark.com.tw/htdocs/

還原在舊伺服器轉存備份的 WordPress 資料庫:

wp db import www_footmark_com_tw-2021-02-28-c4124fd.sql
Success: Imported from 'www_footmark_com_tw-2021-02-28-c4124fd.sql'.

參考

發表留言