Migration WordPress to Docker Container

Migrasi WordPress ke Docker Container

  • WordPres yang akan dimigrasikan berada di shared hosting yang menggunakan cPanel atau bisa juga di server konvensional
  • Domain: sys-ops.bikinin.website – (103.169.7.137)
  • Server Docker Container menggunakan Ubuntu 22.04 – (103.169.7.138)
  • Server Docker menggunakan Apache reverse proxy
  • Tampilan WordPress sebelum di migrasi ke docker

Server Ubuntu 22.04

  • Install apache2 pada ubuntu 22.04
root@ubuntu-2204:~# apt install apache2 -y

Backup WordPress

  • Catat konfigurasi wordpress pada file wp-config.php
  • Copy pada bagian berikut:
// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'sysops_wordpress' );

/** Database username */
define( 'DB_USER', 'sysops_wordpress' );

/** Database password */
define( 'DB_PASSWORD', 'sysops_wordpress' );

/** Database hostname */
define( 'DB_HOST', 'localhost' );

/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8mb4' );

/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
  • Export file website lewat file manager
  • Select All semua file > Compress > pilih extensi file tar.gz
  • Hasil file akan terletak pada /public_html/sys-ops-id.tar.gz.
  • Export Database lewat phpmyadmin
  • Upload file database lewat file manager

Setting folder website

  • Buat directory
root@ubuntu-2204:~# mkdir -pv /var/www/sys-ops.bikinin.website
root@ubuntu-2204:~# mkdir -pv /var/www/sys-ops.bikinin.website/src
root@ubuntu-2204:~# mkdir -pv /var/www/sys-ops.bikinin.website/database
root@ubuntu-2204:~# mkdir -pv /var/www/sys-ops.bikinin.website/database/data
root@ubuntu-2204:~# mkdir -pv /var/www/sys-ops.bikinin.website/database/initdb.d
  • Copy/download file wordpress dari cpanel lalu extract ke folder src
root@ubuntu-2204:~# cd /var/www/sys-ops.bikinin.website/
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# wget https://sys-ops.bikinin.website/sys-ops-id.tar.gz --no-check-certificate
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# tar -xvzf sys-ops-id.tar.gz -C src/
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# chown -R www-data:www-data /var/www/sys-ops.bikinin.website/src
  • Copy/Download file .sql dari cpanel lalu simpan ke folder initdb.d
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# wget https://sys-ops.bikinin.website/sysops_wordpress.sql --no-check-certificate
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# cp sysops_wordpress.sql database/initdb.d/

Docker Compose

  • Buat file docker-compose.yml
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# nano docker-compose.yml
version: '1.4'
services:
  sysops_db:
    image: mysql:8.0
    container_name: sysops_db
    volumes:
      - ./database/data:/var/lib/mysql
      - ./database/initdb.d:/docker-entrypoint-initdb.d
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: P@ssw0rd!_root   # password root database
      MYSQL_DATABASE: sysops_wordpress      # nama database dari wp-config.php
      MYSQL_USER: sysops_wordpress          # nama user database dari wp-config.php
      MYSQL_PASSWORD: sysops_wordpress      # password user database dari wp-config.php

  sysops_wp:
    depends_on:
      - sysops_db
    image: wordpress:latest                 # menggunakan wordpress versi terbaru
    container_name: sysops_wp
    ports:
      - "8080:80"
    restart: always
    links:
      - sysops_db:mysql
    volumes:
      - ./src:/var/www/html
  • Jalankan docker-compose
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# docker-compose up -d

[+] Running 35/35 Pull complete                                      54.2s
 ⠿ sysops_wp Pulled                                                  108.8s
   ⠿ fb4d3fb05351 Pull complete                                      90.1s
   ⠿ 23d3af42839e Pull complete                                      94.9s
   ⠿ a5b33728e4a6 Pull complete                                      95.4s
   ⠿ 766e2b674cd0 Pull complete                                      96.0s
 ⠿ sysops_db Pulled                                                  92.9s
   ⠿ 9d10771b98b8 Pull complete                                      67.5s
   ⠿ 3d8ef442614b Pull complete                                      69.0s
   ⠿ 7dc17a6cea26 Pull complete                                      69.9s
   ⠿ 752752efdaea Pull complete                                      77.4s
