Skip to main content

Tutorial: Enhancing Dockerized WordPress with SSL Certificate Generation

In this tutorial, we'll walk through the process of enhancing a Dockerized WordPress setup with SSL certificate generation. This will ensure secure communication between servers and clients, bolstering the security of your WordPress deployment.

Prerequisites:

  1. Docker installed on your system.
  2. Basic understanding of Docker Compose.

Step 1: Update Docker Compose Configuration

Open your docker-compose.yml file and make the following modifications:

version: '3.1'
services:
  wordpress:
    image: wordpress:php7.2-apache
    ports:
      - "8001:80"  
    # Changed from "8000:80" to "8001:80"
    environment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: quangminh1708password
      WORDPRESS_DB_NAME: wordpress_db
    volumes:
      - ./website:/var/www/html
      - ./ssl:/etc/apache2/ssl  # Mount SSL directory
    depends_on:
      - mysql
    networks:
      - my_network

  mysql:
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: wordpress_db
      MYSQL_ROOT_PASSWORD: quangminh1708password
    ports:
      - "3306:3306"
    volumes:
      - ./data:/var/lib/mysql
    networks:
      - my_network

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - mysql
    ports:
      - "8081:80"
    environment:
      PMA_HOST: mysql
      MYSQL_ROOT_PASSWORD: quangminh1708password
    networks:
      - my_network

  nginx:
    image: nginx
    ports:
      - "443:443"
    volumes:
      - ./nginx-conf:/etc/nginx/conf.d  # Mount Nginx configuration directory
      - ./ssl:/etc/nginx/ssl  # Mount SSL directory
    networks:
      - my_network

networks:
  my_network:
    driver: bridge

Step 2: Create SSL Certificate Generation Script

Generate a bash script named generate_ssl.sh with the following content:

#!/bin/bash

SSL_DIR="./ssl"
PRIVATE_KEY="$SSL_DIR/private.key"
CERTIFICATE="$SSL_DIR/certificate.crt"

# Create SSL directory if it doesn't exist
mkdir -p $SSL_DIR

# Generate SSL certificate if not already present
if [ ! -f "$PRIVATE_KEY" ] || [ ! -f "$CERTIFICATE" ]; then
    openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout "$PRIVATE_KEY" -out "$CERTIFICATE"
fi

Step 3: Implement SSL Certificate Generation

Execute the following commands in your terminal:

chmod +x generate_ssl.sh
./generate_ssl.sh

Step 4: Start Docker Containers

Run the following command to initiate your Docker containers:

docker-compose up -d

Conclusion

Congratulations! You've successfully enhanced your Dockerized WordPress setup with SSL certificate generation. Your WordPress deployment now benefits from improved security through encrypted communication.