Self-Hosting WordPress, Pt. 2: Adding Traefik and Custom Domain

Welcome to Part 2 of our self-hosting WordPress series! In Part 1, we set up a WordPress site using Docker and Docker Compose. Now, we’ll take things further by setting up a reverse proxy with Traefik, enabling HTTPS and a custom domain for your site. For this, we assume you already have a custom domain that you’d like to use for your WordPress blog.

What is Traefik, and Why Do You Need It?

Traefik is a modern reverse proxy that simplifies managing web traffic to your applications. It’s like a middleman that sits between the internet and your server, routing incoming requests to our WordPress. It’s offers similar functionality to nginx but as someone who prefers configuration with yaml files, I personally prefer using Traefik. With Traefik, you can:

  • Automatically obtain and renew SSL certificates from Let’s Encrypt.
  • Manage multiple applications (e.g., WordPress, Nextcloud) on the same server with different domains.
  • Simplify networking by handling all incoming traffic on a single entry point.

By adding Traefik to your setup, your WordPress site will be accessible securely over HTTPS, improving security and user trust.

Setting Up Traefik

Step 1: Creating our Traefik Configuration

Set up a Traefik directory in root:

mkdir -p ~/traefik && cd ~/traefik

Next, let’s create a traefik.yml file: This file will configure Traefik’s behavior:

entryPoints:
  web:
    address: ":80"
  websecure:
    address: ":443"

providers:
  docker:
    exposedByDefault: false

certificatesResolvers:
  letsencrypt:
    acme:
      email: <your-email@example.com>
      storage: acme.json
      httpChallenge:
        entryPoint: web

Prepare the SSL certificate storage:

touch acme.json && chmod 600 acme.json

Step 2: Set Up Traefik with Docker Compose

Let’s integrate our Traefik with the docker-compose.yml we had from Part 1:

services:
  traefik:
    image: traefik:v2.5
    command:
      - "--configFile=/traefik.yml"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - "./traefik.yml:/traefik.yml"
      - "./acme.json:/acme.json"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    networks:
      - web

  wordpress:
    image: wordpress
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpresspass
      WORDPRESS_DB_NAME: wordpress
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.wordpress.rule=Host(`<yourdomain.com>`)"
      - "traefik.http.routers.wordpress.entrypoints=websecure"
      - "traefik.http.routers.wordpress.tls.certresolver=letsencrypt"
    networks:
      - web
      - default

  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: strongpassword
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpresspass
    networks:
      - default

networks:
  web:
    external: true

Don’t forget to change to your domain in the labels section, and start Traefik by running docker-compose up -d

Step 3: Test Your Setup

Navigate to https://<yourdomain>.com in your browser. You should see your WordPress site served over HTTPS! And that concludes this installment of self-hosting WordPress. Thanks for reading!

Comments

Leave a Reply

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