Hướng dẫn này sẽ tiến hành tạo một website WordPress thông qua Docker Compose, Nginx, PHP 8.1, MariaDB và Let’s Encrypt trên Windows. Trước hết tạo một thư mục chứa dự án mới. Ở đây mình sẽ đặt tên dự án là wp-project. Bạn có thể thay thế tên này bằng tên mà bạn muốn.
Tạo một file docker-compose.yml
Trước thử nginx chạy được hay không?
version: “3”
services:
client:
image: nginx
ports:
– 8000:80
volumes:
– ./site:/usr/share/nginx/html
Tạo thư mục site chứa file index.html
docker-compose up –detach
Mở web lên xem thử: $ chromium http://localhost:8000
Tạo bộ mã chứng thực ssl
từ https://manage.sslforfree.com/certificates
Chỉnh lại file docker-compose.yml
version: "3.9"
services:
nginx:
container_name: nginx
image: nginx:latest
restart: unless-stopped
ports:
- 80:80
- 443:443
volumes:
- ./nginx/conf:/etc/nginx/conf.d
- ./ssl:/etc/ssl
- ./nginx/site:/usr/share/nginx/html
Tạo file default.conf trong thư mục \nginx\conf
server {
listen [::]:80;
listen 80;
server_name odoo.wpvn.xyz;
return 301 https://odoo.wpvn.xyz$request_uri;
}
server {
listen 443 ssl;
charset utf-8;
server_name odoo.wpvn.xyz;
root /usr/share/nginx/html;
index index.html index.htm;
ssl on;
ssl_certificate /etc/ssl/certificate.crt;
ssl_certificate_key /etc/ssl/private.key;
location ~ /.well-known/acme-challenge {
allow all;
root /usr/share/nginx/html;
}
}
Và 1 file options-ssl-nginx.conf nội dung search tìm trên mạng.
Trong thư mục ssl có 2 file certificate.crt (copy thêm nội dung của ca_bundle.crt vào), private.key
Giờ ssl đã chạy giữa client và server. Kết quả thấy có https và nội dung trang web.
Thực hiện cập nhật docker, bằng câu lệnh trong cmd:
docker-compose down
cảnh báo: Nó loại bỏ các thùng chứa, khối lượng và mạng.
sau đó, cập nhật lại bằng câu lệnh.
docker-compose up
Bạn không cần phải tạo lại hình ảnh bộ chứa mới khi thay đổi trong tệp YAML. Docker đủ khả năng để xác định những thay đổi trong tệp YAML sẽ tạo ra một hình ảnh được cập nhật khi bạn chạy lệnh trên. Đây là bước phổ biến mà tôi đã từng làm theo trong công việc của mình. Lệnh trên sẽ tạo lại các vùng chứa khi tìm thấy các thay đổi trong tệp YAML.
Thử làm cho odoo
như sau:
version: "3.9"
services:
db:
image: postgres:13
user: root
environment:
- POSTGRES_PASSWORD=odoo
- POSTGRES_USER=odoo
- POSTGRES_DB=postgres
restart: always # run as a service
volumes:
- ./postgresql:/var/lib/postgresql/data
odoo14:
image: odoo:14
user: root
depends_on:
- db
tty: true
command: --
# command: odoo scaffold /mnt/extra-addons/custom_module
environment:
- HOST=db
- USER=odoo
- PASSWORD=odoo
volumes:
#- /etc/timezone:/etc/timezone:ro
#- /etc/localtime:/etc/localtime:ro
# - ./entrypoint.sh:/entrypoint.sh # if you want to install additional Python packages, uncomment this line!
- ./addons:/mnt/extra-addons
- ./etc:/etc/odoo
restart: always # run as a service
nginx:
container_name: nginx
image: nginx:latest
restart: unless-stopped
depends_on:
- odoo14
ports:
- 80:80
- 443:443
volumes:
- ./nginx/conf:/etc/nginx/conf.d
- ./ssl:/etc/ssl
- ./nginx/site:/usr/share/nginx/html
Cấu hình default.conf để nginx nhận như sau:
upstream odoo {
server odoo14:8069 weight=1 fail_timeout=0;
}
upstream odoochat {
server odoo14:8072 weight=1 fail_timeout=0;
}
server {
listen 443 ssl;
charset utf-8;
server_name odoo.wpvn.xyz;
root /usr/share/nginx/html;
index index.html index.htm;
# log files
access_log /usr/share/nginx/html/openerp.access.log;
error_log /usr/share/nginx/html/openerp.error.log;
ssl on;
ssl_certificate /etc/ssl/certificate.crt;
ssl_certificate_key /etc/ssl/private.key;
proxy_read_timeout 720s;
proxy_connect_timeout 720s;
proxy_send_timeout 720s;
# Add Headers for odoo proxy mode
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
location /longpolling {
proxy_pass http://odoochat;
}
location / {
proxy_redirect off;
proxy_pass http://odoo;
}
location ~* /web/static/ {
proxy_cache_valid 200 90m;
proxy_buffering on;
expires 864000;
proxy_pass http://odoo;
}
# common gzip
gzip_types text/css text/scss text/plain text/xml application/xml application/json applicat$
gzip on;
}
## http redirects to https ##
server {
listen 80;
server_name odoo.wpvn.xyz;
# Strict Transport Security
add_header Strict-Transport-Security max-age=2592000;
rewrite ^/.*$ https://$host$request_uri? permanent;
}
Tiếp theo mình cấu hình wordpress.
version: "3.9"
services:
wordpress:
container_name: wordpress
image: wordpress:php8.1-apache
restart: always
stdin_open: true
tty: true
environment:
WORDPRESS_DB_HOST: mariadb
WORDPRESS_DB_USER: db_user
WORDPRESS_DB_PASSWORD: db_user_pass
WORDPRESS_DB_NAME: db_name
volumes:
- wordpress_data:/var/www/html
- ./wordpress:/var/www/html
mariadb:
container_name: mariadb
image: mariadb
restart: always
environment:
MYSQL_DATABASE: db_name
MYSQL_USER: db_user
MYSQL_PASSWORD: db_user_pass
MYSQL_RANDOM_ROOT_PASSWORD: 'root_pass'
volumes:
- db_data:/var/lib/mysql
nginx:
container_name: nginx
image: nginx:latest
restart: unless-stopped
ports:
- 80:80
- 443:443
volumes:
- ./nginx/conf:/etc/nginx/conf.d
- ./certbot/conf:/etc/nginx/ssl
- ./certbot/data:/var/www/html
certbot:
container_name: certbot
image: certbot/certbot:latest
command: certonly --webroot --webroot-path=/var/www/html --email youremail@domain.com --agree-tos --no-eff-email -d domain.com -d www.domain.com
volumes:
- ./certbot/conf:/etc/letsencrypt
- ./certbot/logs:/var/log/letsencrypt
- ./certbot/data:/var/www/html
volumes:
db_data:
wordpress_data:
Theo như nội dung cấu hình của File docker-compose.yml, thì chúng ta sẽ cần tạo file default.conf bên trong đường dẫn nginx/conf
server {
listen [::]:80;
listen 80;
server_name domain.com www.domain.com;
root /var/www/html;
index index.php;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html;
}
location / {
try_files $uri @apache;
}
location ~ ^/.user.ini {
deny all;
}
location ~* .(svg|svgz)$ {
types {}
default_type image/svg+xml;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location @apache {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://wordpress:80;
}
location ~[^?]*/$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://wordpress:80;
}
location ~ .php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://wordpress:80;
}
location ~/. {
deny all;
access_log off;
log_not_found off;
}
}
Tiếp theo có hai thư mục certbot và wordpress được tạo ra và nằm cùng với file docker-compose.yml
cerbot: Chứa các file liên quan đến chứng chỉ SSL của bạn.
wordpress: Chứa mã nguồn website WordPress của bạn.
Khi bạn có chứng chỉ SSL Let’s Encrypt, bạn có thể cấu hình HTTPS và chuyển hướng trang web sang HTTPS bằng cách chỉnh sửa file cấu hình default.conf
server {
listen [::]:80;
listen 80;
server_name domain.com www.domain;
return 301 https://domain.com$request_uri;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
server_name domain.com;
ssl_certificate /etc/nginx/ssl/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/live/domain.com/privkey.pem;
return 301 https://www.domain.com$request_uri;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
server_name www.domain.com;
ssl_certificate /etc/nginx/ssl/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/live/domain.com/privkey.pem;
root /var/www/html;
index index.php;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html;
}
location / {
try_files $uri @apache;
}
location ~ ^/.user.ini {
deny all;
}
location ~* .(svg|svgz)$ {
types {}
default_type image/svg+xml;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location @apache {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://wordpress:80;
}
location ~[^?]*/$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://wordpress:80;
}
location ~ .php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://wordpress:80;
}
location ~/. {
deny all;
access_log off;
log_not_found off;
}
}
https://huongdan.azdigi.com/cai-da-wordpress-docker-compose-nginx-apache-ssl/
https://blog.jarrousse.org/2022/04/09/an-elegant-way-to-use-docker-compose-to-obtain-and-renew-a-lets-encrypt-ssl-certificate-with-certbot-and-configure-the-nginx-service-to-use-it/
https://thana.in.th/2019/10/15/how-to-set-up-free-ssl-certificates-from-lets-encrypt-using-docker-and-nginx/
https://blog.emtwo.ch/2022/08/home-assistant-in-docker-with-ngix-and.html
https://www.bogotobogo.com/DevOps/Docker/Docker-Compose-Nginx-Reverse-Proxy-Multiple-Containers.php
Bài viết liên quan: