How to Fix “server reached pm.max_children” Error in PHP-FPM 7 with Optimal Configuration


3 views

When running resource-intensive WordPress jobs on a PHP-FPM 7 + Nginx setup, you might encounter:

[pool www] server reached pm.max_children setting (5), consider raising it

This occurs when all available PHP-FPM child processes are occupied, preventing new requests from being processed.

On a 2GB RAM server, checking running processes shows:

ps aux | grep fpm
root      1508  0.0  1.5 367260 31380 ?        Ss   Nov05   0:11 php-fpm: master process
www-data 10231  0.0  2.7 453420 55540 ?        S    15:10   0:03 php-fpm: pool www
www-data 13266  0.0  2.4 449892 50900 ?        S    22:13   0:00 php-fpm: pool www
www-data 13572  0.0  1.8 372468 37740 ?        S    23:14   0:00 php-fpm: pool www

For a 2GB server, follow this calculation method:

# Get average PHP-FPM memory usage per process
ps -o rss= -p $(pgrep -f 'php-fpm: pool www' | head -1) | awk '{print $1/1024}'

# Calculate max_children based on available memory
# Formula: (Total RAM - System Reserve) / Average Process Memory
# Example: (2048MB - 512MB) / 50MB ≈ 30 max_children

Edit your pool configuration (typically at /etc/php/7.0/fpm/pool.d/www.conf):

[www]
user = www-data
group = www-data
listen = /run/php/php7.0-fpm.sock
pm = dynamic
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 10
pm.max_requests = 500

Combine these PHP-FPM tweaks with:

# Nginx fastcgi settings
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;

Set up monitoring with:

# Install and configure php-fpm status
apt-get install php7.0-fpm
echo "pm.status_path = /status" >> /etc/php/7.0/fpm/pool.d/www.conf
service php7.0-fpm restart

Then access metrics via:

curl http://localhost/status?json

When your PHP-FPM error logs show [pool www] server reached pm.max_children setting (5), you're facing a classic scaling issue. On a 2GB RAM server running Nginx with PHP-FPM 7.x, this bottleneck typically appears during WordPress cron jobs, WooCommerce imports, or heavy plugin operations.

Instead of the unsupported ps -ylC php-fpm --sort:rss, use this modern approach:

# Get memory per PHP-FPM process
sudo ps -eo size,pid,cmd --sort -size | grep 'php-fpm' | awk '{ hr=$1/1024; printf("%13.2f MB ",hr) } { for (x=4;x<=NF;x++) { printf("%s ",$x) } print "" }'

# Alternative for PHP-FPM 7+
sudo ps --format sz,pid,cmd -C php-fpm | sort -n

For a 2GB server:

Total Available RAM = 2048MB
OS Overhead = 300MB
Nginx = 100MB
MySQL = 300MB
Remaining = 1348MB

Average PHP-FPM process size = 50MB (from diagnostics)
pm.max_children = 1348 / 50 ≈ 27

Edit /etc/php/7.x/fpm/pool.d/www.conf:

pm = dynamic
pm.max_children = 15  # Conservative start for 2GB
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 5
pm.max_requests = 500  # Prevent memory leaks

Add to wp-config.php:

define('WP_MEMORY_LIMIT', '128M');
define('WP_MAX_MEMORY_LIMIT', '256M');

For resource-intensive operations:

# Process batch jobs via WP-CLI
wp cron event run --due-now --path=/var/www/html

# Use request termination for long processes
set_time_limit(0);
ignore_user_abort(true);

Install php-fpm-status:

sudo apt install php-fpm-status
sudo systemctl restart php7.x-fpm

# Then access:
http://your.server/fpm-status?json