Tune PHP-FPM Pool Settings for High-Traffic WordPress Sites on Linux

Tune the PHP-FPM pool for WordPress

PHP-FPM runs a pool of PHP worker processes; each worker handles one request at a time. Too few workers, and requests wait in a queue even though the CPU is idle. Too many, and the server runs out of memory and starts swapping, which is worse. The right number follows from two measurements: the memory a worker uses and the memory the server can give to PHP.

Problem: Under load the site answers with 502 or 504 errors and the PHP-FPM log says “server reached pm.max_children”, or the opposite: the server swaps and everything is slow.

Solution: Measure the memory per worker, calculate pm.max_children from the RAM that is left after MySQL and the system, choose pm = dynamic or ondemand, recycle workers with pm.max_requests, and turn on the status page and the slow log to check the result.

Measure the workers

ps -o rss= -C php-fpm8.1 | awk '{ sum += $1; n++ } END { printf "%d workers, %.0f MB average\n", n, sum / n / 1024 }'
free -m

Measure under normal traffic, not right after a restart: a WordPress worker with WooCommerce typically uses 60 to 120 MB once it has served a few requests.

Calculate

RAM of the server                     4096 MB
- MySQL (innodb_buffer_pool_size + ~20%) 1300 MB
- system, Nginx, Redis, cron jobs       500 MB
= available for PHP-FPM               2296 MB

pm.max_children = 2296 / 90 MB per worker = 25

The pool configuration

; /etc/php/8.1/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 25
pm.start_servers = 6
pm.min_spare_servers = 4
pm.max_spare_servers = 8
pm.max_requests = 500

pm.status_path = /fpm-status
request_slowlog_timeout = 5s
slowlog = /var/log/php8.1-fpm-slow.log
request_terminate_timeout = 120s

dynamic keeps some idle workers ready, which suits a site with constant traffic. For a small server with several low-traffic sites, pm = ondemand with pm.process_idle_timeout = 10s starts workers only when requests come and frees the memory afterwards. pm.max_requests restarts each worker after 500 requests, which contains slow memory leaks in plugins.

request_terminate_timeout kills a request that runs longer; keep it above the longest legitimate web request, such as an import started in the admin (WP-CLI runs without FPM and is not affected), and in line with the Nginx fastcgi_read_timeout.

Apply and check:

sudo php-fpm8.1 -t && sudo systemctl reload php8.1-fpm

The status page

# Nginx, inside the server block
location = /fpm-status {
    allow 127.0.0.1;
    deny all;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
curl -s http://127.0.0.1/fpm-status
# active processes, idle processes, listen queue, max children reached

A growing “listen queue” or a “max children reached” counter above 0 means the pool is too small for the traffic, if there is memory left. If there is none, the fix is page caching (see Nginx FastCGI cache), not more workers.

NOTE: The slow log shows a PHP stack trace for every request that takes longer than request_slowlog_timeout. It is the quickest way to find the plugin or the remote API call that holds the workers. Look at it before changing the numbers.