搬一個 Live WordPress 網站落 Docker

其實 WordPress 網站要搬係有 plugins 幫你手,隻 plugin 仲好厲害添,可以喺搬嘅時候幫你改埋啲內容,例如你個 Domain 要改名,佢可以將你個網站現有嘅 Domain links 改為新嘅 Domain name,勁方便。

既然咁方便,有咩值得講?

因為我上一篇 post 講隻 WordPress Docker,整好咗隻 Docker 咪試下搬隻 website 入伙,上演真人 show。😎

目標網站,請留意,有 HTTPS 嘅,係用 cPanel 生成嘅。 個 HTTPS 係用 WordPress plugin “Really Simple SSL” 做嘅,首先去 disable 佢,搬完先更正番。
搞掂,Safari 顯示為 Not Secure。 安裝隻 All-in-One WP Migration plugin。
安裝完,激活,之後就多咗一個 menu item,入面有 3 個功能,我哋首先用 Export,即係將隻 website 嘅資料揪出嚟。 Export 提供咗 3 款 options 選擇。
第 1 個係 Find XXX Replace with YYY,即係將資料入面有 XXX 都順手改做 YYY。今次唔使用到,直接跳過。 第 2 個係可以抄啲唔抄啲,我係搬家,所有嘢都照搬,用唔著。
第 3 個係重點,可以選 export 出嚟嘅檔案你想放去邊?我淨係想自己嚟,冇需要太復雜,於是揀 FILE。 隻 plugin 即刻做嘢。
第一個 error,隻 website storage 唔夠空間,要去 cPanel 增加儲存空間,即係俾錢要買位。 準備完畢,全部資料要 535MB,唔好彩,超出咗免費版嘅上限 512MB 少少,如果你冇咩錢,可以跟喱個網頁試下嘅。買唔買慢慢諗,先下載隻檔案。
整隻 Docker,copy 一份 script,叫做 wp_directoutput,然後修改隻 .env 檔案,如上。 再用 WinSCP 傳上隻 Docker VM 度。

登入隻 Docker 然後照辨 create 啲 containers,唔好漏咗搞 permissions:

docker exec wp_directoutput chown -R www-data:www-data wp-content

我就係漏咗。😂

去 Portainer check 下,多咗 3 隻 containers。
去 Google Cloud 我隻私家 nginx Reverse Proxy 加個 conf 俾佢用。 改好咗記得 restart 隻 nginx,佢會即時 load 隻新整嘅 conf 檔案,冇 error 即係安全通過。
準備好,可以去 cPanel 改個 Domain IP 指去個新 website 嘅 IP,一但改咗就可以暫時收工,因為要等啲 domain name servers 由舊 IP 變為新 IP,要有耐性,等。 當等夠時間之後,你第一次 visit 個網站佢會要你 create 個 admin user,喱個係 WordPress 嘅例行要求,記住,after 你 Restore 咗個 backup,個 admin user 會變番係舊嗰個。
立即去加番隻 All-in-One WP Migration plugin,然後㨂 Import。 Drag 個 backup 檔案落去,上傳,然後等佢分析一下,佢或者會出現以上嘅 error message,不過唔緊要,可以按 PROCEED。
個 plugin 識得自動波 Restore。 最後會 retore 個 database。
./certbot-auto certonly --standalone -d directoutput.com.hk

 

完成之後要去 Settings > Permalinks 按一次 Save,原因個 plugin 作者冇講,淨係要你做一次,咁咪做囉。不過,要留意 Settings > General 兩個位:WordPress Address (URL) 同埋 Site Address (URL),依家未有 SSL,做完以下嘅步驟就要返嚟修正為 HTTPS。

 

個網站係已經用得,不過由於之前係有用 SSL 嘅,我哋去隻 nginx Reverse Proxy 度加番個 certificate。有時要先行 stop 個 nginx 先 run 到上面嘅指令。
*** 完 ***
加完 certificate 就要改埋個 website 嘅 conf 檔案,如上。  

送個 conf 檔案:

## Handle http:// connections
server  {
        listen          80;
        server_name     domain.com.hk www.domainname.com.hk;
        return 301 https://$server_name$request_uri;
}

## Handle https:// connections
server  {
        listen          443 ssl;
        server_name     www.marche.com.hk;

        #access_log     /var/log/nginx/www.domainname.access.log;
        #error_log      /var/log/nginx/www.domainname.error.log debug;

    ##
    # SSL Settings
    ##

    ssl            on;

    # Set cert locations
    ssl_certificate    /etc/letsencrypt/live/domainname.com.hk/fullchain.pem;
    ssl_certificate_key    /etc/letsencrypt/live/domainname.com.hk/privkey.pem;

    ssl_session_cache    shared:SSL:10m;
    ssl_session_timeout    10m;
    ssl_protocols        SSLv3 TLSv1 TLSv1.1 TLSv1.2;

    ssl_ciphers            RC4:HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers    on;

    keepalive_timeout    60;

    location / {


        ##
        # Proxy settings
        ##

        proxy_pass                      http://192.168.12.149:8005;

        # Convert all inbound request into http:// request to upstream server
        proxy_redirect          http:// $scheme://;

        proxy_set_header    Accept-Encoding "";
        proxy_set_header        Host                            $host;
        proxy_set_header    X-Real-IP                   $remote_addr;
        proxy_set_header    X-Forwarded-For             $remote_addr;
        proxy_set_header    X-Forwarded-Proto    $scheme;


        add_header        Front-End-Https    on;

        proxy_max_temp_file_size    0;
        proxy_connect_timeout        90;
        proxy_send_timeout        90;
        proxy_read_timeout        90;
        proxy_buffer_size        4k;
        proxy_buffers            4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size    64k;

        client_max_body_size        512m;
        client_body_buffer_size    512k;

        ##
        # Filter response settings
        ##

        # Filter all response content with http://www.marche.com.hk into https://www.marchet.com.hk
        sub_filter      http://www.marche.com.hk/ https://www.marche.com.hk/;

        # Filter not only response type text/html but also text/css and text/xml
        sub_filter_types    text/css text/xml;

        # Filter all response
        sub_filter_once    off;
    }
}