如何解决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
    评论
这段代码是一个 `docker-compose.yml` 文件,用于定义一个多容器的 Docker 应用程序。主要分为三个部分,分别是版本信息、网络信息和服务信息,下面是详细的翻译: ``` version: '3' # 版本号 networks: # 网络配置 likeadmin: # 网络名 driver: bridge # 网络类型 services: # 服务配置 nginx: # nginx服务 container_name: likeadmin-nginx # 容器名 image: nginx:1.23.1 # 容器镜像 restart: always # 容器停止后自动重启 depends_on: # 依赖关系 - "php" # 依赖于php服务 volumes: # 挂载卷 - ../server:/docker_php/server - ./config/nginx/conf.d:/etc/nginx/conf.d - ./log/nginx/logs:/logs networks: # 网络 - likeadmin ports: # 端口绑定 - "80:80" php: # php服务 container_name: likeadmin-php image: likeshop/php:8.0.22-fpm restart: always working_dir: /docker_php/server volumes: - ../server:/docker_php/server networks: - likeadmin ports: - "9000:9000" user: "1000:1000" mysql: # mysql服务 container_name: likeadmin-mysql image: mysql:5.7.29 #X86架构 #image: amd64/mysql:5.7.29 #arm架构 restart: always environment: # 环境变量 MYSQL_ROOT_PASSWORD: root volumes: # 挂载卷 - ./data/mysql5.7.29/lib:/var/lib/mysql - ./config/mysql/mysqld.cnf:/etc/mysql/my.cnf networks: # 网络 - likeadmin ports: # 端口绑定 - "3306:3306" redis: # redis服务 container_name: likeadmin-redis image: redis:7.0.4 restart: always volumes: # 挂载卷 - ./data/redis:/data networks: # 网络 - likeadmin ports: # 端口绑定 - "6379:6379" node: # node服务 container_name: likeadmin-node image: node:14.18.1 restart: always volumes: # 挂载卷 - ../admin:/likeadmin_php/admin networks: # 网络 - likeadmin tty: true working_dir: /likeadmin_php/admin ports: # 端口绑定 - "5173:5173" ``` 这个 `docker-compose.yml` 文件定义了5个服务,分别是 `nginx`、`php`、`mysql`、`redis` 和 `node`,并且它们都在同一个网络 `likeadmin` 中。每个服务都有自己的 `container_name`、`image`、`restart`、`volumes`、`networks` 和 `ports` 等配置信息,其中 `depends_on` 用于定义服务之间的依赖关系。 希望这个翻译能够帮助你更好地理解这个 `docker-compose.yml` 文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值