The solution to the high CPU usage of the linux php-fpm process

In recent development, I found that opening webpages is getting slower and slower, so I use the top command to find that the php-fpm CPU has soared to more than 90%, so this problem needs to be dealt with urgently. The
The reason and solution of PHP-FPM process CPU soaring
main solutions are as follows:

1. Set to control the number of processes in the php-fpm process pool.

修改pm.max_children的数量,根据内存来进行分配,系统开一个进程20-30M。比如系统内存1G,那就将差不多能开30个进程,所以可以设置pm.max_children 为30,以此类推,然后需要重启下php-fpm服务。

pm.max_children: The number of php-fpm processes opened in static mode.
pm.start_servers: The number of starting php-fpm processes in dynamic mode.
pm.min_spare_servers: The minimum number of php-fpm processes in dynamic mode.
pm.max_spare_servers: The maximum number of php-fpm processes in dynamic mode.

2. Turn on the slow log.

编辑php-fpm.conf文件找到request_slowlog_timeout = 0这一行,默认值为0,表示不开启slowlog,将其值改为3s,表示跟踪执行时间达到或超过3s的脚本。找到slowlog,它的值表示慢执行日志的路径。

3. Memory allocation.

内存分配太少,理论上开一个进程消耗CPU 20-30M,所以1G内存的可以开30个进程左右,如果是虚拟机的话可以分配2G内存。

4. Edit the php-fpm.conf configuration file.

php_admin_value[memory_limit] = 128M(我服务器上的配置文件在/etc/php5/fpm/pool.d/www.conf 这个文件是被包含在php-fpm.conf里的) 后边的数字可以随便更改:32M,64M,128M,256M,512M,这个设置可根据你的服务器内存大小和你的需求来写,修改后要加载一下php-fpm服务。

 

 

Guess you like

Origin blog.csdn.net/whatday/article/details/114601984