Docker ComposeHướng DẫnWordpress

Làm thế nào để tạo môi trường Docker WordPress Nginx

Hướng dẫn bạn cách tạo môi trường develop Wordpress sử dụng webserver là Nginx bằng Docker Compose.

Bạn đang mong muốn tạo một môi trường webserver cho WordPressphp-fpm + nginx nhanh bằng Docker Compose trên tất cả các hệ điều hành Windows, MacOS, Linux, cùng mình tham khảo các bước dưới đây nhé

Yêu cầu tạo môi trường như sau

  1. Domain cho môi trường dev local là: example.co:5000
  2. Webserver là nginx
  3. Dùng php-fpm
  4. Source code wordpress
  5. Mysql
  6. Tất cả đều được ánh xạ tới nơi chứa project ví dụ như source code wordpress được lưu trữ trong project mình có thể thêm sửa xóa

Điều kiện cần để tạo môi trường Docker WordPress Nginx

  1. Để làm được trước tiên bạn buộc phải cài Docker nhé, bài viết này sẽ hướng dẫn bạn cách cài đặt Docker trên  các hệ điều hành Windows, MacOS, Linux
  2. Cần hiểu các kiến thức/lệnh cơ bản về Docker (image, container, docker hub,...)
  3. Hoặc chẳng cần biết gì, chỉ cần biết LAMP WordPress trước đây cũng được, bạn chỉ việc làm theo từng bước sau đó ngâm cứu tìm hiểu kỹ hơn sau cũng được, nhưng trong từng bước mình sẽ hướng dẫn thật dễ hiểu cho bạn hiểu

Tạo file docker-compose.yml cho WordPress + Nginx + Mysql

Trước khi tạo file docker-compose.yml bạn chắc chắn con trỏ mình đang ở thư mục project bạn mong muốn tạo ví dụ ở đây mình sẽ dùng thư mục có tên là example

Khai báo service database

version: '3'

services:
  db:
    image: mysql:8.0
    container\_name: db-example
    restart: unless-stopped
    env\_file: ./app/.env
    environment:
      - MYSQL\_DATABASE=example
    volumes:
      - ./dbdata:/var/lib/mysql
      #- ./db/db.sql:/docker-entrypoint-initdb.d/install\_wordpress.sql #if you have db.sql of project input here
    command: '--default-authentication-plugin=mysql\_native\_password'
    networks:
      - app-network
...

Giải thích vài điểm quan trọng cho bạn hiểu

  1. Database ở đây mình sẽ sử dụng image mysql:8.0
  2. Container name sẽ là đặt là db-example
  3. File .env khai báo biến môi trường trong thư mục app (ví dụ như MYSQL_ROOT_PASSWORD(mật khẩu root của  mysql), MYSQL_USER, MYSQL_PASSWORD,...)
  4. Biến môi trường environment: MYSQL_DATABASE=example (tên database là example)
  5. Ánh xạ mysql tới folder dbdata ./dbdata:/var/lib/mysql

Khai báo service wordpress

  wordpress:
    depends\_on:
      - db
    image: wordpress:5.1.1-fpm-alpine
    container\_name: wordpress-example
    restart: unless-stopped
    env\_file: ./app/.env
    environment:
      - WORDPRESS\_DB\_HOST=db:3306
      - WORDPRESS\_DB\_USER=root
      - WORDPRESS\_DB\_PASSWORD=root
      - WORDPRESS\_DB\_NAME=example
    volumes:
      - ./app:/var/www/html
    networks:
      - app-network

Service wordpress:

  1. Có liên kết tới db depends_on: db
  2. Image mình sẽ sử dụng wordpress:5.5.1-fpm-alpine
  3. Container mình đặt là: wp-example
  4. Cũng khai báo biến môi trường trong file ./app/.env
  5. Khai báo biến môi trường trực tiếp: (WORDPRESS_DB_HOST, WORDPRESS_DB_USER, WORDPRESS_DB_PASSWORD, WORDPRESS_DB_NAME)
  6. Ánh xạ source code wordpress tới thư mục app: ./app:/var/www/html

Khai báo service webserver

...
  webserver:
    depends\_on:
      - wordpress
    image: nginx:1.15.12-alpine
    container\_name: webserver-example
    restart: unless-stopped
    ports:
      - "8080:80"
    volumes:
      - ./app:/var/www/html
      - ./nginx-conf:/etc/nginx/conf.d
    networks:
      - app-network
...

Service webserver Nginx

  1. Lệ thuộc buộc phải có sẵn wordpress: depends_on: - wordpress
  2. Image sử dụng là: nginx:1.15.12-alpine
  3. Đăt tên container webserver là webserver-example
  4. Port sử dụng trên máy host là 80 container là 5000: 5000:80
  5. Ánh xạ webroot tại thư mục app: ./app:/var/www/html
  6. Ánh xạ thư mục config nginx /etc/nginx/conf.d tới thư mục nginx-conf: ./nginx-conf:/etc/nginx/conf.d - Trong thư mục nginx-conf cần phải có file nginx.conf,  mình sẽ hướng dẫn tạo file nginx.conf bên dưới

