Skip to main content

Wordpress - Docker Compose Stack with Nginx Reverse Proxy

282 words·
Wordpress Docker-Compose Nginx MariaDB
Table of Contents

Wordpress Docker Compose
#

Folder Structure
#

# Create folder structure
sudo mkdir -p /opt/wordpress/wp_data && cd /opt/wordpress

# Change permissions
sudo chown -R 33:33 /opt/wordpress/wp_data

Environment File
#

# Create environment file
sudo vi .env

# .env
MYSQL_ROOT_PASSWORD=my-root-pw # Define root PW
MYSQL_DATABASE=wordpress
MYSQL_USER=wordpress
MYSQL_PASSWORD=my-sql-pw # Define SQL PW

WORDPRESS_DB_HOST=db
WORDPRESS_DB_USER=wordpress
WORDPRESS_DB_PASSWORD=my-sql-pw # Define SQL PW
WORDPRESS_DB_NAME=wordpress

Docker Compose File
#

# Create Docker Compose file
sudo vi docker-compose.yml
# docker-compose.yml
version: "2.4"
services:
  db:
    image: mariadb:10.6.4-focal
    container_name: wordpress-db
    command: '--default-authentication-plugin=mysql_native_password'
    volumes:
      - ./db_data:/var/lib/mysql
    restart: unless-stopped
    env_file:
      - .env
    expose:
      - 3306
      - 33060

  wordpress:
    image: wordpress:latest
    container_name: wordpress-app
    volumes:
      - ./wp_data:/var/www/html
    ports:
      - 80:80
    restart: unless-stopped
    env_file:
      - .env

Start Docker Container
#

# Create / start Docker container
sudo docker compose up -d

Reverse Proxy
#

Certbot
#

# Install Certbot
sudo apt install certbot -y

# Create certificate
sudo certbot certonly --standalone -d wordpress.jklug.work

Nginx
#

# Install nginx
sudo apt install nginx -y

# Copy default config
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/wordpress.jklug.work
# Edit config
sudo vi /etc/nginx/sites-available/wordpress.jklug.work

# wordpress.jklug.work
server {
    listen 443 ssl;
    server_name wordpress.jklug.work;

    ssl_certificate         /etc/letsencrypt/live/wordpress.jklug.work/fullchain.pem;
    ssl_certificate_key     /etc/letsencrypt/live/wordpress.jklug.work/privkey.pem;


    location / {
        proxy_pass http://127.0.0.1:80;
        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 X-Forwarded-Host $server_name;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_redirect off;
    }
}
# Disable default config
sudo rm /etc/nginx/sites-enabled/default

# Enable config
sudo ln -s /etc/nginx/sites-available/wordpress.jklug.work /etc/nginx/sites-enabled/

# Restart Nginx
sudo systemctl restart nginx

Wordpress Website & Login
#

  • Finish initial setup
# Worpress Initial Setup
wordpress.jklug.work
  • After initial setup
# Wordpress user login
wordpress.jklug.work/wp-login.php

# Wordpress Website
wordpress.jklug.work

Wordpress Settings
#

Go to the settings section of the wordpress instance and make sure that WordPress Address (URL) and Site Address (URL) point to https://wordpress.jklug.work.