Docker WordPress Environment with HTTPS in 5 Steps

If you’re a developer working with WordPress, setting up a local environment that mirrors a production setup can save you a lot of time and frustration. Testing locally means you can make tweaks and adjustments without risking your live site or dealing with the sluggishness of a remote server. But, to really replicate a production environment, it’s crucial to use HTTPS rather than HTTP, as this is the standard for modern web development and essential for security and SEO.

Thank me by sharing on Twitter 🙏

In this guide, I’ll walk you through setting up WordPress with HTTPS using Docker and Docker Compose. Not only will we configure WordPress to run over HTTPS on localhost, but we’ll also ensure we’re using SSL with a self-signed certificate. By the end, you’ll have a solid, local WordPress setup that’s both secure and reliable, ready for serious development and testing.

Prerequisites

Before diving in, it’s essential to have Docker and Docker Compose installed. If you haven’t set them up already, you can find the latest installation instructions on Docker’s official website. Once you’re set up, you’ll be ready to create your Docker configuration for WordPress with HTTPS.

Step 1: Create a Dockerfile for WordPress with SSL Support

To start, we need a Dockerfile that will serve as the foundation of our WordPress container. By default, the official WordPress Docker image only supports HTTP. So, our Dockerfile will add SSL support by creating a self-signed certificate and configuring Apache to use HTTPS.

Here’s the Dockerfile configuration:

Dockerfile
# Use the official WordPress image
FROM wordpress:latest

# Install OpenSSL to create self-signed certificates
RUN apt-get update && apt-get install -y openssl

# Enable the SSL and rewrite Apache modules
RUN a2enmod ssl rewrite

# Create SSL directory in Apache
RUN mkdir -p /etc/apache2/ssl

# Generate a self-signed SSL certificate (valid for 365 days)
RUN openssl req -x509 -nodes -days 365 \
  -newkey rsa:2048 \
  -keyout /etc/apache2/ssl/apache.key \
  -out /etc/apache2/ssl/apache.crt \
  -subj "/C=US/ST=Local/L=Local/O=Local/OU=Dev/CN=localhost"

# Copy a custom Apache virtual host configuration to support HTTPS
COPY ./apache-ssl.conf /etc/apache2/sites-available/000-default.conf

# Expose both HTTP and HTTPS ports
EXPOSE 80 443

This Dockerfile begins by using the latest official WordPress image. Then, we install OpenSSL and enable the necessary Apache modules for SSL and URL rewriting. The script also creates an SSL directory and generates a self-signed certificate with a 365-day expiration. Finally, we copy a custom Apache configuration file (apache-ssl.conf) into the container, which we’ll create next.

Step 2: Configure Apache for HTTPS with a Self-Signed Certificate

In this step, we create an apache-ssl.conf file to set up Apache for HTTPS and direct any HTTP requests to HTTPS automatically. This file should be saved in the same directory as your Dockerfile.

Here’s what the apache-ssl.conf file should look like:

Apache
<VirtualHost *:80>
    ServerName localhost
    Redirect permanent / https://localhost/
</VirtualHost>

<VirtualHost *:443>
    ServerName localhost
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        AllowOverride All
        Require all granted
    </Directory>

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/apache.crt
    SSLCertificateKeyFile /etc/apache2/ssl/apache.key

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

This configuration creates two virtual hosts in Apache. The first one listens on port 80 and redirects any requests to HTTPS on port 443. The second virtual host listens on port 443, serves the WordPress files, and enables SSL with the certificate we generated. This configuration ensures all requests are secure, even if someone accidentally accesses the HTTP version.

Step 3: Set Up docker-compose.yml for WordPress and MySQL

To manage both WordPress and MySQL as containers, we’ll use Docker Compose. Docker Compose simplifies container management by allowing us to define and run multiple containers as services within a single YAML file. Here’s the docker-compose.yml file:

YAML
services:
  wordpress:
    build: .
    container_name: wordpress
    ports:
      - "8080:80"
      - "8443:443"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: user
      WORDPRESS_DB_PASSWORD: password
      WORDPRESS_DB_NAME: wordpress
    depends_on:
      - db
    volumes:
      - ./wp-data:/var/www/html

  db:
    image: mysql:5.7
    container_name: wordpress_db
    restart: always
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: rootpassword
    volumes:
      - ./db-data:/var/lib/mysql

volumes:
  wp-data:
  db-data:

In this configuration:

  • The wordpress service builds from our custom Dockerfile and runs on ports 8080 (HTTP) and 8443 (HTTPS).
  • The db service is a MySQL container configured with basic environment variables, including the database name, user, and password.
  • Both services use volumes for persistence so that database and WordPress files are saved between container restarts.

Step 4: Build and Run the Docker Containers

With all our configurations in place, we’re ready to build and start our containers.

First, build the WordPress Docker image with:

ShellScript
docker-compose build

Once the build is complete, start the containers:

ShellScript
docker-compose up

Docker Compose will start both the WordPress and MySQL containers. Once they’re running, you can access your WordPress site in your browser by navigating to https://localhost:8443. Because this setup uses a self-signed certificate, you’ll likely encounter a security warning in your browser. Simply accept the warning to proceed to your local site.

Step 5: Configure WordPress to Use HTTPS

Even though we’ve set up WordPress to run over HTTPS, we still need to update its settings to use HTTPS URLs for all resources and links. Here’s how to do it:

  1. Open your browser and go to https://localhost:8443/wp-admin to log in to the WordPress dashboard.
  2. Navigate to Settings > General.
  3. Change both the WordPress Address (URL) and Site Address (URL) fields to https://localhost:8443.
  4. Save the changes.

This step ensures WordPress will load assets over HTTPS and prevents mixed-content warnings.

Wrapping Up

Setting up WordPress locally with HTTPS using Docker and Docker Compose gives you a close approximation of a live environment, complete with SSL. This setup provides a secure environment where you can test plugins, themes, and other changes without risking your production site. It’s a powerful, flexible solution that saves time and prepares you for a smoother transition from development to deployment.

With this configuration, you now have a fully functioning WordPress environment over HTTPS on localhost. This setup not only mirrors a production server more closely but also allows you to explore WordPress development confidently in a secure local setup.

Share this:

Leave a Reply