Notes

Setting up WordPress, MySQL, Nginx proxy and SSL certificates in Docker

Edit on GitHub

Docker
2 minutes
  • building and running the docker setup

    • mysql
    • wordpress
    • nginx proxy
    • auto-renewing SSLs
  • we’ll have a data folder where we’ll keep the files/data we need to mount inside docker containers as volumes

  • wordpress and database will be our directories for the WordPress site and the MySQL database files. These will be in the user’s home directory (and not in the data folder)

  • we’ll have a .env file for the secrets. .env in the project’s root is picked up by Docker automatically. You can reference environment variables inside docker-compose.yml like you reference bash variables: ${MYSQL_PASSWORD}

You can use environment variables in configuration values with a Bash-like ${VARIABLE} syntax - see variable substitution for full details.

.
├── data
│   ├── certbot
│   │   ├── conf
│   │   └── www
│   └── nginx
│       └── nginx.conf
└── docker-compose.yml
# .env

# MySQL Credentials
MYSQL_DATABASE=blah
MYSQL_USER=blah
MYSQL_PASSWORD=blah
MYSQL_ROOT_PASSWORD=blah

# WordPress Credentials
WORDPRESS_DB_HOST=mysql # this is the name of our MySQL service container
WORDPRESS_DB_NAME=blah
WORDPRESS_DB_USER=blah
WORDPRESS_DB_PASSWORD=blah
 1# WordPress
 2wordpress:
 3  container_name: wordpress
 4  image: wordpress:php7.4-apache
 5  restart: always
 6  stdin_open: true
 7  tty: true
 8  environment:
 9    WORDPRESS_DB_HOST: ${WORDPRESS_DB_HOST}
10    WORDPRESS_DB_USER: ${WORDPRESS_DB_USER}
11    WORDPRESS_DB_PASSWORD: ${WORDPRESS_DB_PASSWORD}
12    WORDPRESS_DB_NAME: ${WORDPRESS_DB_NAME}
13  volumes:
14    - /home/${USER}/wordpress:/var/www/html
 1# MySQL
 2mysql:
 3  container_name: mysql # this will be the name used in logs when starting/stopping instead of ` wp-devops_mysql_1`
 4  image: mysql:5.7
 5  restart: unless-stopped
 6  # if you use mysql version 8 you need PHP to handle passwords correctly
 7  # command: '--default-authentication-plugin=mysql_native_password'
 8  environment:
 9    # getting these values from `.env`
10    MYSQL_DATABASE: ${MYSQL_DATABASE}
11    MYSQL_USER: ${MYSQL_USER}
12    MYSQL_PASSWORD: ${MYSQL_PASSWORD}
13    MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
14  ports:
15    - 3306:3306
16  volumes:
17    - /home/${USER}/database:/var/lib/mysql
 1# Nginx Proxy
 2nginx:
 3  container_name: nginx
 4  image: nginx:latest
 5  restart: unless-stopped
 6  ports:
 7    - 80:80
 8    - 443:443
 9  volumes:
10    - ./nginx/conf:/etc/nginx/conf.d
11    - ./certbot/conf:/etc/nginx/ssl
12    - ./certbot/data:/var/www/html
1# SSL - Certbot or ZeroSSL
2certbot:
3  container_name: certbot
4  image: certbot/certbot:latest
5  command: certonly --webroot --webroot-path=/var/www/html --email youremail@domain.com --agree-tos --no-eff-email -d domain.com -d www.domain.com
6  volumes:
7    - ./certbot/conf:/etc/letsencrypt
8    - ./certbot/logs:/var/log/letsencrypt
9    - ./certbot/data:/var/www/html

Nginx

The following three lines specify which SSL cert file to use.

ssl                  on;
ssl_certificate      /etc/ssl/certificate.crt;
ssl_certificate_key  /etc/ssl/private.key;
1docker-compose up
2
3docker ps -a