PHP相关性能配置

使用php-fpm时的默认配置一般都比较低,需要修改,常见的问题可以从日志中获取,如果你发现服务器挂了,查看下日志一般都会找到问题。

pm = dynamic

这个参数默认为dynamic,它有3种:static, dynamic, ondemand

1、这个可能是你遇到的第一个问题

tail -f /var/log/php5-fpm.log

[25-Jan-2019 04:43:41] WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
[25-Jan-2019 05:10:18] WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
[25-Jan-2019 06:41:22] WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
[25-Jan-2019 06:48:03] WARNING: [pool www] server reached pm.max_children setting (5), consider raising it

产生上面问题需要将默认值5修改为更多,假设每个进程会占用20多兆内存(自己实际查下,不同的应用占用应该不同),根据机器可用内存算下,虚拟机上跑的我这里用了50(如果日志中还是出现,那就继续增加直到服务器最大并发负荷,还不行那就得想其它解决方案了);dyanmic模式下pm.max_children限制的是最大alive状态的数量;如果是static模式,将总是保持指定数量的进程;

pm.max_children = 50

通过如下命令修改

sed -i 's/pm.max_children = [0-9]*/pm.max_children = 50/g' /etc/php5/fpm/pool.d/www.conf

重启生效

/etc/init.d/php5-fpm restart

2、你应该还会还遇到下面问题

[25-Jan-2019 08:29:47] 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 6 total children
[25-Jan-2019 08:29:48] 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 7 total children

那么你需要配置一下几个参数

pm.max_children = 50 # dynamic模式时,限制alive状态children
pm.start_servers = 15 # dynamic模式起作用,初始准备的进程数量
pm.min_spare_servers = 10 # dynamic模式起作用,如果空闲的进程小于这个值将会创建新的进程
pm.max_spare_servers = 40 # dynamic模式起作用,如果空闲的进程大于这个值将开始kill;

上命令行

sed -i 's/pm.max_children = [0-9]*/pm.max_children = 50/g' /etc/php5/fpm/pool.d/www.conf
sed -i 's/pm.start_servers = [0-9]*/pm.start_servers = 15/g' /etc/php5/fpm/pool.d/www.conf
sed -i 's/pm.min_spare_servers = [0-9]*/pm.min_spare_servers = 10/g' /etc/php5/fpm/pool.d/www.conf
sed -i 's/pm.max_spare_servers = [0-9]*/pm.max_spare_servers = 40/g' /etc/php5/fpm/pool.d/www.conf

重启生效

/etc/init.d/php5-fpm restart

3、这个也得说说

pm.max_requests = 500

这个配置与进程回收有关,防止内存泄漏,当每个进程被请求指定次数后关闭这个进程,用新的进程来处理请求。

sed -i -e '/;pm.max_requests/s/^;//' -e 's/pm.max_requests = [0-9]*/pm.max_requests = 500/g' /etc/php5/fpm/pool.d/www.conf | grep max_requests

重启生效

/etc/init.d/php5-fpm restart

相关内容

· PHP

[ 编辑 | 历史 ]
最近由“jilili”在“2019-01-25 12:01:30”修改