写在前面

如果不对php-fpm做任何调优,这样部署的WordPress性能不如使用Apache镜像之后用宿主机的Nginx反代;以本站为例,相同的网络环境下完全加载DOM Content,前者需要7秒以上,后者约为2秒。

Docker-compose部署

Docker版本需要20.10+,才能使用host.docker.internal访问宿主机;使用宿主机的MySQL

需要挂载的两个目录:

/home/wordpress:/var/www/html是WordPress的工作目录

/home/wordpress/php.ini:/usr/local/etc/php/php.ini是Php-fpm的配置文件,这个文件不会被自动创建,需要手动创建,可以使用这里提供的压缩包中的php.ini文件来快速完成这个步骤

提供的压缩包中,post大小限制和上传文件的大小限制均为256M

version: '3.1'

services:
  wordpress:
    image: wordpress:6.2.2-php8.2-fpm
    container_name: wordpress
    restart: always
    ports:
      - 9000:9000
    environment:
      WORDPRESS_DB_HOST: host.docker.internal:3306
      WORDPRESS_DB_USER: someuser
      WORDPRESS_DB_PASSWORD: somepasswd
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - /home/wordpress:/var/www/html
      - /home/docker-wordpress-conf/php.ini:/usr/local/etc/php/php.ini
    extra_hosts:
      - "host.docker.internal:host-gateway"

Nginx反代配置

client_max_body_size 1024m;必须有,默认的大小不足以支撑一次上传多个文件

server{}块的root目录为宿主机上WP的目录

location ~ \.php$块的root目录为docker容器内部中WP的目录

# WordPress single site rules.
# Designed to be included in any server {} block.
# Upstream to abstract backend connection(s) for php
upstream php {
     server 127.0.0.1:9000;
}

server {
     listen 443 ssl;
     # Disable HTTP/2 and HTTP/3 while using FPM.
     server_name wp.colorfulstage.cn;

     ssl_certificate ...;
     ssl_certificate_key ...;

     root /home/wordpress;
     index index.php;

     client_max_body_size 1024m;

     location / {
          # This is cool because no php is touched for static content.
          # include the "?$args" part so non-default permalinks doesn't break when using query string
          try_files $uri $uri/ /index.php?$args;
     }

     location ~ \.php$ {
          #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
          include fastcgi_params;
          root /var/www/html;
          fastcgi_intercept_errors on;
          fastcgi_pass php;
          #The following parameter can be also included in fastcgi_params file
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     }

     #Return 403 while downing files like ".htaccess"
     location ~* \.(ini|htaccess|yml|yaml|conf|cnf|sh)$ {
          deny all;
     }
}
最后更新于 2023-08-04