Docker快速部署WordPress(docker-compose)

该文档可能不完美,存在部分问题,希望各路大神可以踊跃在评论区中提出意见或建议

一、为什么使用Docker?

  • 1、省去特别麻烦的安装步骤,只需要系统安装好docker和docker-compose即可
  • 2、迁移十分简单,只需要打包好整个文件夹,迁移到另外的系统再执行一条命令即可
  • 3、相比实机安装性能额外开销极小

二、环境要求

三、安装docker和docker-compose

1、安装docker:

2、安装docker-compose:

四、初始化环境

文件路径如下:

wordpress_docker ├── cert │ ├── xxxx.key │ └── xxxx.crt ├── docker-compose.yml ├── log │ ├── mysql │ └── nginx │ ├── access.log │ ├── error.log │ ├── wp_access.log │ └── wp_error.log ├── mysql_data │ └── something_mysql_data ├── nginx_conf │ └── wordpress.conf └── wordpress_data └── something_wordpress_data

创建目录和文件:

mkdir -p ~/wordpress_docker/cert \ ~/wordpress_docker/log/nginx \ ~/wordpress_docker/log/mysql \ ~/wordpress_docker/mysql_data \ ~/wordpress_docker/wordpress_data \ ~/wordpress_docker/nginx_conf && \ touch ~/wordpress_docker/docker-compose.yml \ ~/wordpress_docker/nginx_conf/wordpress.conf

五、配置docker-compose.yml和wordpress.conf

1、docker-compose.yml内容如下

version: '3' networks: wordpress_net: name: wordpress_net ipam: driver: default config: - subnet: "172.16.1.0/16" services: nginx: image: nginx:1.18-alpine container_name: nginx restart: unless-stopped depends_on: - wordpress - mysql ports: - "80:80" - "443:443" environment: TZ: "Asia/Shanghai" # 默认时区为UTC+8,即东八区-中国时区,下同 volumes: - "./wordpress_data:/var/www/html:ro" - "./nginx_conf:/etc/nginx/conf.d:ro" - "./cert:/etc/nginx/cert:ro" - "./log/nginx:/var/log/nginx" networks: wordpress_net: ipv4_address: 172.16.1.10 mysql: image: mysql:5.7 container_name: mysql restart: unless-stopped environment: MYSQL_RANDOM_ROOT_PASSWORD: '*random_root_password*' # 默认使用随机密码,如需指定密码,请注释这行并解除下一行注释 # MYSQL_ROOT_PASSWORD: "**must_change**your_mysql_root_password!" # 指定root密码,请务必更改 MYSQL_DATABASE: "wordpress_db" MYSQL_USER: "wordpress_user" MYSQL_PASSWORD: "**must_change**your_wordpress_db_user_password!" # wordpress用户的密码,请务必更改 TZ: "Asia/Shanghai" volumes: - "./mysql_data:/var/lib/mysql" - "./log/mysql:/var/log/mysql" networks: wordpress_net: ipv4_address: 172.16.1.11 wordpress: image: wordpress:5.6-php7.4-fpm-alpine container_name: wordpress restart: unless-stopped depends_on: - mysql environment: WORDPRESS_DB_HOST: "mysql" WORDPRESS_DB_NAME: "wordpress_db" WORDPRESS_DB_USER: "wordpress_user" WORDPRESS_DB_PASSWORD: "**must_change**your_wordpress_db_user_password!" # 对应上述MYSQL_PASSWORD,请务必更改 TZ: "Asia/Shanghai" volumes: - "./wordpress_data:/var/www/html" networks: wordpress_net: ipv4_address: 172.16.1.12

2、wordpress.conf

该配置文件仅提供部分示例,具体配置请根据自身需求决定

https:

server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name yourdomain.com; # 你的域名,需要更改 root /var/www/html; index index.php; gzip on; ssl_certificate /etc/nginx/cert/xxxx.crt; # 你的证书,需要更改 ssl_certificate_key /etc/nginx/cert/xxxx.key; # 你的密钥,需要更改 ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ [^/]\.php(/|$) { fastcgi_pass wordpress:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } access_log /var/log/nginx/wp_access.log; error_log /var/log/nginx/wp_error.log; } server { listen 80; listen [::]:80; server_name yourdomain.com; # 你的域名,需要更改 rewrite ^(.*)$ https://$host$1 permanent; }

http:

server { listen 80; listen [::]:80; server_name yourdomain.com; # 你的域名,需要更改 root /var/www/html; index index.php; gzip on; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ [^/]\.php(/|$) { fastcgi_pass wordpress:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } access_log /var/log/nginx/wp_access.log; error_log /var/log/nginx/wp_error.log; }

六、启动

$ docker-compose -f ~/wordpress_docker/docker-compose.yml pull && \ $ docker-compose -f ~/wordpress_docker/docker-compose.yml up -d # 输出 Pulling mysql ... done Pulling wordpress ... done Pulling nginx ... done Creating network "wordpress_net" with the default driver Creating mysql ... done Creating wordpress ... done Creating nginx ... done $ docker ps # 输出 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3142373f9d71 nginx:1.18-alpine "/docker-entrypoint.…" 56 seconds ago Up 53 seconds 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp nginx b5c91da67ef9 wordpress:5.6-php7.4-fpm-alpine "docker-entrypoint.s…" 57 seconds ago Up 56 seconds 9000/tcp wordpress 5260588dff7e mysql:5.7 "docker-entrypoint.s…" 59 seconds ago Up 56 seconds 3306/tcp, 33060/tcp mysql

七、迁移

1、打包并压缩

# 无需关闭container sudo tar -zcf wordpress_docker.tgz ~/wordpress_docker

2、压缩包自行迁移到目标主机

3、解压

tar -zxf wordpress_docker.tgz ~/

4、启动

# 确认主机内含有docker和docker-compose docker-compose -f ~/wordpress_docker/docker-compose.yml pull && \ docker-compose -f ~/wordpress_docker/docker-compose.yml up -d

八、常见问题

  • 1、我可以选择不同版本的docker镜像吗?
    可以,但是请务必确认wordpress镜像带有php-fpm。版本号无所谓,自行测试兼容性

  • 2、为什么选择Nginx,Apache不行吗?
    当然可以选择Apache,只不过设想在阿里云学生机(1核2G)的配置下,能省资源就尽量省(另外就是博主其实不会Apache)

  • 3、MySQL中,选择使用MYSQL_RANDOM_ROOT_PASSWORD后,root密码怎么找?
    请务必在第一次启动容器后,使用该命令docker-compose -f ~/wordpress_docker/docker-compose.yml logs mysql | grep "PASSWORD"

# 示例 mysql | 2021-03-10 15:43:22+08:00 [Note] [Entrypoint]: GENERATED ROOT PASSWORD: thoePha1Eibeechau4phoopheiTeeTh2
  • 更多问题可以在评论区下留言

原文链接:https://jacksonyoung.club/2021/03/wordpress_docker/

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享