Deploying WordPress using Docker

Deploying WordPress using Docker

Deploying WordPress with Nginx using Docker involves setting up multiple containers that work together. This typically includes a WordPress container (often using PHP-FPM), a database container (like MySQL or MariaDB), and an Nginx container to act as a reverse proxy.

Here’s a general outline of the process:

  • Project Setup: Create a project directory to hold your Docker Compose file and Nginx configuration.
  • Docker Compose File: Create a docker-compose.yml file to define the services (containers) for WordPress, the database, and Nginx.
version: '3.8'
services:
  wordpress:
    image: wordpress:fpm-alpine # Or another suitable WordPress FPM image
    restart: always
    user: 1000:1003 # the id of user and group of skanto
    depends_on:
      - db
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpressuser
      WORDPRESS_DB_PASSWORD: wordpresspassword
      WORDPRESS_DB_NAME: wordpressdb
    volumes:
      - ./wordpress_data:/var/www/html

  db:
    image: mysql:8.0 # Or mariadb
    restart: always
    user: 1000:1003 # the id of user and group of skanto
    environment:
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
      MYSQL_DATABASE: wordpressdb
      MYSQL_USER: wordpressuser
      MYSQL_PASSWORD: wordpresspassword
    volumes:
      - ./db_data:/var/lib/mysql

  nginx:
    image: nginx:stable-alpine
    depends_on:
      - wordpress
    ports:
      - "80:80"
      - "443:443" # For SSL, if needed
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./wordpress_data:/var/www/html:ro # Read-only access to WordPress files
networks:
  default:
    driver: bridge
  • WordPress Configuration: Navigate to ./wordpress_data and open wp-config-docker.php file for editing. Adjust database connection information like below
// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', getenv_docker('WORDPRESS_DB_NAME', 'wordpressdb') );

/** Database username */
define( 'DB_USER', getenv_docker('WORDPRESS_DB_USER', 'wordpressuser') );

/** Database password */
define( 'DB_PASSWORD', getenv_docker('WORDPRESS_DB_PASSWORD', 'wordpresspassword') );
  • Nginx Configuration: Create an Nginx configuration file (e.g., default.conf) inside a directory like ./nginx/conf.d. This file will configure Nginx to act as a reverse proxy, forwarding requests to the WordPress container (specifically, the PHP-FPM process within it).
server {
    listen 80;
    server_name your_domain.com; # Replace with your domain or IP

    root /var/www/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass wordpress:9000; # 'wordpress' is the service name in docker-compose
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
  • Deployment: Navigate to your project directory and run docker-compose up -d to build and start the containers.
  • Access WordPress: Once the containers are running, you can access your WordPress site by navigating to the configured server_name or the host’s IP address in your web browser.

This setup provides a robust and scalable environment for running WordPress, leveraging Nginx for efficient request handling and Docker for containerization and isolation. You can further enhance this with SSL certificates (e.g., using Certbot in a separate container) and persistent storage for data.

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다