把wordpress迁移到docker容器中

st 发布于 2021-12-09 1,703 次阅读


备份数据库和webroot

备份数据库:mysqldump -u wordpressuser -p dbname > /path/to/destination/dump.sql

打包webroot:tar -czvf wordpress.tar.gz /var/www/html/wordpress

docker-compose.yml

version: "3.9"

services:
  db:
    image: mariadb
    container_name: wordpress_db
    volumes:
      - ./mariadb:/var/lib/mysql
      - ./dump.sql:/dump.sql
    restart: always
    environment:
      MARIADB_ROOT_PASSWORD: xxxxxxxxxxx
      MARIADB_DATABASE: wordpress
      MARIADB_USER: wordpress
      MARIADB_PASSWORD: xxxxxxxxxxx

  wordpress:
    depends_on:
      - db
    image: wordpress:fpm
    container_name: wordpress
    volumes:
      - ./webroot:/var/www/html
    ports:
      - "127.0.0.1:3030:9000"
    restart: always
    environment:
      WORDPRESS_DB_HOST: wordpress_db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: xxxxxxxxxxx
      WORDPRESS_DB_NAME: wordpress

还原数据库及webroot

首先docker-compose up -d db启动数据库,docker-compose exec db bash进入容器,mariadb -u root -pMARIADB_ROOT_PASSWORD启动数据库,SHOW DATABASES;检查数据库名,exit退出数据库操作界面,最后mariadb -uroot -pMARIADB_ROOT_PASSWORD --database=wordpress < dump.sql恢复数据库。

webroot只要tar -zxf wordpress.tar.gz --directory /然后mv一下文件让webroot处在./webroot即可。

nginx反代

conf文件:

server {
  listen 80;
  listen [::]:80;
  server_name wordpress.stsecurity.moe;

  access_log /var/log/nginx/wordpress-access.log;
  error_log /var/log/nginx/wordpress-error.log;

  root /wordpress/webroot;
  location /.well-known/acme-challenge/ { allow all; }
  location / { return 301 https://$host$request_uri; }
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name wordpress.stsecurity.moe;

  access_log /var/log/nginx/wordpress-access.log;
  error_log /var/log/nginx/wordpress-error.log;

  # Uncomment these lines once you acquire a certificate:
  ssl_certificate     /etc/nginx/ssl/wordpressfullchain.cer;
  ssl_certificate_key /etc/nginx/ssl/wordpresskey.key;

  root /wordpress/webroot;

  index index.php;

    location / {
      try_files $uri $uri/ /index.php?url=$uri;
    }

    location ~ \.(php)$ {
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass localhost:3030;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
      fastcgi_param PATH_INFO $fastcgi_path_info;
      include fastcgi_params;
      fastcgi_read_timeout 3000;
      proxy_connect_timeout 3000s;
      proxy_send_timeout   3000;
      proxy_read_timeout   3000;
    }

}

和一些能够找到的配置不同,fastcgi_param SCRIPT_FILENAME需要改成/var/www/html$fastcgi_script_name,对应容器内地址,因为root不是/var/www/html。

有关Sakurairo

本站目前主题是Sakurairo,它有个利用本地图片随机显示文章cover和首页cover的功能,但是我一开始用fpm-alpine镜像部署之后本地随机图就不再显示了。

访问API地址/wp-json/sakura/v1/image/feature?xxx得到的是["feature image"]而不是图片。

WordPress容器内log显示:

NOTICE: PHP message: PHP Warning:  Use of undefined constant GLOB_BRACE - assumed 'GLOB_BRACE' (this will throw an Error in a future version of PHP) in /var/www/html/wp-content/themes/Sakurairo/inc/classes/Images.php on line 146
weebofscience    | NOTICE: PHP message: PHP Warning:  glob() expects parameter 2 to be int, string given in /var/www/html/wp-content/themes/Sakurairo/inc/classes/Images.php on line 146
weebofscience    | NOTICE: PHP message: PHP Warning:  array_rand() expects parameter 1 to be array, null given in /var/www/html/wp-content/themes/Sakurairo/inc/classes/Images.php on line 147

根据php文档

Note: The GLOB_BRACE flag is not available on some non GNU systems, like Solaris or Alpine Linux.

更换镜像为fpm后问题解决。

更改权限

默认的容器因为权限问题没法自动安装和更新插件。

docker exec -it wordpress bash进入容器。

chown -R www-data:www-data /var/www && find /var/www/ -type d -exec chmod 0755 {} \; && find /var/www/ -type f -exec chmod 644 {} \;

更改对应权限。

自定义php配置

在docker-compose.yml中加上两个volume:

      - ./config/php.ini:/usr/local/etc/php/conf.d/custom.ini
      - ./config/zz-custom.conf:/usr/local/etc/php-fpm.d/zz-custom.conf
Sup
最后更新于 2021-12-11