Install WordPress with Docker Nginx Reverse Proxy to Apache with SSL

Prerequisites

  1. Install Docker on Ubuntu 22.04
  2. Install Docker Compose on Ubuntu 22.04.
  • Domain pointed to your server IP address.
  • Docker installed and configured.
  • Docker Compose installed and configured.

Step 1: Create a project directory

mkdir wp-project

Step 2: Create Docker Compose YML File

cd wp-project
nano docker-compose.yml
version: "3.9" services: wordpress: container_name: wordpress image: wordpress:php8.1-apache restart: always stdin_open: true tty: true environment: WORDPRESS_DB_HOST: mariadb WORDPRESS_DB_USER: db_user WORDPRESS_DB_PASSWORD: db_user_pass WORDPRESS_DB_NAME: db_name volumes: - wordpress_data:/var/www/html - ./wordpress:/var/www/html mariadb: container_name: mariadb image: mariadb restart: always environment: MYSQL_DATABASE: db_name MYSQL_USER: db_user MYSQL_PASSWORD: db_user_pass MYSQL_RANDOM_ROOT_PASSWORD: 'root_pass' volumes: - db_data:/var/lib/mysql nginx: container_name: nginx image: nginx:latest restart: unless-stopped ports: - 80:80 - 443:443 volumes: - ./nginx/conf:/etc/nginx/conf.d - ./certbot/conf:/etc/nginx/ssl - ./certbot/data:/var/www/html certbot: container_name: certbot image: certbot/certbot:latest command: certonly --webroot --webroot-path=/var/www/html --email[email protected] --agree-tos --no-eff-email -d domain.com -d www.domain.com volumes: - ./certbot/conf:/etc/letsencrypt - ./certbot/logs:/var/log/letsencrypt - ./certbot/data:/var/www/html volumes: db_data: wordpress_data:
  • version: Compose file version which is compatible with the Docker Engine. You can check compatibility here.
  • services: here we have 3 services named wordpress, nginx and certbot.
  • image: We use latest WordPress with PHP 8.1, Apache, Nginx and Certbot images available in Docker hub.
  • volumes:
  • wordpress: we have configured this directory to be synced with the directory we wish to use as the web root inside the container.
  • conf: here we will place the Nginx configuration file to be synced with the default Nginx conf.d folder inside the container.
  • cedtbot/conf: this is where we will receive the SSL certificate and this will be synced with the folder we wish to inside the container.
  • ports: configure the container to listen upon the listed ports.
  • command: the command used to receive the SSL certificate.
  • environment: here we list all the environment variables that are available for the WordPress image.
  • WORDPRESS_DB_HOST: Here we are using the service name of MariaDB container.
  • WORDPRESS_DB_USER: Same as the one we have configured in mariadb service.
  • WORDPRESS_DB_PASSWORD: Same as the one we have configured in mariadb service.
  • WORDPRESS_DB_NAME: Same as the one we have configured in mariadb service.

Step 3: Configure Nginx

mkdir -p nginx/conf
nano nginx/conf/default.conf
server { listen [::]:80; listen 80; server_name domain.com www.domain.com; root /var/www/html; index index.php; location ~ /.well-known/acme-challenge { allow all; root /var/www/html; } location / { try_files $uri @apache; } location ~ ^/.user.ini { deny all; } location ~* .(svg|svgz)$ { types {} default_type image/svg+xml; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } location @apache { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $host; proxy_pass http://wordpress:80; } location ~[^?]*/$ { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $host; proxy_pass http://wordpress:80; } location ~ .php$ { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $host; proxy_pass http://wordpress:80; } location ~/. { deny all; access_log off; log_not_found off; } }

Step 4: Deploy WordPress with Docker Compose

docker-compose up -d
docker-compose ps

Step 5: Configure Let’s Encrypt SSL with Nginx

nano nginx/conf/default.confserver { listen [::]:80; listen 80; server_name domain.com www.domain; return 301 https://www.domain.com$request_uri; } server { listen [::]:443 ssl http2; listen 443 ssl http2; server_name domain.com; ssl_certificate /etc/nginx/ssl/live/domain.com/fullchain.pem; ssl_certificate_key /etc/nginx/ssl/live/domain.com/privkey.pem; return 301 https://www.domain.com$request_uri; } server { listen [::]:443 ssl http2; listen 443 ssl http2; server_name www.domain.com; ssl_certificate /etc/nginx/ssl/live/domain.com/fullchain.pem; ssl_certificate_key /etc/nginx/ssl/live/domain.com/privkey.pem; root /var/www/html; index index.php; location ~ /.well-known/acme-challenge { allow all; root /var/www/html; } location / { try_files $uri @apache; } location ~ ^/.user.ini { deny all; } location ~* .(svg|svgz)$ { types {} default_type image/svg+xml; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } location @apache { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $host; proxy_pass http://wordpress:80; } location ~[^?]*/$ { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $host; proxy_pass http://wordpress:80; } location ~ .php$ { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $host; proxy_pass http://wordpress:80; } location ~/. { deny all; access_log off; log_not_found off; } }
docker-compose restart nginx

Conclusion

--

--

Installation and Set Up of Web Applications on Google Cloud

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CloudBooklet

CloudBooklet

Installation and Set Up of Web Applications on Google Cloud