Var/log/php-ftp.log

[13-Aug-2017 03:30:03] NOTICE: fpm is running, pid 28263
[13-Aug-2017 03:30:03] NOTICE: ready to handle connections
[13-Aug-2017 10:11:04] 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 7 total children
[13-Aug-2017 10:11:05] 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 8 total children
[13-Aug-2017 10:11:06] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 9 total children
[13-Aug-2017 10:11:07] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 10 total children
[13-Aug-2017 10:11:08] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 11 total children
[13-Aug-2017 10:11:09] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 12 total children
[13-Aug-2017 10:11:10] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 13 total children
[13-Aug-2017 10:11:11] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 14 total children
Copy the code

First, analyze and verify the problem:

1. The alarm level is “WARNING”, which means that services are not affected and the actual verification is the same.

Pm. start_servers or pm.min/max_spare_servers is recommended because the number of php-fpm processes is small

3. How much? Spawning 8, meaning at least 8 processes.

Mysql > select * from memory;

pmap $(pgrep php-fpm | head -1)
Copy the code

2. Php-fpm configuration formula

pm.start_servers = min_spare_servers + (max_spare_servers - min_spare_servers) / 2
Copy the code

3. Php-fpm optimization method:

Php-fpm has two ways. One is to directly start a specified number of php-fpm processes without increasing or decreasing them – static.

The other is to start a certain number of PHP-fpm processes at the beginning. When the number of requests becomes large, the number of phP-FPM processes is dynamically increased to the upper limit. When idle, the number of idle processes is automatically released to a lower limit – dynamic.

These two different execution modes can be adjusted according to the actual requirements of the server.

Some of the parameters to use are PM, Pm.max_children, pm.start_Servers, Pm.min_spare_Servers, and Pm.max_spare_Servers.

PM means that in that way, you can choose between two values: static or dynamic.

3.1 The meanings of the following four parameters are:

Pm. max_children: number of phP-fpm processes started statically; In dynamic mode it sets the maximum number of phP-fpm processes (note that pm.max_spare_servers must be less than or equal to pm.max_children).

Pm. start_Servers: The number of phP-fpm processes started in dynamic mode.

Pm.min_spare_servers: Minimum number of phP-FPM processes in idle state in dynamic mode.

Pm. max_spare_Servers: Maximum number of phP-fPM processes in the idle state in dynamic mode.

If dm is set to static, only pm.max_children is valid. The system will start a set number of php-fpm processes, always keeping a fixed number of child processes, defined by pm.max_children, which is not flexible and usually not the default.

If DM is set to dynamic, all four parameters take effect. The system starts the pm. start_Servers php-fpm process at the start of the phP-fpm run. The pm. start_Servers process generates a fixed number of child processes (controlled by pM. start_Servers) when started. The maximum number of child processes is controlled by pm.max_children. OK, in this case, the number of child processes varies between the maximum and minimum number. Pm.min_spare_servers and Pm.max_spare_Servers, which means that there can be a minimum and maximum number of idle child processes, and if the number of idle child processes exceeds pm.max_spare_Servers, they will be killed.

As you can see, the PM = dynamic mode is very flexible and is usually the default option. However, dynamic mode causes more memory usage in order to maximize server response, because it only kills idle processes that exceed the maximum number of idle processes (pm.max_spare_Servers), such as 30 and 50, and then the site experiences a peak of visits. At this point, all 50 processes are busy, and there are zero idle processes. Then, after the peak, there may be no requests, so there are 50 idle processes, but phP-Fpm only kills 20 child processes, leaving 30 processes as idle processes waiting for requests. Maybe that’s why even after the peak number of requests a lot of reduce server memory usage but also does not have a lot of, also may be why some time to restart the server situation will be better, because after the restart, PHP – FPM number of child processes will be the minimum number of idle process, rather than the maximum number of idle process before.

Pm. process_IDLE_timeout = pm. process_IDle_timeout = pm.process_idle_timeout = pm.process_idle_timeout = pm.process_idle_timeout = pm.process_idle_timeout If there are no requests from the server for a long time, there will be only one phP-FPM main process. The disadvantages of course are that the server will create processes frequently during peak times or if the pm.process_IDLE_TIMEOUT value is too short. So whether PM = dynamic or PM = ondemand is more appropriate depends on the situation.

So, what is the best execution for a server? In fact, just like Apache, running PHP programs have more or less memory leaks after execution. This is why a PHP-FPM process starts with around 3M of memory and grows to 25-40M after running for a while.

Therefore, dynamic mode can reclaim some memory because it terminates redundant processes, so it is recommended to use it on servers or VPS with low memory. The maximum number is based on memory /25.

For example, for 512MB VPS, it is recommended that pm.max_spare_Servers be set to 20 (512*0.8/20). For pm.min_spare_Servers, it is recommended to set the value between 5 and 10 depending on the load of the server.

However, for servers with large memory, it is more efficient to set it to static.

Since the phP-FPM process also has a time lag when it is switched on and off frequently, it is better to turn it on static with enough memory. The quantity can also be obtained according to memory /30M.

For example, a server with 2GB of memory can be set to 50. 4GB memory can be set to 100 etc.

For example, for my 1024M Cloud, the parameters are as follows:

pm = dynamic
pm.max_children = 35
pm.start_servers = 8
pm.min_spare_servers = 8
pm.max_spare_servers = 35
Copy the code

Can maximize memory savings and improve execution efficiency.

Original text: cloud.tencent.com/developer/a…