Home >Backend Development >PHP Tutorial >Solve the problem of excessive server memory resource consumption caused by too many php-fpm processes in CentOS7

Solve the problem of excessive server memory resource consumption caused by too many php-fpm processes in CentOS7

藏色散人
藏色散人forward
2020-01-13 14:03:323194browse

Foreword:

The server memory usage has been high recently. After checking, it was found that it may be related to the excessive number of php-fpm processes. This article records my process of optimizing the php-fpm configuration file and adds some knowledge related to php-fpm.

What is php-fpm:

php-fpm is the FastCGI process manager, used to control php's memory and processes.

Operating environment:

CentOS 7

Problem check:

First check the total number of php processes:

pstree|grep php-fpm

Echo:

|-php-fpm---20*[php-fpm]

According to the echo information, there are 20 php-fpm processes during the query.

You can also list the top 50 processes that consume the most memory through the following command:

ps auxw|head -1;ps auxw|sort -rn -k4|head -50

Solution process:

Let’s modify the configuration below file to optimize php-fpm to reduce memory usage.

My php-fpm configuration file is in the .../server/php/etc/ directory, enter this directory.

Back up the original configuration file first:

cp php-fpm.conf php-fpm.conf.bak

Note:

After using vim to open the configuration file, press the Esc key, and then enter "/" , then enter "xxx" to search for the string "xxx".

The specific configuration parameters are as follows:

pm.max_children = 100改为:pm.max_children = 25
pm.start_servers = 20改为pm.start_servers = 5
pm.min_spare_servers = 5改为pm.min_spare_servers = 2
pm.max_spare_servers = 35改为pm.max_spare_servers = 10

Restart the server:

reboot

After restarting, you can find that the memory usage is lower than before.

Note:

1. Description of the parameters in php-fpm.conf:

pm.max_children:静态方式下开启的php-fpm进程数量。
pm.start_servers:动态方式下的起始php-fpm进程数量。
pm.min_spare_servers:动态方式下的最小php-fpm进程数。
pm.max_spare_servers:动态方式下的最大php-fpm进程数量。

2. When making the above settings, I will php -The execution mode of -fpm is set to dynamic:

pm = dynamic

3.php-fpm has two execution modes:

One is Static and the other is Dynamic. If set to static, only the pm.max_children parameter will take effect. If set to dynamic, the three parameters pm.start_servers, pm.min_spare_servers and pm.max_spare_servers will take effect. After setting it to dynamic, when php-fpm starts, it will start the corresponding number of processes according to the parameter setting of pm.start_servers. After that, the number of processes of php-fpm will be maintained between the number specified by pm.min_spare_servers and pm.max_spare_servers.

4. How to choose to use the static or dynamic execution mode of php-fpm:

The dynamic php-fpm execution mode allows php-fpm to release redundant processes, thus saving memory resources.

The static php-fpm execution method does not allow php-fpm to release more than processes, which avoids frequently starting or stopping the php-fpm process, thereby reducing the server's response time in some cases.

For more PHP related knowledge, please visit PHP Tutorial!

The above is the detailed content of Solve the problem of excessive server memory resource consumption caused by too many php-fpm processes in CentOS7. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete