acc-optimize-docker-php-fpm
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePHP-FPM Configuration Optimization
PHP-FPM配置优化
Provides production-ready PHP-FPM tuning for Docker containers based on workload type and available resources.
本文提供适用于Docker容器的生产级PHP-FPM调优方案,基于工作负载类型和可用资源进行配置。
Process Manager Modes
进程管理器模式
┌─────────────────────────────────────────────────────────────────┐
│ PHP-FPM PROCESS MANAGERS │
├─────────────────────────────────────────────────────────────────┤
│ STATIC: [W][W][W][W][W][W][W][W] Fixed pool │
│ Best for: dedicated containers, predictable load │
│ DYNAMIC: [W][W][W][W][ ][ ][ ][ ] Scales up/down │
│ Best for: variable load, shared resources │
│ ONDEMAND: [ ][ ][ ][ ][ ][ ][ ][ ] Starts on request │
│ Best for: low-traffic, dev environments │
└─────────────────────────────────────────────────────────────────┘┌─────────────────────────────────────────────────────────────────┐
│ PHP-FPM PROCESS MANAGERS │
├─────────────────────────────────────────────────────────────────┤
│ STATIC: [W][W][W][W][W][W][W][W] Fixed pool │
│ Best for: dedicated containers, predictable load │
│ DYNAMIC: [W][W][W][W][ ][ ][ ][ ] Scales up/down │
│ Best for: variable load, shared resources │
│ ONDEMAND: [ ][ ][ ][ ][ ][ ][ ][ ] Starts on request │
│ Best for: low-traffic, dev environments │
└─────────────────────────────────────────────────────────────────┘静态模式:固定进程池,最适合专用容器、可预测负载的场景
动态模式:进程数可上下扩展,最适合负载多变、资源共享的场景
按需模式:有请求时才启动进程,最适合低流量、开发环境
Memory Calculation Formula
内存计算公式
max_children = (available_memory - system_overhead) / avg_worker_memory
Where:
available_memory = container_memory_limit
system_overhead = ~50MB (OS + PHP-FPM master + nginx)
avg_worker_memory = 30-60MB (depends on application)| Container Memory | System Overhead | Worker Memory | max_children |
|---|---|---|---|
| 256MB | 50MB | 40MB | 5 |
| 512MB | 50MB | 40MB | 11 |
| 1024MB | 50MB | 40MB | 24 |
| 2048MB | 50MB | 50MB | 39 |
Measure actual worker memory:
bash
ps aux | grep "php-fpm: pool" | awk '{sum+=$6; n++} END {print sum/n/1024 " MB"}'max_children = (available_memory - system_overhead) / avg_worker_memory
Where:
available_memory = container_memory_limit
system_overhead = ~50MB (OS + PHP-FPM master + nginx)
avg_worker_memory = 30-60MB (depends on application)其中:
available_memory = 容器内存限制
system_overhead = 约50MB(操作系统 + PHP-FPM主进程 + nginx)
avg_worker_memory = 30-60MB(取决于应用程序)
| 容器内存 | 系统开销 | 工作进程内存 | max_children |
|---|---|---|---|
| 256MB | 50MB | 40MB | 5 |
| 512MB | 50MB | 40MB | 11 |
| 1024MB | 50MB | 40MB | 24 |
| 2048MB | 50MB | 50MB | 39 |
Configuration by Workload
测量实际工作进程内存
API Workload (Many Fast Requests)
—
ini
[www]
pm = static
pm.max_children = 20
request_terminate_timeout = 30s
request_slowlog_timeout = 5s
pm.max_requests = 1000
slowlog = /proc/self/fd/2bash
ps aux | grep "php-fpm: pool" | awk '{sum+=$6; n++} END {print sum/n/1024 " MB"}'Worker Workload (Few Long Requests)
按工作负载类型配置
—
API工作负载(大量快速请求)
ini
[www]
pm = dynamic
pm.max_children = 8
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 4
request_terminate_timeout = 300s
request_slowlog_timeout = 30s
pm.max_requests = 200ini
[www]
pm = static
pm.max_children = 20
request_terminate_timeout = 30s
request_slowlog_timeout = 5s
pm.max_requests = 1000
slowlog = /proc/self/fd/2Mixed Workload
工作进程负载(少量长耗时请求)
ini
[www]
pm = dynamic
pm.max_children = 15
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 8
request_terminate_timeout = 60s
request_slowlog_timeout = 10s
pm.max_requests = 500ini
[www]
pm = dynamic
pm.max_children = 8
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 4
request_terminate_timeout = 300s
request_slowlog_timeout = 30s
pm.max_requests = 200Dynamic Mode Formulas
混合工作负载
pm.start_servers = (min_spare + max_spare) / 2
pm.min_spare_servers = max_children * 0.25
pm.max_spare_servers = max_children * 0.75ini
[www]
pm = dynamic
pm.max_children = 15
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 8
request_terminate_timeout = 60s
request_slowlog_timeout = 10s
pm.max_requests = 500pm.max_requests by Application Type
动态模式计算公式
| Application Type | Recommended Value |
|---|---|
| Stateless API | 1000-5000 |
| Framework with ORM | 500-1000 |
| Legacy application | 100-500 |
| Memory-intensive processing | 50-200 |
pm.start_servers = (min_spare + max_spare) / 2
pm.min_spare_servers = max_children * 0.25
pm.max_spare_servers = max_children * 0.75Health Check and Monitoring
按应用程序类型设置pm.max_requests
ini
pm.status_path = /status
ping.path = /ping
ping.response = pongdockerfile
HEALTHCHECK \
CMD php-fpm-healthcheck || exit 1Key status metrics to monitor:
- active processes: current load
- listen queue: requests waiting (should be 0)
- max children reached: need more workers
- slow requests: performance issues
| 应用程序类型 | 推荐值 |
|---|---|
| 无状态API | 1000-5000 |
| 带ORM的框架 | 500-1000 |
| 遗留应用程序 | 100-500 |
| 内存密集型处理 | 50-200 |
Docker-Specific Settings
健康检查与监控
ini
; Logging to stdout/stderr
error_log = /proc/self/fd/2
access.log = /proc/self/fd/2
clear_env = yes
catch_workers_output = yes
decorate_workers_output = noini
pm.status_path = /status
ping.path = /ping
ping.response = pongdockerfile
HEALTHCHECK \
CMD php-fpm-healthcheck || exit 1需要监控的关键状态指标:
- active processes(活跃进程数): 当前负载
- listen queue(监听队列数): 等待处理的请求(应保持为0)
- max children reached(达到最大子进程数): 需要增加工作进程数
- slow requests(慢请求): 性能问题预警
Before/After Comparison
Docker专属配置
| Metric | Default | Optimized (static) | Improvement |
|---|---|---|---|
| max_children | 5 | 20 | +300% capacity |
| Idle memory (512MB) | 200MB wasted | ~50MB overhead | -75% waste |
| Request latency (p99) | 500ms | 150ms | -70% |
| Throughput (req/s) | 50 | 200 | +300% |
| Memory leak recovery | Never | Every 1000 req | Predictable |
ini
; Logging to stdout/stderr
error_log = /proc/self/fd/2
access.log = /proc/self/fd/2
clear_env = yes
catch_workers_output = yes
decorate_workers_output = noComplete Production Configuration
优化前后对比
ini
[global]
error_log = /proc/self/fd/2
log_level = warning
daemonize = no
[www]
user = www-data
group = www-data
listen = 0.0.0.0:9000
pm = static
pm.max_children = 20
pm.max_requests = 1000
request_terminate_timeout = 30s
request_slowlog_timeout = 5s
slowlog = /proc/self/fd/2
pm.status_path = /status
ping.path = /ping
ping.response = pong
clear_env = yes
catch_workers_output = yes
decorate_workers_output = no
access.log = /proc/self/fd/2
access.format = "%R - %u %t \"%m %r\" %s %{mili}dms %{mega}MMB %C%%"| 指标 | 默认配置 | 优化后(静态模式) | 提升效果 |
|---|---|---|---|
| max_children | 5 | 20 | +300% 处理能力 |
| 空闲内存(512MB容器) | 浪费200MB | 约50MB开销 | 减少75%内存浪费 |
| 请求延迟(p99) | 500ms | 150ms | 降低70% |
| 吞吐量(请求/秒) | 50 | 200 | 提升300% |
| 内存泄漏恢复 | 从不 | 每1000次请求 | 可预测的内存回收 |
Generation Instructions
完整生产环境配置
- Determine workload type: API, Worker, or Mixed
- Calculate max_children: Based on container memory and worker memory
- Select PM mode: Static for production, dynamic for variable load
- Configure timeouts: Based on expected request duration
- Set max_requests: Based on application memory behavior
- Enable monitoring: Status page, health check, slow log
ini
[global]
error_log = /proc/self/fd/2
log_level = warning
daemonize = no
[www]
user = www-data
group = www-data
listen = 0.0.0.0:9000
pm = static
pm.max_children = 20
pm.max_requests = 1000
request_terminate_timeout = 30s
request_slowlog_timeout = 5s
slowlog = /proc/self/fd/2
pm.status_path = /status
ping.path = /ping
ping.response = pong
clear_env = yes
catch_workers_output = yes
decorate_workers_output = no
access.log = /proc/self/fd/2
access.format = "%R - %u %t \"%m %r\" %s %{mili}dms %{mega}MMB %C%%"—
配置生成步骤
—
- 确定工作负载类型: API、工作进程或混合类型
- 计算max_children: 基于容器内存和工作进程内存
- 选择进程管理器模式: 生产环境推荐静态模式,负载多变场景用动态模式
- 配置超时时间: 基于预期的请求时长
- 设置max_requests: 基于应用程序的内存使用行为
- 启用监控: 状态页面、健康检查、慢日志