There are many possible reasons why your PHP-FPM would reach the max_children.
Most common ones are:

  • A lot of concurrent site visitors
  • Slow execution of the PHP scripts due to server resources or buggy scripts
  • Very low setting of max_children setting in php-fpm config

If you have a busy server your php-fpm may crash out with the following error in the logs ( /var/log/phpX-fpm.log or /var/log/plesk-phpXX-fpm/error.log , where X is a PHP version):

tail -f /var/log/php-fpm/error.log

Look for the following entries:

WARNING: [pool example.com] server reached max_children setting (5), consider raising it
ERROR: unable to read what child say: Bad file descriptor (9)

Your website is not accessible with 503 Service Temporarily Unavailable or 502 Bad Gateway error.

Check that php-fpm service is running:

ps aux | grep fpm

Expected output should be running (once):

php-fpm: master process (/opt/plesk/php/5.6/etc/php-fpm.conf)

Check number of allowed running php-fpm processes

ps afvx | grep domain.com

Expected output:

7075 pts/0    S+     0:00     19   155 103148  884  0.1          \_ grep domain.com  
7018 ?        S      0:02    852  3394 375249 56132  8.3  \_ php-fpm: pool domain.com  
7019 ?        S      0:02   1778  3394 374469 55152  8.2  \_ php-fpm: pool domain.com  
7021 ?        S      0:01    735  3394 372729 52908  7.9  \_ php-fpm: pool domain.com  
7022 ?        S      0:04    716  3394 377661 59236  8.8  \_ php-fpm: pool domain.com

Resolution: Edit the conf file with the following:

vi /etc/php-fpm.conf

or

vi /etc/php-fpm.d/[domain.com].conf

For Plesk follow the instructions on the header of the conf file to override default php-fpm values by creating a custom php.ini file with specific fpm directives in:

/var/www/vhosts/system/mydomain.tld/conf/php.ini

Note that if the file is non-existent and you may need to create it.

Add the following entries:

[php-fpm-pool-settings]  
pm = dynamic  
pm.max_children = 25  
pm.start_servers = 10  
pm.min_spare_servers = 5  
pm.max_spare_servers = 20  
pm.max_requests = 498

Or for Plesk optionally just add the following entries:

php-fpm-pool-settings]
pm.max_children = 40

Restart php-fpm service:

service php-fpm restart

For Plesk you need to update PHP settings to apply the changes:

/usr/local/psa/bin/php_settings -u

If that fails you can try to recompile domains settings:

/usr/local/psa/admin/sbin/httpdmng --reconfigure-all

or for one domain:

/usr/local/psa/admin/sbin/httpdmng --reconfigure-domain example.com

Then restart php-fpm56 service (or php-fpmXX where X is a PHP version used in Plesk):

service plesk-php56-fpm restart

Verify the max_children limit has increased using the above commands.

If you need to calculate and change these values based on the amount of memory on the system the following command will help us to determine the memory used by each (PHP-FPM) child process:

ps -ylC php-fpm --sort:rss

The RSS column shows non-swapped physical memory usage by PHP-FPM processes in kilo Bytes.
If on an average each PHP-FPM process takes ~85MB of RAM on your server, appropriate value for pm.max_children can be calculated as:

pm.max_children = Total RAM dedicated to the web server / Max child process size

The server has 8GB of RAM, so:

pm.max_children = 6144MB / 85MB = 72

You need to take into account any other services running on the machine while calculating memory usage.
Then change the settings as follow:

pm.max_children = 70  
pm.start_servers = 20  
pm.min_spare_servers = 20  
pm.max_spare_servers = 35  
pm.max_requests = 500

You can check an average memory usage by single PHP-FPM process with this handy command:

ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'

You can use the same steps above to calculate the value for MaxClients for Apache web server - just substitute the php-fpm with httpd.


This article was last modified: May 20, 2017, 10:46 a.m.

0 Comments

Please log in to leave a comment.

Add or change tags.

A comma-separated list of tags.

Share

Hacker News

Top