Hướng dẫn host WordPress trên VPS với Dokploy: Docker Compose + Nginx + Redis

KP
Khoi Pro (Kevin)

Bài viết này hướng dẫn host WordPress trên VPS cá nhân thông qua Dokploy, sử dụng Docker Compose với Nginx, MariaDB, WordPress PHP-FPM và Redis cache.

1. Cấu trúc dự án

Tạo thư mục dự án với cấu trúc sau:

wordpress-dokploy/
├── docker-compose.yml
├── nginx/
│   └── default.conf
└── .env

2. docker-compose.yml

services:
  wordpress:
    image: wordpress:6.7-php8.3-fpm-alpine
    environment:
      WORDPRESS_DB_HOST: database
      WORDPRESS_DB_NAME: \${DB_NAME}
      WORDPRESS_DB_USER: \${DB_USER}
      WORDPRESS_DB_PASSWORD: \${DB_PASSWORD}
      WORDPRESS_CONFIG_EXTRA: |
        define('WP_REDIS_HOST', 'redis');
        define('WP_REDIS_PORT', 6379);
        define('WP_CACHE', true);
    volumes:
      - wp_data:/var/www/html

  database:
    image: mariadb:10.11
    environment:
      MYSQL_ROOT_PASSWORD: \${DB_ROOT_PASSWORD}
      MYSQL_DATABASE: \${DB_NAME}
      MYSQL_USER: \${DB_USER}
      MYSQL_PASSWORD: \${DB_PASSWORD}
    volumes:
      - db_data:/var/lib/mysql

  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - wp_data:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - wordpress

  redis:
    image: redis:7-alpine

volumes:
  wp_data:
  db_data:

3. Cấu hình Nginx

Tạo file nginx/default.conf:

server {
    listen 80;
    server_name _;
    root /var/www/html;
    index index.php;

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

    location ~ \.php$ {
        fastcgi_pass wordpress:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
    }

    location ~ /\. {
        deny all;
    }
}

4. File .env

DB_NAME=wordpress
DB_USER=wpuser
DB_PASSWORD=strong_password_here
DB_ROOT_PASSWORD=root_password_here

5. Deploy lên Dokploy

  1. Push toàn bộ code lên GitHub repository
  2. Trong Dokploy dashboard: Add Service > Docker Compose
  3. Chọn repository, chọn nhánh
  4. Thiết lập biến môi trường từ .env
  5. Bấm Deploy
  6. Dokploy tự động build, chạy container, cấp SSL Let’s Encrypt

Sau khi deploy, truy cập domain để cài đặt WordPress. Vào wp-admin, cài Redis Object Cache plugin và bật kết nối Redis.

Tham khảo: Dokploy là gì? | Dokploy + Laravel | Redis Object Cache

Viết bởi Khoi Pro (Kevin)

WordPress Core Contributor & General Translation Editor for Vietnamese. Founder of CODE TOT JSC. Chia sẻ kiến thức về WordPress, PHP, DevOps và freelancer.