File docker-compose.yml hoàn thiện

version: '3'

services:
  db:
    image: mysql:8.0
    container\_name: db-example
    restart: unless-stopped
    env\_file: ./app/.env
    environment:
      - MYSQL\_DATABASE=example
    volumes:
      - ./dbdata:/var/lib/mysql
      #- ./db/db.sql:/docker-entrypoint-initdb.d/install\_wordpress.sql #if you have db.sql of project input here
    command: '--default-authentication-plugin=mysql\_native\_password'
    networks:
      - app-network

  wordpress:
    depends\_on:
      - db
    image: wordpress:5.1.1-fpm-alpine
    container\_name: wordpress-example
    restart: unless-stopped
    env\_file: ./app/.env
    environment:
      - WORDPRESS\_DB\_HOST=db:3306
      - WORDPRESS\_DB\_USER=root
      - WORDPRESS\_DB\_PASSWORD=root
      - WORDPRESS\_DB\_NAME=example
    volumes:
      - ./app:/var/www/html
    networks:
      - app-network

  webserver:
    depends\_on:
      - wordpress
    image: nginx:1.15.12-alpine
    container\_name: webserver-example
    restart: unless-stopped
    ports:
      - "8080:80"
    volumes:
      - ./app:/var/www/html
      - ./nginx-conf:/etc/nginx/conf.d
    networks:
      - app-network

volumes:
  wordpress-example:
  dbdata:

networks:
  app-network:
    driver: bridge

Tạo file nginx.conf

Tạo file config trong thư mục nginx-conf/nginx.conf Mục đích tạo file này để config webser theo ý muốn mình:

  1. Domain sẽ là example.co
  2. Port host là 80
  3. Thực hiện chạy backend php-fpm port 9000
server {
        listen 80;
        #listen \[::\]:80;

        server\_name example.co www.example.co;

        index index.php index.html index.htm;

        root /var/www/html;

        server\_tokens off;

        add\_header X-Frame-Options "SAMEORIGIN" always;
        add\_header X-XSS-Protection "1; mode=block" always;
        add\_header X-Content-Type-Options "nosniff" always;
        add\_header Referrer-Policy "no-referrer-when-downgrade" always;
        add\_header Content-Security-Policy "default-src \* data: 'unsafe-eval' 'unsafe-inline'" always;
        # add\_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
        # enable strict transport security only if you understand the implications

        location / {
                try\_files $uri $uri/ /index.php$is\_args$args;
        }

        location ~ \\.php$ {
                try\_files $uri =404;
                fastcgi\_split\_path\_info ^(.+\\.php)(/.+)$;
                fastcgi\_pass wordpress-example:9000;
                fastcgi\_index index.php;
                include fastcgi\_params;
                fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;
                fastcgi\_param PATH\_INFO $fastcgi\_path\_info;
        }

        location ~ /\\.ht {
                deny all;
        }

        location = /favicon.ico {
                log\_not\_found off; access\_log off;
        }
        location = /robots.txt {
                log\_not\_found off; access\_log off; allow all;
        }
        location ~\* \\.(css|gif|ico|jpeg|jpg|js|png)$ {
                expires max;
                log\_not\_found off;
        }
}

Giải thích quan trọng ở dòng fastcgi_pass wp-example:9000; có nghĩa là nginx sẽ thực hiện run backend wordpress-fpm thông qua port 9000 với container name đặt ở trên là wp-example

Kết

  1. Mở terminal
  2. Thực hiện lệnh docker-compose up -d để Docker bắt đầu khởi tạo wordpress php-fpm + webserver nginx + mysql 8.0
  3. Thử truy cập http://example.co:5000 sẽ thấy kết quả ?

Chúc bạn thành công, nếu có gì thắc mắc hoặc có không đúng hãy comment bên dưới cho mình biết thêm nhé ? Ngoài ra mình còn đề link >>> Github để bạn có thể clone về local tham khảo thêm

Tags

Bài viết liên quan

2 Comments

    1. Nó cấu hình các tùy chọn cho truy cập website tại địa chỉ IP của máy chủ hoặc tên miền “example.co” và “www.example.co”. Nó đặt thư mục gốc của website tại “/var/www/html” và sử dụng “index.php”, “index.html”, “index.htm” như trang chủ. Nó cấu hình một số header bảo mật và tùy chọn cho tập tin đặc biệt (ví dụ: favicon, robots.txt) và xử lý tập tin .php bằng FastCGI.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Back to top button
Close