如何解决PHP-FPM报错: server reached max_children

本文转载自 How To: Solve PHP-FPM server reached max_children | Webcore Community | Webcore Cloud

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
OR: unable to read what child say: Bad file descriptor (9)

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: 检测PHP-FPM平均占用内存脚本

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值