![php5-fpm 最大化所有核心(nginx+php5fpm+wordpress)](https://linux22.com/image/623496/php5-fpm%20%E6%9C%80%E5%A4%A7%E5%8C%96%E6%89%80%E6%9C%89%E6%A0%B8%E5%BF%83%EF%BC%88nginx%2Bphp5fpm%2Bwordpress%EF%BC%89.png)
我们有一个 16 GB 的 linode,运行着 8 个 CPU 核心,我正在尝试调试一个问题,当我尝试负载测试时,服务器开始在一个非常基本的 wordpress 网站上每秒发出大约 250 个请求。我只点击了网站的首页,仅供参考。
当我登录服务器并在 Htop 上查看服务器统计信息时,我看到所有核心都已固定,并且有大量 php5-fpm 进程。测试完成后,这些进程仍然存在,我最终必须重新启动 php-fpm 才能使服务器恢复运行。
我今天做负载测试的时候在日志中看到了这个。
[23-Dec-2013 12:19:03] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 0 idle, and 27 total children
[23-Dec-2013 12:19:04] 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 35 total children
[23-Dec-2013 12:19:05] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 45 total children
[23-Dec-2013 12:19:06] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 55 total children
[23-Dec-2013 12:19:07] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 65 total children
[23-Dec-2013 12:19:08] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 75 total children
[23-Dec-2013 12:19:19] 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 93 total children
所以显然有些事情是乱七八糟的,我只是不擅长针对我们当前的系统调整这些设置。
这是我们的 www.conf
; Start a new pool named 'www'.
; the variable $pool can we used in any directive and will be replaced by the
; pool name ('www' here)
[www]
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = www-data
group = www-data
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses on a
; specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /var/run/php5-fpm.sock
; Set listen(2) backlog. A value of '-1' means unlimited.
; Default Value: 128 (-1 on FreeBSD and OpenBSD)
listen.backlog = 65536
pm = dynamic
; 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 = 150
; 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 = 20
; 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 = 10
; 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 = 35
; 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 = 500
pm.status_path = /status-www
; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
;slowlog = log/$pool.log.slow
slowlog = /var/log/php-fpm-slow.log
; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
request_slowlog_timeout = 115s
; The timeout for serving a single request after which the worker process will
; be killed. This option should be used when the 'max_execution_time' ini option
; does not stop script execution for some reason. A value of '0' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
request_terminate_timeout = 120s
; Set open file descriptor rlimit.
; Default Value: system defined value
rlimit_files = 65536
; Set max core size rlimit.
; Possible Values: 'unlimited' or an integer greater or equal to 0
; Default Value: system defined value
rlimit_core = 0
对 www.conf 有什么建议或需要指出的地方吗?
答案1
首先,您确实应该问自己一个问题:您的网站真的能够处理每秒 250 个请求吗?对于一个页面来说,这是很多请求。坦率地说,我认为,考虑到您拥有的虚拟化系统,每秒 250 个请求占用最大 CPU 是正常的。
pm.max_children = 150
您可能一直在提高这个值,因为您不断收到错误消息,说您已达到子级的最大数量。但是,当您将其提高到系统无法处理的范围之外时,您实际上是在搬起石头砸自己的脚。最好不要接受它无法处理的下一个请求,而不是接受。增加这个值不仅会使您的系统变薄,还会创建更多的资源需求和更多的开销。当它爆炸式增长到您的系统无法再处理的大小时,它会产生冻结之类的迹象。我不知道您的 VPS 到底给了您什么(您永远不会知道,因为它不是静态的,因为它取决于节点中的其他用户),但我个人认为,对于您指定的系统,任何超过 20 的值都是不现实的设置。
您还应该相应地调整启动/最小/最大备用,因为大于最大子级是没有意义的。或者您可以不担心它并设置为pm = static
。
答案2
将流程管理改为按需管理
pm = ondemand