php-fpm.conf中pm.max_children多大合适,报错you may need to increase pm.start_servers, or pm.min/max_spare_se

web使用的是nginx+php的架构,网站上线还没多久,所以优化方面基本只是做了些初始的配置。
查看php-fpm.log发现有警告,这些警告和网站的挂了个时间基本吻合。我就从这里开始入手。
先开始也是找了些文档,但是第二天还是出现问题。后来查看配置文件并翻译了下(百度),英文底子不好。pm模块类似apache的模块,是分静态和动态的。
 
网上说的很多调整都是基于动态居多,但是并没说么定义这个模块。所以大家用动态和静态还是要仔细看看配置文件

pm = static 
;此处静态
 
; The number of child processes to be created when pm is set to 'static' and the
 
; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
 
; This value sets the limit on the number of simultaneous requests that will be
 
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
 
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
 
; CGI. The below defaults are based on a server without much resources. Don't
 
; forget to tweak pm.* to fit your needs.
 
; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
 
; Note: This value is mandatory.
 
pm.max_children = 300
 
 
 
; The number of child processes created on startup.
 
; Note: Used only when pm is set to 'dynamic'
 
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
 
;pm.start_servers = 50
 
 
 
; The desired minimum number of idle server processes.
 
; Note: Used only when pm is set to 'dynamic'
 
; Note: Mandatory when pm is set to 'dynamic'
 
;pm.min_spare_servers = 20
 
 
 
; The desired maximum number of idle server processes.
 
; Note: Used only when pm is set to 'dynamic'
 
; Note: Mandatory when pm is set to 'dynamic'
 
;pm.max_spare_servers = 500
 
 
 
; The number of seconds after which an idle process will be killed.
 
; Note: Used only when pm is set to 'ondemand'
 
; Default Value: 10s
 
pm.process_idle_timeout = 10s;
 
 
 
; The number of requests each child process should execute before respawning.
 
; This can be useful to work around memory leaks in 3rd party libraries. For
 
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
 
; Default Value: 0
 
pm.max_requests = 10240

 红色字段就是定义方式的,定义好这个再去根据服务器情况设置参数
 
假如使用静态 pm.max_children这个参数会起作用,其余不会。动态反之。
 
2G内存pm.max_children大概开启50左右,按照实际情况来调优,这个是很必要的。

pm.max_children = 50 --允许创建的最大子进程数
pm.start_servers = 15 --起始进程数(服务启动后初始进程数量)
pm.min_spare_servers = 15 --最小空闲进程数(清理空闲进程后的保留数量)
pm.max_spare_servers = 35 --最大空闲进程数(当空闲进程达到此值时清理)

我的php-fpm.conf设置如下:

