使用 Docker Compose 部署 Wordpress

前言

最近又双叕开始折腾个人博客,这次就不想太费事了,直接上 WP。废话不多说,直接上代码。
PS:以下所有操作均基于 CentOS 7。
PS2:部分内容参考自:《Docker —— 从入门到实践》。

安装 Docker 及 Docker Compose

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 安装依赖包
yum install -y yum-utils

# 添加 yum 软件源(国内)
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo

# 添加 yum 软件源(官方)
# 国内、官方源视情况二选一
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

# 安装 docker-ce
yum install docker-ce docker-ce-cli containerd.io

# 将当前用户加入到 docker 用户组
usermod -aG docker $USER
# 配置为自动启动服务
systemctl enable docker
# 启动服务
systemctl start docker

# 安装 pip
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
# 安装 Docker Compose
pip install -U docker-compose

这里有个小坑,默认情况下,通过 Docker 安装的服务端口都是直接对外开放的,如果是在云服务器(比如阿里云的ECS)上部署,还可以通过安全组的功能来限制不被外网访问,但是如果是和博主一样在VPS上部署的,就需要自行配置防火墙规则了。使用 firewalld 配置的方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 删除已有的 Docker 链配置,该配置可能不存在
firewall-cmd --permanent --direct --remove-chain ipv4 filter DOCKER-USER

# 删除已有的 Docker 规则配置
firewall-cmd --permanent --direct --remove-rules ipv4 filter DOCKER-USER

# 添加 Docker 用户
firewall-cmd --permanent --direct --add-chain ipv4 filter DOCKER-USER

# 添加规则,注意,拒绝访问的规则一定要在最后。
# 允许容器访问外部
firewall-cmd --permanent --direct --add-rule ipv4 filter DOCKER-USER 0 -i docker0 -j ACCEPT -m comment --comment "allows incoming from docker"
# 允许容器访问网卡1
firewall-cmd --permanent --direct --add-rule ipv4 filter DOCKER-USER 0 -i docker0 -o eth0 -j ACCEPT -m comment --comment "allows docker to eth0"
# 允许容器访问外部
firewall-cmd --permanent --direct --add-rule ipv4 filter DOCKER-USER 0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "allows docker containers to connect to the outside world"
# 允许容器间互相访问
firewall-cmd --permanent --direct --add-rule ipv4 filter DOCKER-USER 0 -j RETURN -s 172.0.0.0/8 -m comment --comment "allow internal docker communication"
# 拒绝其他所有访问
firewall-cmd --permanent --direct --add-rule ipv4 filter DOCKER-USER 0 -j REJECT --reject-with icmp-host-unreachable -m comment --comment "reject all other traffic"
# 刷新规则
firewall-cmd --reload

如果规则命令有误,或者需要修改规则,也可以直接编辑 /etc/firewalld/direct.xml 文件,然后再执行刷新规则。

创建 docker-compose.yml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
version: "3"
services:
db:
container_name: mysql
image: mysql:8.0
command:
- --default_authentication_plugin=mysql_native_password
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
volumes:
- ./data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: ROOT用户密码
MYSQL_DATABASE: 数据库 Schema
MYSQL_USER: 数据库用户名
MYSQL_PASSWORD: 数据库密码
wordpress:
container_name: wordpress
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: 数据库 Schema
WORDPRESS_DB_USER: 数据库用户名
WORDPRESS_DB_PASSWORD: 数据库密码
volumes:
- ./html:/var/www/html
- ./config/upload.ini:/usr/local/etc/php/conf.d/upload.ini
volumes:
db_data:
wordpress:

其中的 ./config/upload.ini 是用来配置 php 上传相关的配置项,当然还可以在里面放入其他想要更改的配置。 然后执行 docker-compose up -d 就可以启动 Dokcer 并创建两个容器。

配置 nginx

执行 vi /etc/nginx/conf.d/wp.conf,按 {i} 键进入编辑模式,输入下列内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
upstream wordpress-workhorse {
server 127.0.0.1:8000 fail_timeout=10s;
}

server {
listen 80;
server_name 绑定的域名;

# 指定到刚才配置的路径
root /path/to/html;

location / {
proxy_pass http://wordpress-workhorse;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 7d;
access_log off;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}

编辑完成后,可以执行 nginx -t 命令检查配置是否正确,然后执行 systemctl reload nginx 刷新配置。
至此就可以通过域名正常访问 Wordpress 了。