PHP-FPM Tuning for Nginx: pm.max_children, pm.start_servers, and More

How PHP-FPM Tuning for Nginx Works

PHP-FPM tuning for Nginx decides how many PHP worker processes run, how they are created, and when they are recycled. Get it wrong and a busy site either queues requests until it times out or eats all available RAM and crashes. This guide explains pm.max_children, pm.start_servers, and the other process manager settings, with a practical method to calculate sane values from your server’s RAM instead of copying numbers from another server.

PHP-FPM runs as a pool of worker processes. Nginx passes PHP requests to that pool over a socket, and each worker handles one request at a time. When all workers are busy, new requests wait in a queue. When the queue fills, Nginx returns 502 or 504 errors. More workers mean more parallel requests, but each worker uses memory, so the pool size is capped by RAM. That trade-off is the whole story of PHP-FPM tuning, and every other setting is a detail around it.

This guide assumes a standard LEMP stack. If you have not built one yet, the tutorial for installing WordPress on a LEMP stack sets up Nginx, PHP, and MariaDB in the right order. The pool configuration lives in /etc/php/8.3/fpm/pool.d/www.conf on Ubuntu 24.04, with the version number matching your installed PHP. Always confirm your PHP version with php -v before editing a path.

Calculate pm.max_children From Available RAM

The pm max children setting caps the number of worker processes. In the configuration file it appears as pm.max_children, and it is the single most important number in the pool. Likewise, the pm start servers value controls how many workers exist at boot before traffic arrives. Calculate it from real memory usage, not from a blog post’s example. First, check how much memory one PHP worker actually uses on your server:

ps -ylC php-fpm8.3 --sort:rss | awk '{s+=$8} END {print "avg RSS KB:", s/NR}'
free -m

A typical WordPress worker uses 30 to 80 MB of resident memory, depending on plugins. Take the average, reserve memory for the system, MySQL, and Nginx, and divide what is left by the per-worker figure. The formula is:

max_children = (total RAM - RAM for system and MySQL) / average worker RAM

On a 2 GB VPS with 1 GB reserved for the system and database and 50 MB workers, that gives 20 children. On a 4 GB server with the same assumptions, it gives 60. Both numbers are starting points. Watch memory under real traffic for a week and adjust, because plugin changes move the average worker size. A related sizing question is covered in the guide to how much RAM a WordPress VPS needs, which helps you decide whether tuning alone is enough or the server itself is undersized.

Choose a Process Manager Mode

The pm setting controls how workers are created. There are three modes, and the right one depends on traffic pattern:

pm = dynamic
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 8
pm.max_requests = 500

With dynamic, PHP-FPM starts pm.start_servers workers at boot and scales between the spare-server bounds as traffic changes. This is the right default for most sites because it keeps idle memory low while handling spikes. Set pm.start_servers to roughly a quarter of pm.max_children, pm.min_spare_servers to a small number that covers a sudden burst, and pm.max_spare_servers below half of pm.max_children so idle workers do not pile up.

With static, all pm.max_children workers start at boot and stay running. This gives the most predictable latency because no worker ever needs to start under load, but it uses full memory all the time. Static suits high-traffic sites on servers with plenty of RAM. With ondemand, workers start only when a request arrives and die after pm.process_idle_timeout. That saves memory on low-traffic sites but adds startup latency to the first request after idle, so it is a poor choice for a site that cares about response time.

The PHP-FPM process manager setting you choose should match how steady your traffic is. A site with constant traffic benefits from static. A site with clear peaks and quiet nights benefits from dynamic. A personal site visited a few times a day can use ondemand without anyone noticing.

Recycle Workers With pm.max_requests

The pm.max_requests setting restarts each worker after it serves a set number of requests. This protects against slow memory leaks in PHP code or extensions. A value of 500 to 1000 is reasonable for WordPress. Setting it too low causes constant worker restarts, which wastes CPU. Setting it to 0 disables recycling, which is fine only if you are confident nothing leaks. Most production sites should recycle, because a slow leak that takes days to notice is worse than the small cost of a restart.

Two more settings deserve attention. pm.status_path exposes a status page that shows active processes, queue length, and slow requests. Enable it on a restricted location and check it when tuning, because it shows whether your pool is actually full or just slow. Opcode caching is separate from process tuning but just as important: the guide to OPcache settings for WordPress on PHP and Ubuntu explains how to stop PHP from recompiling scripts on every request, which reduces the CPU each worker needs.

After editing the pool file, restart PHP-FPM and check the status:

sudo php-fpm8.3 -t
sudo systemctl restart php8.3-fpm
sudo systemctl status php8.3-fpm --no-pager

Replace 8.3 with your PHP version. Restarting PHP-FPM drops in-flight requests, so do it during a quiet moment. Then watch the error log for “server reached pm.max_children” warnings. That message means the pool is full and requests are queuing, which is the signal to raise pm.max_children if RAM allows, or to add RAM if it does not. Choosing between the two stacks is a separate decision, and the LAMP versus LEMP comparison for WordPress hosting explains why the Nginx and PHP-FPM combination behaves the way it does under load.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.