Bạn đang mong muốn tạo một môi trường máy chủ web cho WordPress là php-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é. Show lục mục Yêu cầu tạo môi trường như sau
Điều kiện cần để tạo môi trường Docker WordPress Nginx
Tạo tệp docker-compose. yml cho WordPress + Nginx + MysqlTrước khi tạo tệp ví dụ ở đây mình sẽ sử dụng thư mục có tên là Khai báo cơ sở dữ liệu dịch vụ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
Khai báo dịch vụ wordpresswordpress: 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 dịch vụ wordpress
Khai báo dịch vụ 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 ... Dịch vụ máy chủ web Nginx
Tập tin docker-compose. yml hoàn thiệnversion: '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 tệp nginx. confTạo tệp cấu hình trong thư mục Mục đích tạo file này để config webser theo ý muốn của mình
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 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-network0 Kết quả
Chúc các bạn thành công, nếu có gì thắc mắc hoặc sai sót hãy comment bên dưới cho mình biết thêm nhé ? |