在优化PHP的进程数的时候咱们首先要了解咱们服务器执行一个php使用的内存php
1: 查询一个php占用的内存方法前端
pmap $(pgrep php-fpm | head -1)后端
我这里查询到的是服务器
00002ba5d0bec000 4K rw-s- /dev/zero (deleted)并发
00002ba5d0bed000 4K rw-s- /dev/zero (deleted)php-fpm
00007fffc568b000 80K rwx-- [ stack ]优化
00007fffc569f000 4K rw--- [ anon ]spa
00007fffc57fd000 12K r-x-- [ anon ]代理
ffffffffff600000 8192K ----- [ anon ]code
total 309548K
大概30MB的样子
我自己机器内存是32G,那么理论上计算最大能承受php的并发是 32G/30MB=1092个
那么若是除去系统跟其余软件的使用内存大概算900个
2 :php-fpm的配置公式:pm.start_servers = min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.max_children的计算方法,本机内存/30兆每一个为最大的限定值再适当减少
相关参数解读:
pm.max_children:静态方式下开启的php-fpm进程数量。
pm.start_servers:动态方式下的起始php-fpm进程数量。
pm.min_spare_servers:动态方式下的最小php-fpm进程数量。
pm.max_spare_servers:动态方式下的最大php-fpm进程数量。
针对个人服务器状况调整这几个值为(固然我这边是集群因此我启动300个已经足够了):
pm.max_children = 300
pm.start_servers = 155
pm.min_spare_servers = 10
pm.max_spare_servers = 300
###################################################
(备注:若是不按这个公式计算作配置多多少少会出现写问题,好比
seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 34 total children (这个很明显是要你调节进程数)
child 1616 exited with code 0 after 619688.718148 seconds from start (一下三个好像都是提示执行超时)
child 14636 exited on signal 15 (SIGTERM) after 269249.100189 seconds from star
request: "POST /test.php") execution timed out (198.776247 sec), terminating
等等相似的报错
若是是ngixn作的前端代理会出现当后端服务器出现相似问题就会链接失败提示没在线的主机等等状况
no live upstreams while connecting to upstream, client
)
####################################################
转自
PHP fpm优化 - 信不信由你 - 51CTO技术博客http://312461613.blog.51cto.com/965442/1559672/