Docker Build WordPress 思路2 – docker compose 迁移

整体的思路:

  • 备份文件过来 放到web 然后wget 就好
  • 备份文件放在新主机 /bk 目录下, 其中.sql 需要镜像进去并且导入到wordpress,这个通过共享路径操作。wp文件解压作为目录直接镜像进去也可以了。
  • git clone 下来配置文件 直接docker-compose up -d 运行启动。

以下的配置文件都可以git保存下来,同时又敏感信息,一定要私有化仓储。

我用的docker-compose.yml 文件:

version: '3.3'

services:
  db:
    image: mysql:5.7
    restart: unless-stopped
    environment:
       MYSQL_DATABASE: wordpress
       
       MYSQL_ROOT_PASSWORD: wordpress
       
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress
    volumes: 
      - /web/sql:/var/lib/mysql
      - /web/import:/test

  wordpress:
    depends_on: 
      - db
    image: wordpress:5.1.1-fpm-alpine
    restart: unless-stopped
    environment:
       WORDPRESS_DB_HOST: db:3306 # host
       
       WORDPRESS_DB_NAME: wordpress # name
       
       WORDPRESS_DB_USER: wordpress # user
       WORDPRESS_DB_PASSWORD: wordpress # password
       
    ports:
       - "8000:80"   
    volumes:
      - /web/wp:/var/www/html
      
  webserver:
    depends_on:
      - wordpress
    image: nginx:1.15.12-alpine
    restart: unless-stopped
    ports:
      - "80:80"
    volumes:
      - ./wp:/var/www/html
      - ./nginx-conf:/etc/nginx/conf.d

其中volumes:

  • /web/import:/test 用于从cp文件夹复制备份的.sql到镜像,并且导入数据库, 导入到默认的wordpress数据库
  • /web/sql:/var/lib/mysql 为数据库共享数据卷
  • /web/wp:/var/www/html 为迁移的文件的目录

额外还有一个nginx.conf 位于./nginx-conf, 这个配置文件还没有配置域名的。。

server {
        listen 80;
        listen [::]:80;

        server_name example.com www.example.com;

        index index.php index.html index.htm;

        root /var/www/html;

        location ~ /.well-known/acme-challenge {
                allow all;
                root /var/www/html;
        }

        location / {
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass wordpress:9000;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
        }

        location ~ /\.ht {
                deny all;
        }

        location = /favicon.ico { 
                log_not_found off; access_log off; 
        }
        location = /robots.txt { 
                log_not_found off; access_log off; allow all; 
        }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
                expires max;
                log_not_found off;
        }
}

最后作为测试机,修改单独的域名,我这里直接用IP地址了。。

  • define(‘WP_HOME’, ‘http://107.172.157.152/’);
  • define(‘WP_SITEURL’, ‘http://107.172.157.152/’);

添加到wp-config.php 即可