基于docker stack部署wordpress

1. Docker Hub上有关于wordpress部署方式的说明,我们使用docker stack方式部署wordpress,为便于部署其它应用,在wordpress前面部署了nginx进行反向代理,docker-compose.yml文件内容如下:

    • nginx处在frontend和backend网络中
    • wordpress处在backend网络中
    • mysql处在backend网络中
    • nginx的配置文件夹,html目录内容,log文件夹镜像到主机上(文件夹映射会自动在host与container之间同步,文件则不会,所以volumes bind最好用文件夹)
    • mysql的数据文件夹镜像到主机上
    • wordpress的完整目录(/var/www/html)镜像到主机上
    • wordpress安装目录下的wp-content文件夹保存了所有的插件及上传文件,但主程序(.php文件)在上层文件位中,为了以后的主程序升级,我们把整个目录镜像出去备份
    version: "3.3"
    services:
      nginx:
        image: nginx:1.13.8
        deploy:
          mode: "global"
          labels:
            - "position=frontend"
          resources:
            limits:
              cpus: "0.1"
              memory: 50M
          restart_policy:
            condition: any
        ports:
          - "80:80"
        volumes:
          - type: "bind"
            source: "/etc/nginx/"
            target: "/etc/nginx/"
            read_only: true
          - type: "bind"
            source: "/var/www/html/"
            target: "/usr/share/nginx/html"
            read_only: true
          - type: "bind"
            source: "/var/log/nginx/"
            target: "/var/log/nginx/"
        networks:
          - frontend
          - backend
    
      mysql5_7:
        image: mysql:5.7
        deploy:
          labels:
            - "position=backend"
          placement:
            constraints:
              - "node.role == manager"
          restart_policy:
            condition: any
        volumes:
          - type: "bind"
            source: "/var/lib/mysql5_7"
            target: "/var/lib/mysql"
        #restart: always
        environment:
          MYSQL_ROOT_PASSWORD: "**********"
          MYSQL_DATABASE: wordpress
          MYSQL_USER: wordpress
          MYSQL_PASSWORD: "**********"
        networks:
          - backend
    
      wordpress:
        depends_on:
          - mysql5_7
        image: wordpress:4.9.5-apache
        deploy:
          labels:
            - "position=backend"
          restart_policy:
            condition: any
        volumes:
          - type: "bind"
            source: "/var/lib/wordpress"
            target: "/var/www/html"
        environment:
          WORDPRESS_DB_HOST: mysql5_7:3306
          WORDPRESS_DB_USER: wordpress
          WORDPRESS_DB_PASSWORD: "***************"
        networks:
          - backend
    
    networks:
      frontend:
      backend:

    2. nginx中配置转发

    nginx.conf内容

    http {
        server {
            listen 80;
            location / {
                proxy_pass http://wordpress:80;
                proxy_set_header Host www.blog.org;
                proxy_pass_request_headers on;
            }
        }
    }
    

    3. 以stack方式部署, stack名称为web

    #docker stack deploy -c docker-compose.yml web
    

    4. 问题解决

    wordpress实例在docker container中以www-data用户运行,用户id为33,如下图,在文件上传或插件更新时会提示没有文件夹写权限


    通过修改主机上的镜像文件夹owner解决,虽然该用户id在主机上并不存在:

    #chown -R 33:33 /var/lib/wordpress/
    

    5. 不提供FTP信息自动完成WordPress更新

    WordPress默认更新时需要提供FTP信息远程更新, 通过配置可能设置自动更新:
    修改wp-config.php, 添加如下内容:

    define('FS_METHOD','direct');
    同时确保wordpress根目录的Owner已经修改为33

Leave a Comment