07-PHP服务配置详解本文档详细介绍PHP服务的配置采用双容器架构nginx:alpine php:8.2-fpm-alpine实现静态文件服务和动态PHP处理分离。双容器架构说明PHP服务由两个容器组成┌─────────────────────────────────────────────────────────┐ │ PHP服务 (双容器) │ ├─────────────────────────────────────────────────────────┤ │ │ │ ┌─────────────────────┐ ┌─────────────────────────┐ │ │ │ php (nginx:alpine)│ │ php-fpm (php:8.2-fpm) │ │ │ │ 172.20.2.11 │ │ 172.20.2.21 │ │ │ │ │ │ │ │ │ │ 监听: 80 │◄───│ fastcgi_pass │ │ │ │ │ │ 监听: 9000 │ │ │ │ 静态文件 │ │ PHP解析 │ │ │ └─────────────────────┘ └─────────────────────────┘ │ │ │ │ shared volume: web-data │ │ (/usr/share/nginx/html /var/www/html) │ └─────────────────────────────────────────────────────────┘容器职责容器镜像IP职责phpnginx:alpine172.20.2.11静态文件服务、接收请求、转发PHP到fastcgiphp-fpmphp:8.2-fpm-alpine172.20.2.21解析PHP代码、执行逻辑通信流程客户端请求 → nginx-lb → php(172.20.2.11:80) │ ▼ fastcgi_pass php-fpm(172.20.2.21:9000) │ ▼ 读取/写入 共享卷 web-datanginx配置php-node*.confphp-node1.confuser nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; ​ events { worker_connections 1024; use epoll; multi_accept on; } ​ http { include /etc/nginx/mime.types; default_type application/octet-stream; ​ log_format main $remote_addr - $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for request_time: $request_time; ​ access_log /var/log/nginx/access.log main; ​ sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; client_max_body_size 100M; ​ gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xmlrss application/rssxml font/truetype font/opentype application/vnd.ms-fontobject image/svgxml; ​ server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.php index.html index.htm; ​ location / { try_files $uri $uri/ /index.php?$query_string; } ​ location ~ \.php$ { fastcgi_pass 172.20.2.21:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; include fastcgi_params; } ​ location /health { access_log off; return 200 php-healthy\n; add_header Content-Type text/plain; } ​ error_page 404 /404.html; error_page 500 502 503 504 /50x.html; } }节点配置差异节点配置文件fastcgi_passrootNode1php-node1.conf172.20.2.21:9000/usr/share/nginx/htmlNode2php-node2.conf172.20.2.22:9000/usr/share/nginx/htmlNode3php-node3.conf172.20.2.23:9000/usr/share/nginx/html关键配置项详解1. fastcgi_passlocation ~ \.php$ { fastcgi_pass 172.20.2.21:9000; # 指向本节点的php-fpm容器 ... }重要不能使用127.0.0.1:9000或localhost:9000必须使用php-fpm的外部IPmacvlan分配的IP每个节点的php.conf中IP不同2. root路径root /usr/share/nginx/html; # nginx容器内路径注意nginx容器使用/usr/share/nginx/htmlphp-fpm容器使用/var/www/html通过共享卷web-data保持一致3. SCRIPT_FILENAMEfastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;说明必须是nginx容器内的完整路径与root配置保持一致php.ini配置[PHP] upload_max_filesize 50M post_max_size 50M max_execution_time 60 memory_limit 256M display_errors Off log_errors On error_log /var/log/php/error.log date.timezone Asia/Shanghai ​ [Session] session.save_handler redis session.save_path tcp://172.20.3.11:6379?authYourStr0ng!Pass ​ [opcache] opcache.enable 1 opcache.memory_consumption 128 opcache.interned_strings_buffer 8 opcache.max_accelerated_files 4000配置项说明配置说明upload_max_filesize上传文件最大大小post_max_sizePOST数据最大大小max_execution_time脚本最大执行时间memory_limit脚本最大内存session.save_handlerSession存储方式session.save_pathRedis服务器地址opcache.*OPcache加速配置Docker Compose配置Node1示例php: image: nginx:alpine container_name: php networks: backend-net: ipv4_address: 172.20.2.11 volumes: - ./config/php/php-node1.conf:/etc/nginx/nginx.conf:ro - web-data:/usr/share/nginx/html restart: unless-stopped healthcheck: test: [CMD-SHELL, curl -f http://localhost/index.php /dev/null 21 || exit 1] interval: 10s timeout: 5s retries: 3 ​ php-fpm: build: ./config/php-fpm # 基于php:8.2-fpm-alpine安装pdo_mysql container_name: php-fpm networks: backend-net: ipv4_address: 172.20.2.21 volumes: - ./config/php/php.ini:/usr/local/etc/php/conf.d/custom.ini:ro - web-data:/var/www/html restart: unless-stopped healthcheck: test: [CMD, php-fpm, -t] interval: 10s timeout: 5s retries: 3 ​ volumes: web-data:服务IP分配节点phpphp-fpmNode1172.20.2.11172.20.2.21Node2172.20.2.12172.20.2.22Node3172.20.2.13172.20.2.23部署注意事项1. 配置文件必须先创建# 确保配置文件存在 touch /opt/cluster-deploy/config/php/php-node1.conf touch /opt/cluster-deploy/config/php/php-node2.conf touch /opt/cluster-deploy/config/php/php-node3.conf touch /opt/cluster-deploy/config/php/php.ini2. 正确配置fastcgi_passNode1:fastcgi_pass 172.20.2.21:9000;Node2:fastcgi_pass 172.20.2.22:9000;Node3:fastcgi_pass 172.20.2.23:9000;3. 共享卷一致性nginx容器挂载web-data:/usr/share/nginx/htmlphp-fpm容器挂载web-data:/var/www/html确保两边路径对应正确4. 健康检查端点每个php容器提供/health端点# 测试健康状态 curl http://172.20.2.11/health curl http://172.20.2.12/health curl http://172.20.2.13/health创建测试页面# 创建测试页面 cat /opt/cluster-deploy/index.php EOF ?php echo h1PHP Service Test/h1; echo pServer: . gethostname() . /p; echo pPHP Version: . phpversion() . /p; echo pTime: . date(Y-m-d H:i:s) . /p; echo pREMOTE_ADDR: . $_SERVER[REMOTE_ADDR] . /p; # 测试Redis连接 try { $redis new Redis(); $redis-connect(172.20.3.11, 6379); $redis-auth(YourStr0ng!Pass); echo pRedis: Connected/p; } catch (Exception $e) { echo pRedis: Error - . $e-getMessage() . /p; } # 测试Session session_start(); $_SESSION[test] Hello from PHP!; echo pSession ID: . session_id() . /p; echo pSession Data: . ($_SESSION[test] ?? Not set) . /p; ? EOF # 复制到共享卷 docker run --rm -v web-data:/data -v $(pwd)/index.php:/index.php alpine cp /index.php /data/常见问题Q1: 502 Bad Gateway检查php-fpm是否运行docker ps | grep php-fpm检查fastcgi_pass地址是否正确检查网络连通性ping 172.20.2.21Q2: PHP代码不执行检查SCRIPT_FILENAME路径确认root路径与挂载路径一致Q3: Session无法写入Redis检查Redis连接telnet 172.20.3.11 6379验证密码认证检查php.ini中的session配置验证命令# 查看PHP容器 docker ps | grep -E php|php-fpm # 测试PHP服务 curl http://172.20.2.11/index.php curl http://172.20.2.12/index.php curl http://172.20.2.13/index.php # 测试健康检查 curl http://172.20.2.11/health curl http://172.20.2.12/health curl http://172.20.2.13/health # 查看PHP-FPM日志 docker logs php-fpm # 查看PHP版本 docker exec php-fpm php -v下一步08-Redis配置详解.md - 了解Redis集群配置12-验证测试.md - 验证PHP服务