网站首页其他教程文章正文  

php-fpm: pool www 进程很多,cpu 和 内存占用很大,怎么办?

     

今天,我发现网站的加载时间变长了,然后就连上 ssh 看了下 vps ,好家伙,cpu 直接 90% 了,然后又用 ps aux 查看 cpu 和内存占用情况,发现 php-fpm: pool www 进程很多

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
www        843  3.1  1.2 171972 24948 ?        S    Apr18   0:35 php-fpm: pool www
www        844  4.6  1.4 176356 28900 ?        S    Apr18   0:31 php-fpm: pool www
www        845  3.3  1.6 178880 32044 ?        S    Apr18   0:30 php-fpm: pool www
www        846  5.1  1.5 177060 30288 ?        S    Apr18   0:34 php-fpm: pool www
www        852  2.9  1.1 171800 22812 ?        S    Apr18   0:59 php-fpm: pool www
www        853  4.1  1.6 179840 31956 ?        S    Apr18   0:31 php-fpm: pool www
www        854  3.8  1.2 170132 24008 ?        S    Apr18   0:33 php-fpm: pool www
www        855  4.0  1.2 170752 24052 ?        S    Apr18   0:30 php-fpm: pool www
www        856  1.9  1.5 177728 30256 ?        S    Apr18   0:30 php-fpm: pool www
www        857  5.1  1.5 177100 31260 ?        S    Apr18   0:32 php-fpm: pool www
www        897  3.7  1.1 171912 23484 ?        S    Apr18   0:32 php-fpm: pool www
www        973  4.3  1.5 176784 29936 ?        S    Apr18   0:30 php-fpm: pool www
www        991  3.1  1.5 178832 31740 ?        S    Apr18   0:30 php-fpm: pool www
www        994  2.3  1.7 179808 34620 ?        S    Apr18   0:34 php-fpm: pool www
www       6751  5.5  1.2 171840 24124 ?        S    May02   0:11 php-fpm: pool www
www       6752  4.7  1.4 174180 29452 ?        S    May02   0:11 php-fpm: pool www
www       6753  4.2  1.4 174128 29488 ?        S    May02   0:12 php-fpm: pool www
www       6754  4.3  1.5 176176 31680 ?        S    May02   0:12 php-fpm: pool www
www       6755  3.1  1.1 167976 23540 ?        S    May02   0:11 php-fpm: pool www
www      10316  4.0  1.4 174112 29668 ?        S    May07   0:09 php-fpm: pool www

从上面可以看出,虽然每个 php-fpm: pool www 进程占用的 cpu 和内存不大,但是总共有 20 个啊,加起来 cpu 和 内存占用就很大了,难怪网站加载变慢了

解决方法
# 编辑 php-fpm.conf

vi /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 = 20
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.max_requests = 1024
pm.process_idle_timeout = 10s
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/slow.log

修改后

[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 = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
pm.max_requests = 1024
pm.process_idle_timeout = 10s
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/slow.log

对比观察,发现主要是修改以下几项的数值

pm.max_children = 10 php-fpm: pool www 最大进程数
pm.start_servers = 2 
pm.min_spare_servers = 1 
pm.max_spare_servers = 6

修改好后,执行以下命令重启 php-fpm

# 重启 php-fpm

systemctl restart php-fpm

或者在 crontab 中添加定时重启 php-fpm 的任务

# 查看 crontab 定时任务

crontab -l

# 在 crontab 中添加定时重启 php-fpm 任务

crontab -e

# 每天凌晨 1 点重启 php-fpm 服务

0 1 * * * systemctl restart php-fpm

# 添加后,保存并退出

:wq

至此,成功的解决了 php-fpm: pool www 进程过多,cpu 和 内存占用很大的问题

     
购买 Google Voice 靓号 / GV 靓号

2 评论

  1. 这个修改是个双刃剑,当你网站访问流量变大的时候,他自己会调整很多进程以解决更高并发的问题,你强行改小,确实不占用内存,但是更多用户访问的时候,就会变成无法访问。

    1. 确实,我也发现这问题了,所以我用了定时重启,谢谢提醒

发表评论