PHP-FPM 进程每隔几天就会崩溃,原因是已达到 pm.max_children,我必须手动启动它才能使网站正常运行。
服务器配置:
Total Memory: 7986M
CPU: 2 Core
1 PHP Process Taking: 22M
PHP-FPM配置:
pm = dynamic
pm.max_children = 280
pm.start_servers = 15
pm.min_spare_servers = 15
pm.max_spare_servers = 35
pm.max_requests = 1000
检查PHP-FPM日志,发现以下错误。
[02-Aug-2017 23:23:54] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 10 idle, and 41 total children
[02-Aug-2017 23:24:04] 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 55 total children
[02-Aug-2017 23:24:05] 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 63 total children
[02-Aug-2017 23:24: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 78 total children
[02-Aug-2017 23:24: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 93 total children
[02-Aug-2017 23:24: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 108 total children
[02-Aug-2017 23:24:09] 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 123 total children
[02-Aug-2017 23:24:10] 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 138 total children
[02-Aug-2017 23:24:11] 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 153 total children
[02-Aug-2017 23:24:12] 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 168 total children
[02-Aug-2017 23:24:13] 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 183 total children
[02-Aug-2017 23:24:14] 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 198 total children
[02-Aug-2017 23:24:15] 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 213 total children
[02-Aug-2017 23:24:16] 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 228 total children
[02-Aug-2017 23:24:17] 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 243 total children
[02-Aug-2017 23:24:18] 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 258 total children
[02-Aug-2017 23:24:19] 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 273 total children
[02-Aug-2017 23:24:20] WARNING: [pool www] server reached pm.max_children setting (280), consider raising it
我可以请某人建议 PHP-FPM 的最佳优化配置,这有助于避免崩溃。
下面这一行是什么意思:
[02-Aug-2017 23:23:54] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 10 idle, and 41 total children
答案1
以下是计算正确数字的一个建议:
https://myshell.co.uk/blog/2012/07/adjusting-child-processes-for-php-fpm-nginx/
以下命令将帮助我们确定每个(PHP-FPM)子进程使用的内存:
ps -ylC php-fpm --sort:rss
然后获取服务器的可用 RAM 并计算:
pm.max_children = Total RAM dedicated to the web server / Max child process size
另请参阅:
答案2
首先,您应该找出您真正需要多少个 fpm 子进程。在您的池配置中启用慢速日志或使用 NewRelic 等外部服务来查找代码中的慢速和薄弱环节。可能是您在 MySQL 连接或 PHP-curl 中遇到了瓶颈。您收到“似乎很忙”的消息,因为它们正忙于等待来自数据库或其他东西的答复。无缘无故地增加 pm.max_children 是一种不好的做法。