[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = notice

[www]
listen = /tmp/php-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 200
pm.start_servers = 20
pm.min_spare_servers = 20
pm.max_spare_servers = 80
request_terminate_timeout = 1200
request_slowlog_timeout = 0
slowlog = var/log/slow.log

php-fpm.log警告内容如下:

[07-Sep-2020 16:11:45] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 14 idle, and 178 total children
[07-Sep-2020 16:12:02] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 19 idle, and 191 total children
[07-Sep-2020 16:12:38] WARNING: [pool www] server reached pm.max_children setting (200), consider raising it
[07-Sep-2020 19:50:37] NOTICE: Terminating ...
[07-Sep-2020 19:51:48] NOTICE: fpm is running, pid 1271
[07-Sep-2020 19:51:48] NOTICE: ready to handle connections
[18-Sep-2020 17:38:01] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 14 idle, and 69 total children
[18-Sep-2020 17:38:02] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 14 idle, and 75 total children
[18-Sep-2020 17:38:16] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 9 idle, and 90 total children
[18-Sep-2020 17:38:17] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 8 idle, and 98 total children
[18-Sep-2020 17:38:22] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 5 idle, and 117 total children
[18-Sep-2020 17:38:23] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 18 idle, and 125 total children
[18-Sep-2020 17:38:24] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 15 idle, and 127 total children
[18-Sep-2020 17:38:25] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 19 idle, and 132 total children
[18-Sep-2020 17:38:26] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 8 idle, and 133 total children
[18-Sep-2020 17:38:27] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 6 idle, and 145 total children
[18-Sep-2020 17:38:34] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 15 idle, and 166 total children
[18-Sep-2020 17:38:35] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 17 idle, and 171 total children
[18-Sep-2020 17:38:45] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 15 idle, and 181 total children
[18-Sep-2020 17:38:46] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 0 idle, and 186 total children
[18-Sep-2020 17:38:47] WARNING: [pool www] server reached pm.max_children setting (200), consider raising it


[my_app]
;FastCGI/PHP-FPM使用UNIX Sockets
listen = /data/my_app/tmp/php.sock
listen.backlog = 300
user = www
group = www
pm = dynamic
;估算pm.max_children = (MAX_MEMORY - 500MB) / 20MB 
pm.max_children = 100
;推荐为最大的pm.max_children的%10
pm.start_servers = 10    ; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 1000
pm.status_path = /php_status

request_terminate_timeout = 0
request_slowlog_timeout = 0
slowlog = /data/my_app/logs/slow.log

补充:查看PHP-FPM内存占用的几个有用小命令,记录如下:

1.查看每个FPM的内存占用:

ps -ylC php-fpm --sort:rss
ps -ylC php-fpm --sort:rss|wc -l

当然,在后后面加 | wc -l可查看系统当前FPM总进程数,我的目前在8个左右。

PHP官方的建议设置值:可以用于web服务器的内存量 / 最大子进程数

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

2.查看FPM在你的机子上的平均内存占用:

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

3. php-fpm进程数设置多少合适

在我们做php高并发下的优化的时候,涉及到一个问题,就是fpm进程数设置多少合适,在有的文章中推荐设置为n或者n×2(n为cpu数量)
有的文档中推荐按照可用内存/30m (30m为fpm每个进程内存)
两种说法其实都没有问题,但是都忽略了一个基本的事实就是具体情况具体分析

基础知识

本段摘自 https://yq.aliyun.com/articles/33422

需要了解的参数是:pm、pm.max_children、pm.start_servers、pm.min_spare_servers、pm.max_spare_servers。
pm:表示使用那种方式,有两个值可以选择,就是static(静态)或者dynamic(动态)。
在更老一些的版本中,dynamic被称作apache-like。这个要注意看配置文件的说明。
下面5个参数的意思分别为:

pm = dynamic 如何控制子进程,选项有static和dynamic    
pm.max_children:静态方式下开启的php-fpm进程数量    
pm.max_requests:php-fpm子进程能处理的最大请求数    
pm.start_servers:动态方式下的起始php-fpm进程数量
pm.min_spare_servers:动态方式下的最小php-fpm进程数
pm.max_spare_servers:动态方式下的最大php-fpm进程数量

区别:
如果pm设置为 static,那么其实只有pm.max_children这个参数生效。系统会开启设置数量的php-fpm进程。
如果pm设置为 dynamic,那么pm.max_children参数失效,后面3个参数生效。
系统会在php-fpm运行开始 的时候启动pm.start_servers个php-fpm进程,
然后根据系统的需求动态在pm.min_spare_servers和pm.max_spare_servers之间调整php-fpm进程数。

在高并发场景(我们公司是一万并发测试)中,一般都是采用的static方式,因为一般来说我们的服务器都是有严格的业务分离,fpm服务器就是用来作为fpm服务器使用,除去还有一个nginx之外,不会去考虑有其他的服务器占用资源。

具体问题具体分析

在php的运行中,无非是两种场景

  1. 大运算
  2. 高io

1 大运算的场景,即 php程序需要用大量的cpu资源来进行数据计算之类的操作,在这种场景下,fpm进程可以设置为cpu数量的一倍或者两倍
2 高io场景,php的使用场景中(最起码是本电商场景中)基本上属于高io,因为程序花了大量的时间在等待redis返回等待数据库返回。高io场景下,因为cpu大多处在wa状态下,所以可以尽量的加大fpm进程数,所以这个时候使用内存/30m是更为合理的

经过我们自己真实压测,大量redis和mysql读写的io密集情况下,16G的内存,fpm我们设置为400个的时候qps比fpm 16个 32个要好不少

参考:https://zhuanlan.zhihu.com/p/94627701

posted @ 2020-09-22 14:48  码农编程进阶笔记  阅读(258)  评论(0编辑  收藏  举报
返回顶部 有事您Q我