服务器CPU满载问题

前因

先说明一下,原先服务器是阿里云1核心2G内存的服务器,开了Redis、ES、MySQL、PHP、Nginx等等服务、外加其他一些后台常驻进程后,资源用的基本上用的是满满当当。但是php-fpm时常会报如下的错误。

[28-Oct-2016 14:25:52] 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 17 total children
[28-Oct-2016 14:25:53] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 7 idle, and 22 total children
[28-Oct-2016 14:25:54] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 9 idle, and 25 total children
[28-Oct-2016 14:25:59] WARNING: [pool www] server reached pm.max_children setting (30), consider raising it

异想天开之路

很明显嘛,系统跟你说php-fpm进程太少了,nginx转发过来的请求太多了,让你给多开几个进程,配置改起来也方便,就下面几个。

pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 20
pm.max_spare_servers = 30

然后就恶梦就逐渐开始了,刚开始还运行的好好的,后面就开始连锁反应了。

  • 内存直接爆了
  • 系统开始杀进程,内存不足还干毛活啊
  • 导致大量php-fpm进程无法响应,nginx返回给用户502错误
  • 进程被系统杀了,php-fpm又开始不断的fork新进程出来
  • 接着CPU就直接满载了
  • 然后最最可怕的东西来了,SSH完全连不上了

SSH一连接不上,那就是想修修不了啊,赶紧重启服务器,升级内存吧。当然升级内存只是其中一种方式,原先本来想把全站数据用nginx静态化掉,这样最省资源但是有个很大的问题就是数据无法即时更新。

结论

服务器的配置在不能确定会有何影响的情况下不要乱改,不然够你喝一壶的。