pm.max_children is the single most impactful PHP-FPM tuning variable and the one most frequently set to an arbitrary number copied from a Stack Overflow answer written for a different server. Setting it too low causes request queuing and 502 errors under normal load; setting it too high exhausts RAM and triggers the OOM killer, which kills processes non-deterministically and takes the site down harder than the overload would have. The correct value is a calculation, not a guess.
How to verify
Measure the current state before touching anything:
# Available RAM (subtract buffers/cache — Linux reclaims these)
free -m
# Total used by PHP-FPM process tree
ps aux | grep 'php-fpm' | grep -v grep | awk '{sum += $6} END {print sum/1024 " MiB"}'
# Per-worker RSS sample (the number to base calculations on)
ps aux | grep 'php-fpm: pool www' | grep -v grep | awk '{print $6}' | sort -n
# Check current max_children setting
grep 'pm.max_children' /etc/php/8.3/fpm/pool.d/www.conf
# Check listen queue (non-zero = workers are saturated now)
curl -s 127.0.0.1/status | grep 'listen queue'
What’s happening
Each PHP-FPM worker is a forked child process. When the kernel forks, it creates a copy-on-write clone of the master’s address space. As the worker processes requests, it writes to memory (loading classes, building responses, allocating buffers) and diverges from the master’s pages — this divergence is the Resident Set Size (RSS) that ps reports in column 6.
RSS grows as workers process requests and stabilises once the hot code paths are warm (typically after the first 5–20 requests for a framework application). After warmup, worker RSS is roughly constant unless the application has a memory leak. The pm.max_requests directive recycles workers to flush any accumulated growth.
The formula:
available_ram = total_ram - os_overhead - competing_services
max_children = floor(available_ram / p90_worker_rss)
“Available RAM” is what remains after the OS kernel, filesystem cache headroom, database processes (MySQL, PostgreSQL, Redis), Nginx, and any other services on the host claim their working sets. The filesystem cache is reclaimable — Linux will evict it under memory pressure — but if you starve it entirely, disk I/O spikes sharply. Leave at least 20% of total RAM for the OS and cache.
The procedure
1. Sample worker RSS under production-like load.
Run this during a period of normal activity, not idle:
ps aux | grep 'php-fpm: pool www' | grep -v grep | awk '{print $6}' | sort -n | \
awk 'BEGIN{c=0; s=0} {a[c++]=$1; s+=$1} END{
print "min:", a[0]/1024, "MiB";
print "max:", a[c-1]/1024, "MiB";
print "avg:", s/c/1024, "MiB";
print "p90:", a[int(c*0.9)]/1024, "MiB"
}'
Use the p90 (90th percentile) value for the calculation, not the average. Using the average underestimates peak memory and leaves headroom for OOM events on the worst-case workers.
2. Measure memory available to PHP-FPM.
free -m | awk '/^Mem:/ {
available = $7
print "Available (kernel estimate):", available, "MiB"
}'
# Identify competing services
ps aux --sort=-%mem | head -20
# MySQL/MariaDB buffer pool size (if running)
mysql -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size'\G" 2>/dev/null | grep Value
Subtract from total RAM:
- OS kernel + system daemons: typically 200–400 MiB on a 4 GiB node
- MySQL/MariaDB
innodb_buffer_pool_sizeplus overhead: whatever is configured - Redis
maxmemoryif present - Nginx worker memory: typically 20–50 MiB for all workers combined
- 15–20% of total RAM for OS page cache
3. Apply the formula.
Example: 4 GiB node, p90 worker RSS = 128 MiB, MySQL buffer pool 512 MiB, Nginx 40 MiB, OS/cache reserve 800 MiB.
available = 4096 - 800 - 512 - 40 = 2744 MiB
max_children = floor(2744 / 128) = 21
Apply a 10% safety margin: max_children = 19.
4. Set and validate.
pm.max_children = 19
pm.max_requests = 500
systemctl reload php8.3-fpm
# Watch memory after reload
watch -n5 "free -m && ps aux | grep 'php-fpm: pool' | grep -v grep | wc -l"
Under load, confirm the listen queue stays at 0 and RSS per worker stays near the sampled p90:
curl -s 127.0.0.1/status?full | grep -E 'listen queue|max children reached|active processes'
5. Iterate after a deployment. Framework updates, new Composer packages, and plugin additions change worker RSS. Re-sample after every significant deployment and adjust max_children if the p90 shifts by more than 15%.
Common pitfalls
Sampling during low traffic. Worker RSS is smallest when workers are idle and OPcache hasn’t warmed yet. Sampling at 3 AM and basing max_children on that number leaves you with a value that’s 30–50% too high once daytime traffic warms the cache.
Using RES from top instead of RSS from ps. top’s RES column and ps’s RSS column report the same thing, but top’s VIRT (virtual memory) is much larger and irrelevant — don’t use it.
Ignoring shared memory. PHP-FPM workers share OPcache’s shared memory segment. That segment does not appear in individual worker RSS — it’s accounted in the master’s SHR column. OPcache’s memory is in addition to worker RSS and must be subtracted from available RAM separately: check php -i | grep opcache.memory_consumption.
Setting max_children once and never revisiting. The correct value drifts over time as the application grows. Schedule a quarterly RSS audit, especially after major Composer updates.
Not accounting for PHP extensions that allocate off-heap. Extensions like imagick, gd, and libxml allocate memory outside PHP’s heap that doesn’t appear in RSS until it’s faulted in. If your application processes images or parses large XML documents, bump the safety margin from 10% to 20%.
Capacity planning for PHP-FPM across multiple pools and servers — including RSS baselining, memory pressure alerting, and automated adjustment — is part of the Stack Harbor managed operations service.