PHP-FPM pm.max_children Tuning: server reached pm.max_children
Fix server reached pm.max_children: read listen queue on the FPM status page, measure one worker's memory, and size max_children to the RAM PHP can use.
WARNING: [pool www] server reached pm.max_children setting (2), consider raising itPHP-FPM has every worker it is allowed (pm.max_children) busy, and further requests are waiting for one to free up. Before raising the number, use the status page to see how many requests are queuing and measure how much memory one worker uses. The ceiling is "memory you can give PHP ÷ size of one worker". The official docs give no recommended number, so you have to measure both.
[1] Check the current pool settings. These are the defaults from Ubuntu 24.04's php8.3-fpm package:
$ grep -E '^(pm|pm\.[a-z_]+|listen) *=' /etc/php/8.3/fpm/pool.d/www.conflisten = /run/php/php8.3-fpm.sockpm = dynamicpm.max_children = 5pm.start_servers = 2pm.min_spare_servers = 1pm.max_spare_servers = 3max_children = 5 means five requests at a time. The official php:8.3-fpm image ships the same values in www.conf. The three pm modes, per the FPM configuration docs:
| pm | Workers | Fits |
|---|---|---|
static | always max_children | dedicated box with known memory · no spawn delay |
dynamic | grows and shrinks between min/max_spare_servers, up to max_children | default · general use |
ondemand | spawned per request, killed after pm.process_idle_timeout (default 10s) | many pools that are mostly idle |
In every mode the ceiling is max_children.
[2] Turn on the status page and watch the queue. Add pm.status_path = /status to the pool and have the web server pass only that path, from internal addresses, to FPM — the official docs warn it exposes request URLs. You can also query FPM directly with cgi-fcgi (package libfcgi-bin). Point -connect at the pool's listen value. The output below comes from the official Docker image php:8.3-fpm (listen = 9000, TCP), hence 127.0.0.1:9000. The Ubuntu package listens on a socket, as in [1], so use -connect /run/php/php8.3-fpm.sock and run it as a user the socket permissions (listen.owner, listen.group) allow, or with sudo. This is a pool with 2 workers, hit with four 4-second requests, queried one second later:
$ SCRIPT_NAME=/status SCRIPT_FILENAME=/status REQUEST_METHOD=GET cgi-fcgi -bind -connect 127.0.0.1:9000pool: wwwprocess manager: dynamic…accepted conn: 5listen queue: 1max listen queue: 3listen queue len: 4096idle processes: 0active processes: 2total processes: 2max active processes: 2max children reached: 1slow requests: 0| Field | What it tells you |
|---|---|
listen queue | requests waiting for a free worker right now — nonzero means added latency |
max listen queue | the most that ever waited since start |
max children reached | 1 or more if the limit was ever hit |
idle processes | stuck at 0 for long means no headroom |
The counters reset when FPM restarts, so a zero seen at a quiet hour means nothing — look at peak time. If listen queue is always 0 and only max children reached ticks up occasionally, it was a short burst and not urgent.
[3] Measure one worker. Add ?full to the status query for per-worker last request memory (peak memory of the previous request). A worker that is busy shows 0, so read the Idle rows. This is after a script that builds a 20 MB string:
$ SCRIPT_NAME=/status SCRIPT_FILENAME=/status REQUEST_METHOD=GET QUERY_STRING=full cgi-fcgi -bind -connect 127.0.0.1:9000 | grep -E '^(pid|state|script|last request memory)'pid: 188state: Runningscript: -last request memory: 0pid: 189state: Idlescript: /srv/app/mem.phplast request memory: 23072768$ ps -o pid,rss,cmd -C php-fpm PID RSS CMD 186 24616 php-fpm: master process (/usr/local/etc/php-fpm.conf) 188 12148 php-fpm: pool www 189 11456 php-fpm: pool wwwThat worker used about 22 MB during the request, but its RSS in ps dropped to 11 MB afterwards. Measuring with ps at a quiet moment underestimates. Size a worker by the process memory measured under load: sample RSS from ps several times during the peak and take the high values, or use PSS (shared pages split between processes) from a tool like smem if you have one. last request memory is what the PHP memory manager counted for one request, so it misses memory allocated outside it and is only a hint; memory_limit is a per-script allocation limit, not a ceiling on the whole worker.
[4] Do the math and change it. Say that after reserving memory for the database, the web server and the OS you can give PHP 2 GB, and a worker peaks at 60 MB. The master process and some headroom come off first, so start under 30 rather than at 2048 ÷ 60 ≈ 34. Replace these numbers with what you measured.
Before editing, keep a copy of the current file. If a backup with that name already exists, pick another name so you don't overwrite it:
$ sudo cp -a /etc/php/8.3/fpm/pool.d/www.conf /etc/php/8.3/fpm/pool.d/www.conf.before-tuningThen change the values:
pm.max_children = 30pm.start_servers = 6pm.min_spare_servers = 4pm.max_spare_servers = 8If you leave start_servers out, it defaults to (min_spare + max_spare) / 2. Test the syntax, then reload:
$ sudo php-fpm8.3 -t$ sudo systemctl reload php8.3-fpmDo not reload unless -t prints test is successful.
[5] Roll back. If swap usage climbs or the OOM killer starts taking PHP workers (find the OOM victim), restore the old value right away:
$ sudo cp -a /etc/php/8.3/fpm/pool.d/www.conf.before-tuning /etc/php/8.3/fpm/pool.d/www.conf$ sudo php-fpm8.3 -t && sudo systemctl reload php8.3-fpm[6] Cut off long requests separately. If slow requests are why workers run out, a higher max_children just fills up again. request_terminate_timeout (default 0, off) kills a worker that exceeds it. With 3s and a 4-second request, the FPM log shows:
WARNING: [pool www] child 189, script '/srv/app/slow.php' (request: "GET /slow.php") execution timed out (3.005863 sec), terminatingWARNING: [pool www] child 189 exited on signal 15 (SIGTERM) after 4.005438 seconds from startNOTICE: [pool www] child 197 startedThe request is dropped with no response; an nginx front end reports it as upstream prematurely closed connection. To find what is slow before cutting it off, enable request_slowlog_timeout and slowlog. Inside a Docker container, though, the test logged failed to ptrace(ATTACH) child …: Operation not permitted and no backtrace was written — only the executing too slow warning.
Common traps
- Several pools add up. Sum
max_children × worker sizeover every pool; that total has to fit in PHP's share. - If raising
max_childrendoesn't help, the front end may be the limit (worker_connections tuning). Database connections also grow with the worker count (PostgreSQL too many clients · MySQL Too many connections). pm.max_requests(default 0, unlimited) respawns workers after N requests to work around leaks in extensions. Look at it if worker size keeps growing over time.