Install WordPress and MariaDB with Reverse Proxy Nginx on Docker Container

Install WordPress dan MariaDB dengan Reverse Proxy Nginx pada Docker Container

Artikel sebelumnya:


  • Buat persistant volume docker untuk menyimpan data wordpress dan mariadb
docker volume create wp81
docker volume create db81
  • Cek volume docker
docker volume ls

DRIVER    VOLUME NAME
local     db81
local     wp81
  • Download image wordpress:latest dan mariadb:10.5
docker pull wordpress:latest
docker pull mariadb:10.5
  • Cek docker image
docker images

REPOSITORY               TAG       IMAGE ID       CREATED        SIZE
wordpress                latest    f05a1ca36b1f   11 hours ago   622MB
mariadb                  10.5      4db72891d344   3 weeks ago    408MB
  • Buat container wordpress dengan image: wordpress:latest
  • nama: wordpress81
  • hostname: wordpresshost
  • port: 81:80
  • max memory: 256M
  • volume: wp81:/var/www/html
  • dijalankan di background
sudo docker run -d -p 81:80 -m 256M --name wordpress81 \
-h wordpresshost --restart always -v wp81:/var/www/html wordpress:latest
  • Buat container mariadb dengan image: mariadb:10.5
  • nama: mariadb81
  • hostname: mariadbhost
  • port: 33061:3306
  • max memory: 256M
  • volume: db81:/var/lib/mysql
  • dijalankan di background
  • environment variabel: root password: root_P@ssw0rd! , database: wordpress81_db , userDB: wordpress81_user , passwordDB: wordpress81_pass
sudo docker run -d -p 33061:3306 -m 256M --name mariadb81 \
-e MYSQL_ROOT_PASSWORD=root_P@ssw0rd! \
-e MYSQL_DATABASE=wordpress81_db \
-e MYSQL_USER=wordpress81_user \
-e MYSQL_PASSWORD=wordpress81_pass \
-h mariadbhost --restart always -v db81:/var/lib/mysql mariadb:10.5
  • Cek container docker
docker ps -a

CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS                                               NAMES
86123196cc2b   mariadb:10.5             "docker-entrypoint.s…"   10 seconds ago   Up 7 seconds    0.0.0.0:33061->3306/tcp, :::33061->3306/tcp         mariadb81
73c784f21b2b   wordpress:latest         "docker-entrypoint.s…"   49 seconds ago   Up 46 seconds   0.0.0.0:81->80/tcp, :::81->80/tcp                   wordpress81
  • Cek ip address container wordpress (172.17.0.5) dan mariadb (172.17.0.6)
docker exec wordpress81 cat /etc/hosts

127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.5      wordpresshost
-------------------------------------
docker exec mariadb81 cat /etc/hosts

127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.6      mariadbhost
  • Buat server block (virtual host) pada nginx dan aktifkan reverse proxy ke container wordpress
  • listen port: 80
  • nama domain: wp-docker.bikinin.website
  • proxy_pass http://localhost:81
server {
   listen 80;
   server_name wp-docker.bikinin.website;

   location ^~ / {
       index index.html index.htm index.php;
       try_files $uri $uri/ /index.php?$args;

       proxy_pass http://localhost:81;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header REMOTE-HOST $remote_addr;

       add_header X-Frame-Options "SAMEORIGIN";
       add_header X-Content-Type-Options "nosniff";
       add_header X-Xss-Protection "1; mode=block";
       add_header X-Cache $upstream_cache_status;

       set $static_fileYsIv9c7Y 0;
       if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
       {
           set $static_fileYsIv9c7Y 1;
           expires 12h;
       }
       if ( $static_fileYsIv9c7Y = 0 )
       {
           add_header Cache-Control no-cache;
       }
   }
}
  • Restart service nginx yang ada pada host
systemctl restart nginx

  • Akses wordpress dengan url: http://wp-docker.bikinin.website
  • masukan nama database sesuai dengan yang sudah di buat sebelumnya saat menjalankan docker mariadb
  • masukan database host: 172.17.0.6:3306 (ip address mariadb) atau bisa menggunakan nama host container (mariadb81:3306)
  • WordPress berhasil di install


  • Aktifkan ssl let’s encrypt untuk domain wp-docker.bikinin.website
certbot --nginx -d wp-docker.bikinin.website -m [email protected]
  • Edit file wp-config.php pada directory: /var/lib/docker/volumes/wp81/_data/
nano /var/lib/docker/volumes/wp81/_data/wp-config.php
  • Tambahkan bari berikut
$_SERVER['HTTPS'] = 'on';
  • Tampilan konfigurasi server block sesudah di aktifkan ssl let’s encrypt: wp-docker.bikinin.website.conf
server {
   server_name wp-docker.bikinin.website;

   location ^~ / {
       index index.html index.htm index.php;
       try_files $uri $uri/ /index.php?$args;

       proxy_pass http://localhost:81;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header REMOTE-HOST $remote_addr;

       add_header X-Frame-Options "SAMEORIGIN";
       add_header X-Content-Type-Options "nosniff";
       add_header X-Xss-Protection "1; mode=block";
       add_header X-Cache $upstream_cache_status;

       set $static_fileYsIv9c7Y 0;
       if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
       {
           set $static_fileYsIv9c7Y 1;
           expires 12h;
       }
       if ( $static_fileYsIv9c7Y = 0 )
       {
           add_header Cache-Control no-cache;
       }
   }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/wp-docker.bikinin.website/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/wp-docker.bikinin.website/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = wp-docker.bikinin.website) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

   listen 80;
   server_name wp-docker.bikinin.website;
    return 404; # managed by Certbot
}
  • Reload pada browser dengan url: https://wp-docker.bikinin.website

herdiana3389

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