WordPress + Docker Double Caching with Redis and Nginx Reverse Proxy

(UPDATED 2021/09 for latest Traefik release, Brie) While WordPress is great it’s slow and taxing on a webserver. However ultimately Redis’ object cache and the related plugin as well as Nginx’s reverse proxy caching make for an especially fast site. Combine Traefik for management and do it all on Docker for an excellent solution.

Step 1: Create a basic Traefik folder (for this example in your home directory ~/Traefik

Step 2: in the new Traefik directory, create a conf directory and create the acme.json.

mkdir ./conf
sudo touch ./conf/acme.json
sudo chmod 0600 ./conf/acme.json

Step 3: Load the docker-compose.yml into the top Traefik directory as follows:

---
version: '3.8'
services:
  traefik:
    container_name: traefik
    image: traefik:brie
    restart: unless-stopped
    ports:
      - 80:80
      - 443:443
      - 8080:8080
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
      - ./conf/traefik.toml:/etc/traefik/traefik.toml:ro
      - ./conf/traefik_dynamic.toml:/etc/traefik/traefik_dynamic.toml:ro
      - ./conf/acme.json:/acme.json
    labels:
      traefik.enable: true
      traefik.http.routers.api-http.rule: Host(`traefik.${HOSTNAME1}`)
      traefik.http.routers.api-http.entrypoints: web
      traefik.http.routers.api-http.middlewares: https-redirect@file
      traefik.http.routers.api-https.service: api@internal
      traefik.http.routers.api-https.rule: Host(`traefik.${HOSTNAME1}`)
      traefik.http.routers.api-https.entrypoints: websecure
      traefik.http.routers.api-https.middlewares: security@file, compression@file, https-redirect@file, auth
      traefik.http.routers.api-https.tls: true
      traefik.http.routers.api-https.tls.certresolver: letsencrypt
      traefik.http.middlewares.auth.basicauth.users: "username:httpasswd_double_$$"

  sqlwp:
    container_name: sqlwp
    image: mariadb:latest
    restart: unless-stopped
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
    labels:
      traefik.enable: false

  nginxwp-1:
    container_name: nginxforwp-${HOST1}
    image: nginx:stable-alpine
    restart: unless-stopped
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
      - ./wordpress-${HOST1}:/var/www/html
      - ${LOGS_LOCAL}:/var/log/nginx/
      - ./conf/nginx-${HOST1}:/etc/nginx/
      - nginxcache:/var/www/html/cache
    links:
      - wp-1
    labels:
      traefik.enable: true
      traefik.http.routers.nginxwp-${HOST1}-http.rule: Host(`${HOSTNAME1}`)||Host(`www.${HOSTNAME1}`)
      traefik.http.routers.nginxwp-${HOST1}-http.entrypoints: web
      traefik.http.routers.nginxwp-${HOST1}-http.middlewares: https-redirect@file
      traefik.http.routers.nginxwp-${HOST1}-https.rule: Host(`${HOSTNAME1}`)||Host(`www.${HOSTNAME1}`)
      traefik.http.routers.nginxwp-${HOST1}-https.entrypoints: websecure
      traefik.http.routers.nginxwp-${HOST1}-https.middlewares: security@file, compression@file, https-redirect@file
      traefik.http.routers.nginxwp-${HOST1}-https.tls: true
      traefik.http.routers.nginxwp-${HOST1}-https.tls.certresolver: letsencrypt

  wp-1:
    container_name: wpapp-${HOST1}
    image: wordpress:php7.4-fpm-alpine
    restart: unless-stopped
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
      - ./wordpress-${HOST1}:/var/www/html
      - ./conf/php.ini:/usr/local/etc/php/php.ini
      - nginxcache:/var/www/html/cache
    depends_on:
      - sqlwp
    environment:
      - WORDPRESS_DB_HOST=${WORDPRESS_DB_HOST}
      - WORDPRESS_DB_USER=${WORDPRESS_DB_USER}
      - WORDPRESS_DB_PASSWORD=${WORDPRESS_DB_PASSWORD}
      - WORDPRESS_DB_NAME=${WORDPRESS_DB_NAME}
      - WORDPRESS_TABLE_PREFIX=${HOST1}
    labels:
      traefik.enable: false

volumes:
  dataredis:
  nginxcache:

Step 4: Create a .env file in the main Traefik directory

MYSQL_ROOT_PASSWORD=make_a_good_password
MYSQL_USER=something_but_admin_or_root
MYSQL_PASSWORD=make_a_good_password
MYSQL_DATABASE=wpdb
WORDPRESS_DB_USER=something_but_admin_or_root
WORDPRESS_DB_PASSWORD=make_a_good_other_password
WORDPRESS_DB_HOST=sqlwp
WORDPRESS_DB_NAME=wpdb
HOST1=mydomain
HOSTNAME1=mydomain.com

Follow the rest of the directions here:

https://computerz.solutions/docker-compose-wordpress-nginx-et-traefik/

Leave a Reply

Your email address will not be published. Required fields are marked *