第一步、安装docker 与docker compose 请参考https://u.sb/debian-install-docker/
第二步、创建docker-compose.yml
mkdir wordpress
vim docker-compose.yml
version: '3.1'
services:
nginx: #nginx
image: nginx #镜像来源
container_name: nginx #镜像名称
restart: always #永远重启
network_mode: "host" #设置网络模式宿主机的IP和端口
volumes:
- /home/docker/nginx/nginx.conf:/etc/nginx/nginx.conf #nginx的配置文件挂载
- /home/docker/nginx/conf.d:/etc/nginx/conf.d #nginx的配置目录挂载
- /home/docker/nginx/logs:/var/log/nginx #nginx的日志目录挂载
- /home/docker/nginx/www:/www #项目目录挂载
- /etc/nginx/conf.d/cert/:/etc/nginx/conf.d/cert/ #ssl证书挂载
- /etc/localtime:/etc/localtime:ro #时间同步挂载
blog: #wordpress
image: wordpress:latest
container_name: blog
restart: always
ports:
- 30080:80 #暴露端口信息(HOST:CONTAINER)
environment: #设置环境变量
WORDPRESS_DB_HOST: mysql #数据库的配置参数
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: root
WORDPRESS_DB_NAME: blog
volumes: #文件挂载
- /home/docker/blog:/var/www/html
depends_on: #解决容器的依赖、启动先后的问题
- mysql
mysql: #mysql
image: mysql:8.0.26
container_name: mysql
restart: always
ports:
- 30001:3306
environment:
MYSQL_DATABASE: blog
MYSQL_ROOT_PASSWORD: root
volumes:
- /home/docker/mysql/data:/var/lib/mysql #数据挂载
- /home/docker/mysql/init:/docker-entrypoint-initdb.d #启动挂载
修改nginx配置文件
vim /home/docker/nginx/conf.d/default.conf
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name 你的域名;
ssl_certificate /etc/nginx/conf.d/cert/你的ssl文件.pem;
ssl_certificate_key /etc/nginx/conf.d/cert/你的ssl文件.key;
access_log /var/log/nginx/blog.access.log main;
error_log /var/log/nginx/blog.error.log notice;
location ~* /xmlrpc.php {
deny all;
}
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header Content-Security-Policy upgrade-insecure-requests;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header Host \$host;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_pass http://localhost:30080/;
}
}
server {
listen 80;
server_name fanglina.cn www.fanglina.cn;
rewrite ^(.*)$ https://\$host:443\$1 permanent;
}
第三步、启动容器
<code>docker compose up -d
第四步、如果报数据库错误:
docker restart mysql
docker restart blog
如果修改了docker-compose.yml,先docker compose down
之后再 docker compose up -d