[+] Running 3/3
 ⠿ Network sys-opsbikininwebsite_default  Created                    1.0s
 ⠿ Container sysops_db                    Started                    15.9s
 ⠿ Container sysops_wp                    Started                    15.0s
  • Cek status container
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# docker ps -a
CONTAINER ID   IMAGE              COMMAND                  CREATED         STATUS         PORTS                                   NAMES
e8ef34cf2409   wordpress:latest   "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   sysops_wp
547ee65c465d   mysql:8.0          "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   3306/tcp, 33060/tcp                     sysops_db

Setting Apache Reverse Proxy

  • Buat virtual host untuk domain sys-ops.bikinin.website
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# nano /etc/apache2/sites-available/sys-ops.bikinin.website.conf
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName sys-ops.bikinin.website

        Header set Access-Control-Allow-Headers "ORIGIN, X-REQUESTED-WITH, CONTENT-TYPE"
        Header set Access-Control-Allow-Methods "POST, GET, OPTIONS, PUT, DELETE"
        Header set Access-Control-Allow-Origin "*"
        Header set Access-Control-Allow-Credentials true
        Header set X-XSS-Protection 1;mode=block
        Header set X-Frame-Options SAMEORIGIN
        Header set X-Content-Type-Options nosniff
        Header set Strict-Transport-Security "max-age=15552000; includeSubDomains;preload"
        Header set Referrer-Policy strict-origin-when-cross-origin
        Header set Access-Control-Max-Age 60000

        ProxyPreserveHost On
        ProxyRequests Off
        ProxyVia On
        ProxyPass / http://127.0.0.1:8080/
        ProxyPassReverse / http://127.0.0.1:8080/

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  • Enable mod rewrite, headers, http2, proxy, proxy_http lalu restart service apache2
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# a2enmod rewrite
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# a2enmod headers
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# a2enmod http2
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# a2enmod proxy
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# a2enmod proxy_http
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# a2ensite sys-ops.bikinin.website.conf
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# systemctl restart apache2

Setting WordPress

  • Edit file wp-config.php
  • Ubah define( ‘DB_HOST’, ‘localhost’ ); menjadi define( ‘DB_HOST’, ‘sys-ops_db’ );
  • sesuai dengan nama container mysql pada file docker-compose.yml
  • lalu tambahakan konfigurasi untuk https
root@ubuntu-2204:~# nano /var/www/sys-ops.bikinin.website/src/wp-config.php
define( 'DB_HOST', 'sys-ops_db' );

$_SERVER['HTTPS'] = 'on';

define( 'WP_HOME', 'http://sys-ops.bikinin.website/' );
define( 'WP_SITEURL', 'http://sys-ops.bikinin.website/' );
  • Buat file .htaccess
root@ubuntu-2204:/var/www/sys-ops.bikinin.website# nano src/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

  • Update dns record, ganti IP address untuk domain sys-ops.bikinin.website dari 103.169.7.137 menjadi 103.169.7.138 (server docker)
C:\Users\Exa-Ops>ping sys-ops.bikinin.website

Pinging sys-ops.bikinin.website [103.169.7.138] with 32 bytes of data:
Reply from 103.169.7.138: bytes=32 time=461ms TTL=55
Reply from 103.169.7.138: bytes=32 time=42ms TTL=55
Reply from 103.169.7.138: bytes=32 time=32ms TTL=55
Reply from 103.169.7.138: bytes=32 time=32ms TTL=55

Pengujian

  • Akses halaman depan website
  • Akses post detail
  • Akses halaman dashboard
  • Posting artikel

Optional – Install Free SSL Let’s Encrypt

  • Install let’s encrypt untuk domain sys-ops.bikinin.website
root@ubuntu-2204:~# apt install certbot python3-certbot-apache -y
root@ubuntu-2204:~# certbot --apache --agree-tos --redirect -m [email protected] -d sys-ops.bikinin.website

herdiana3389

A system administrator with skills in system administration, virtualization, linux, windows, networking, cloud computing, container, etc.