折腾:
期间,想要搞清楚:
php-fpm.conf中的pm.max_children到底应该设置为多少
因为:
“错误三:资源耗尽
LNMP 架构处理 php 时,是 nginx 直接调取后端的 php-fpm 服务,如果 nginx 的请求量偏高,而我们又没有给 php-fpm 配置足够的子进程,那么总有 php-fpm 资源耗尽的时候,一旦耗尽 nginx 则找不到 php-fpm,此时就会导致 502 出现。那这时候的解决方案就是去调整php-fpm.conf 中的pm.max_children 数值,使其增加。但也不能无限设置,毕竟服务器的资源有限,根据经验,4G 内存机器如果只跑 php-fpm 和 nginx,不跑 mysql 服务,pm.max_children可以设置为 150,尽量不要超过该数值, 8G 内存可用设置为 300,以此类推。”
另外:
之前刚安装lnmp默认设置,现在(参考之前的配置去改的)
pm.max_children = 20
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 20
的2倍。
pm.max_children what value
pm.max_children proper value[root@crifan ~]# ps -ylC php-fpm –sort:rss
S UID PID PPID C PRI NI RSS SZ WCHAN TTY TIME CMD
S 0 31646 1 0 80 0 11760 101475 – ? 00:00:09 php-fpm
S 1001 13681 31646 0 80 0 60920 105621 syscal ? 00:00:03 php-fpm
S 1001 13555 31646 0 80 0 61316 105495 syscal ? 00:00:05 php-fpm
S 1001 13306 31646 0 80 0 62388 105741 syscal ? 00:00:19 php-fpm
S 1001 12916 31646 0 80 0 62408 105566 syscal ? 00:01:02 php-fpm
S 1001 13227 31646 0 80 0 62748 105677 syscal ? 00:00:26 php-fpm
S 1001 13122 31646 0 80 0 63252 105746 syscal ? 00:00:36 php-fpm
S 1001 12007 31646 0 80 0 66336 126830 syscal ? 00:01:38 php-fpm
S 1001 10875 31646 0 80 0 66464 108546 syscal ? 00:02:11 php-fpm
S 1001 13110 31646 0 80 0 67796 128468 syscal ? 00:00:37 php-fpm
S 1001 12830 31646 0 80 0 69220 128804 syscal ? 00:01:21 php-fpm
S 1001 10832 31646 0 80 0 69328 128344 syscal ? 00:02:34 php-fpm
S 1001 9785 31646 0 80 0 70120 130583 syscal ? 00:02:34 php-fpm
S 1001 11813 31646 0 80 0 72468 129049 syscal ? 00:01:52 php-fpm
S 1001 10960 31646 0 80 0 72496 129130 syscal ? 00:01:58 php-fpm
S 1001 9672 31646 0 80 0 74328 129612 syscal ? 00:02:37 php-fpm
S 1001 5000 31646 0 80 0 75256 129777 syscal ? 00:04:51 php-fpm
S 1001 7339 31646 0 80 0 75404 129953 syscal ? 00:04:05 php-fpm
S 1001 1209 31646 0 80 0 79000 129899 syscal ? 00:06:46 php-fpm
S 1001 1811 31646 0 80 0 99580 134813 syscal ? 00:06:05 php-fpm
S 1001 25494 31646 0 80 0 107948 137853 syscal ? 00:14:21 php-fpm
The RSS column shows non-swapped physical memory usage by PHP-FPM processes in kilo Bytes.
RSS列显示的就是,PHP-FPM的进程的非swap的物理内存占用量,单位KB
-》上面可以看出,平均内存占用量只有8M左右。
-》怎么感觉更像是SZ那列呢?
-》之前优化后的单个页面加载的内存占用,也就是10多M,比较像是SZ列?pm.max_children = Total RAM dedicated to the web server / Max child process size – in my case it was 85MB
I left some memory for the system to breath. You need to take into account any other services running on the machine while calculating memory usage.
Please note that very high values does not mean necessarily anything good.
此处:
pm.max_children
= 3G(一共4G,算上只有3G给web server用吧)/15M(就算最大占用15MB吧,也不管是RSS还是SZ列了)
=3096/15
=206.4
=200
结果用另外的命令去看是:[root@crifan ~]# ps –no-headers -o "rss,cmd" -C php-fpm | awk ‘{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }’
67M
结果发现上面RSS列看错了。
实际上是:
最大的内存占用是:
107948KB=105MB
所以上面应该改为:
pm.max_children
=3096/105
=29
【总结】
算了,最后去改为:
介于之前和开始的中间值吧:[root@crifan ~]# cat /usr/local/php/etc/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 = 30
pm.start_servers = 15
pm.min_spare_servers = 15
pm.max_spare_servers = 30
#request_terminate_timeout = 100
request_terminate_timeout = 600
#request_slowlog_timeout = 0
request_slowlog_timeout = 5
slowlog = var/log/slow.log
【后记】
后来是:
改为了:pm = dynamic
pm.max_children = 10
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
待后续确认效果如何。
【后记 2017-10-30】
后来运行了段时间,发现也没啥问题,CPU和内存占用率都还不算高:
所以算是真的解决了问题,并且,不需要php-fpm.conf中的pm.max_children设置很高。