Why Tune PHP-FPM?
PHP-FPM (FastCGI Process Manager) handles PHP requests for Nginx and Apache. Default settings are conservative — tuning the process pool dramatically affects how many concurrent requests your server can handle.
Pool Configuration
Edit your pool config (e.g., /etc/php/8.2/fpm/pool.d/www.conf):
[www]
user = www-data
group = www-data
; Process manager type
pm = dynamic
; Max children (total PHP workers)
pm.max_children = 50
; Start with this many workers
pm.start_servers = 10
; Min/max idle workers
pm.min_spare_servers = 5
pm.max_spare_servers = 20
; Recycle workers after N requests (prevents memory leaks)
pm.max_requests = 1000Calculating max_children
Each PHP-FPM worker uses 20-60 MB of RAM depending on your application. Calculate:
max_children = (Available RAM for PHP) / (Average worker memory)
# Example: 4 GB server, 2 GB for PHP, 40 MB per worker
max_children = 2048 / 40 = ~50Check actual memory usage per worker:
ps -ylC php-fpm --sort:rss | awk '{sum+=$8; n++} END {print "Avg:", sum/n/1024, "MB"}'Static vs Dynamic vs Ondemand
- static — fixed number of workers, always running. Best for consistent high traffic.
- dynamic — scales between min and max spare. Best for variable traffic.
- ondemand — spawns workers only when needed. Best for low-traffic sites to save RAM.
Slow Log
Enable the slow log to find bottlenecks:
slowlog = /var/log/php-fpm-slow.log
request_slowlog_timeout = 5s