The internet’s most-quoted PHP-FPM formula is “max_children = (total RAM - reserved) / per-worker RAM” and every word of that needs unpacking. The number you actually want depends on what counts as “per-worker RAM”, what the workload looks like, and whether the box does anything besides PHP. This article walks through how to measure those inputs and pick pm.max_children (and friends) from data rather than guesswork.
How to verify
Measure what each FPM worker actually uses right now:
ps -eo pid,rss,cmd --sort=-rss | grep "php-fpm: pool" | head
sudo systemctl status php8.3-fpm --no-pager | head
curl -s "http://127.0.0.1/fpm-status?full"
free -h
sudo grep -E '^(pm|memory_limit)' /etc/php/8.3/fpm/pool.d/www.conf /etc/php/8.3/fpm/php.ini
The status endpoint (if enabled in the pool config) reports active processes, idle processes, max active processes, and max children reached. If max children reached is non-zero, you are already capacity-bound and incoming requests have queued.
What’s happening
A PHP-FPM worker’s RSS has three components that grow over time. Opcache and APCu are shared across workers (shared memory) — they count once against the host, not per worker. The PHP runtime itself (interpreter, loaded extensions) is ~30 MiB. The application’s per-request working set (loaded classes, framework, in-memory query results) is what actually varies, usually 40-150 MiB per worker on a modern app like WordPress or Laravel.
The host’s RAM budget needs to subtract: the OS and kernel buffers (~500 MiB on a small box), Nginx (~100 MiB), the OPcache and APCu shared segments (256 MiB is typical), MySQL/Redis if local, and a 20% headroom for the page cache and bursts. Whatever is left is divisible into PHP workers.
pm = static means N workers always running. pm = dynamic keeps pm.min_spare_servers idle and grows up to pm.max_children, then idles back down. pm = ondemand only starts workers on incoming requests and lets them die. For latency-sensitive workloads static is best — no cold-start cost per request burst. For shared hosting where many idle apps coexist, ondemand makes sense.
The fix
-
Sample real per-worker RSS over an hour of production traffic:
while true; do ps -eo rss,cmd --no-headers | awk '/php-fpm: pool/ {sum+=$1; n++} END {if (n>0) print n, sum/n/1024 " MiB avg"}' sleep 60 done | tee /tmp/fpm-rss.logTake the 95th-percentile worker RSS, not the average. A pool sized to the average will OOM during a traffic burst.
-
Compute the available pool RAM:
pool_ram = total_ram - 500 (OS) - 100 (nginx) - 256 (opcache+apcu) - 20%_headroom - whatever_else_runs_hereOn a 4 GiB host with only Nginx + FPM + an external DB, that is roughly 4096 - 500 - 100 - 256 - 800 = 2440 MiB for workers.
-
Divide by p95 per-worker RSS to get
pm.max_children:max_children = pool_ram / p95_rssIf p95 is 120 MiB,
max_children = 2440 / 120 ≈ 20. Use 18-20 to leave a margin. -
Choose the process management mode based on workload shape:
; latency-sensitive single app: static pm = static pm.max_children = 20 pm.max_requests = 1000 ; bursty traffic, idle most of the time: dynamic pm = dynamic pm.max_children = 20 pm.start_servers = 8 pm.min_spare_servers = 4 pm.max_spare_servers = 12 pm.max_requests = 1000 -
Watch the FPM status
max children reachedcounter for a week. If it stays at 0, you are over-provisioned (or workload is fine); if it climbs every day, raisemax_childrenor add another host.
Operational notes
php.ini’smemory_limitis per-request, not per-worker. A worker that handled a 200 MiB request keeps that allocation across requests until it recycles —pm.max_requestsexists to bound that drift.- Do not confuse
pm.max_childrenwith thread count. PHP-FPM workers are processes — each consumes RSS independently. Settingmax_children = 100on a 2 GiB box guarantees OOM. - A box where MySQL or Redis also runs needs explicit memory accounting for them — InnoDB buffer pool, Redis
maxmemory. Subtract frompool_rambefore dividing. pm = ondemandadds visible latency to the first request after an idle period — the worker spawn is ~30 ms. Acceptable for low-traffic sites, painful for any site with response-time SLOs.- Slow-log (
request_slowlog_timeout = 5s+slowlog = /var/log/php-fpm-slow.log) is the single best diagnostic. It captures the stack trace of any request exceeding the threshold, which surfaces the endpoints actually filling worker capacity.
For investigating runaway worker memory growth, see Tracking PHP-FPM memory leaks. Stack Harbor’s managed operations practice runs the sampling-and-sizing cycle quarterly per host — a number that was right at launch is rarely right a year later as traffic and the app evolve.