WordPress部署(on Docker)

Avatar photo

首先打开iptables,fpm需要布置成隐藏的,只暴露前端的nginx。所以需要用iptables实现桥接。
CentOS7中默认防火墙是firewalld,需要执行

systemctl disable firewalld
systemctl stop firewalld

然后重新安装iptables服务

yum install iptables-services

Docker安装(link Docker安装(Linux CentOS)

从私人仓库获取Mysql 8.0

docker pull harbor.jusesgod.com:8443/library/mysql:8.0
docker run --name mysql8 -p 3306:3306 -p 33060:33060 \
-v /home/mysql8/data:/var/lib/mysql \
-v /home/mysql8/log:/var/log/mysql \
-v /home/mysql8/conf/conf.d:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=${mysql_pw} -d harbor.jusesgod.com:8443/library/mysql:8.0

安装完成后用docker inspect查看容器的ip ${mysql_ip},安装wordpress

docker run --name wordpress-php8 \
-e WORDPRESS_DB_HOST=192.168.50.109:3306 \
-e WORDPRESS_DB_USER=root \
-e WORDPRESS_DB_PASSWORD=870731juses* \
-e WORDPRESS_DB_NAME=wordpress \
-e WORDPRESS_TABLE_PREFIX=wp_ \
-v /home/dockerfile/wordpress/www/html:/var/www/html -d wordpress:6.4.3-php8.1-fpm

同样安装完之后要查看下容器的ip ${wordpress_ip}
wordpress和nginx布置在一个局域网,并共享一套php资源文件夹

安装nginx

docker run --name nginx-php8 -p 80:80 -p 8080:8080 -v /home/dockerfile/php-nginx/conf/nginx.conf:/etc/nginx/nginx.conf d -v /home/dockerfile/wordpress/www/html:/usr/share/nginx/html -d nginx

nginx配置文件:(放在conf.d文件夹中)

server {
    listen        80;
    server_name    ${servername};
    
    location ~ \.php$ {
        root /var/www/html;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass ${wordpress_ip}:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location / {
        root /usr/share/nginx/html;
        index index.php;
    }
}

尝试静态文件不通过Nginx,都是用php-fpm来调用,但是启动之后js无法加载,一下是进行的尝试性修改,但是没有成功:
1. 首先从镜像中拉取php-fpm的额外配置文件:
docker cp wordpress-php8:/usr/local/etc/php-fpm.d /home/dockerfile/wordpress/php-fpm.d
修改其中的www.conf
listen = 127.0.0.1:9000
security.limit_extensions =

启动wordpress时挂载新的配置文件
docker run --name wordpress-php8 \
-e WORDPRESS_DB_HOST=${mysql_ip}:3306 \
-e WORDPRESS_DB_USER=root \
-e WORDPRESS_DB_PASSWORD=${mysql_pw} \
-e WORDPRESS_DB_NAME=wordpress \
-e WORDPRESS_TABLE_PREFIX=wp_ \-v /home/dockerfile/wordpress/php-fpm.d:/usr/local/etc/php-fpm.d \
-v /home/dockerfile/wordpress/www/html:/var/www/html -d wordpress:6.4.3-php8.1-fpm

对应的js和css文件可以获取了,但是响应的content-type还是text/html
对nginx的请求进行修改,add_header X-Content-Type-Options nosniff; proxy_set_header Content-Typ “text/javascrip”;
不能修改网页的content-type

server {
  listen 80;
  server_name www.jusesgod.com;
  root /var/www/html;


  index index.php;


location ~ \.css {  
    fastcgi_split_path_info ^(.+?\.css)(/.*)$;
    add_header X-Content-Type-Options nosniff;
    proxy_set_header X-Powered-By "";
    proxy_set_header Content-Typ "text/css";
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #fastcgi_param PATH_INFO       $fastcgi_path_info;
    #fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;


    fastcgi_pass   172.17.0.3:9000;
    fastcgi_index  index.php;
  }


location ~ \.js {  
    fastcgi_split_path_info ^(.+?\.js)(/.*)$;
    add_header X-Content-Type-Options nosniff;
    proxy_set_header X-Powered-By "";
    proxy_set_header Content-Typ "text/javascrip";
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #fastcgi_param PATH_INFO       $fastcgi_path_info;
    #fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;


    fastcgi_pass   172.17.0.3:9000;
    fastcgi_index  index.php;
  }


  location / {  
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    #if (!-f $document_root$fastcgi_script_name) {
    #  return 404;
    #}
    add_header X-Content-Type-Options nosniff;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO       $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;


    fastcgi_pass   172.17.0.3:9000;
    fastcgi_index  index.php;
    proxy_set_header x-powered-by "";
  }
}

如果改成return 200 “”; 类型就会正常。所以应该是fastcgi导致的。暂时没有更多时间研究了。

所以得要在php服务器的机器上再添加一个nginx,如果是静态页面就通过nginx返回,只有php页面交给php处理。
这样就相当于使用了第一种模式。在hub.docker.com中的wordpress镜像中,对于使用fpm的用户有一个最佳实践:
Demonstration Docker config for WordPress on PHP-FPM behind Nginx
在部署nginx的时候,通过–volumes-from wordpress-fpm 来将nginx的文件挂载和wordpress的文件挂载关联起来,很优雅的避免了静态文件和php文件部署问题。

最终nginx的php.conf配置文件:

server {
  listen 80;
  server_name ${hostname};

  location / {
    root /usr/share/nginx/html;
    index index.php;
  }
  #rewrite /wp-admin$ $scheme://$host$uri/ permanent;

  location ~ [^/]\.php(/|$) {
    root /var/www/html;
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;

    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO       $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;


    fastcgi_pass   ${wordpress_ip}:9000;
    fastcgi_index  index.php;
  }
}