Reference website:
Https://www.fujieace.com/php/php-fpm.html
Https://www.fujieace.com/php/pm-max_children-2.html
How big is the Pm.max_children?
The php-fpm.conf has two key parameters:
One is "max_children" and the other is "request_terminate_timeout".
Pm.max_children represents the maximum number of child processes that PHP-FPM can start.
Request_terminate_timeout indicates that a process that takes a long time to execute is terminated directly.
The value of my two settings is "40″, one is" 900″, but this value is not generic, but it needs to be calculated by itself.
One, pm.max_children how much suitable?
This value is in principle the bigger the better, the php-cgi process is much faster, the queue requests will be very small.
Setting "Max_children" also needs to be set according to the performance of the server.
The method is calculated as follows:
In general, a server normally consumes the memory of each php-cgi in 20m~30m, so my "Max_children" I set to 40, 20m*40=800m that is, at the peak of all php-cgi consumption within 800M, Less than my valid memory is 2Gb.
And if my "max_children" set smaller, such as 5-10, then php-cgi will be "very tired", processing speed is also very slow, waiting for a long time, the CPU is also very high.
If the request has not been processed for a long time, the 504 Gateway time-out this error, while the very tired of the php-cgi are dealing with the problem will appear 502 Bad Gateway this error.
Max_children better settings according to REQ/S (throughput rate, the maximum number of requests per unit of time, Unit req/s) to set, if the program is the processing capacity of the req/s, then set 100 is better, this is dynamic to adjust.
Two
Request_terminate_timeoutHow big is it?
The method is calculated as follows:
If your server performance is good enough, and the broadband resources are sufficient, PHP scripts without loops or bugs, you can set the "request_terminate_timeout" directly to 0s. The meaning of 0s is to let php-cgi go on without time limit.
And if you can't do this, that means your php-cgi may have a bug, or your broadband is not enough or other causes your php-cgi to be able to feign death then it is recommended that you assign a value to "Request_terminate_timeout". This value can be set based on the performance of your server.
Generally, the better the performance, the higher you can set, 20 minutes-30 minutes. Because my server PHP script needs to run for a long time, some may be more than 10 minutes so I set 900 seconds, so that does not cause php-cgi dead and 502 bad gateway this error.
#################################################################################
PM = dynamic; Indicates which process quantity is used to manage the method
Dynamic indicates that the number of PHP-FPM processes is dynamically, starting with the number specified by Pm.start_servers, and automatically incremented if more requests are available, guaranteeing that the number of idle processes is not less than pm.min_spare_servers, and if the number of processes is large, The corresponding cleanup will also be carried out to ensure that the number of redundant processes is not more than pm.max_spare_servers;
Static indicates that the number of PHP-FPM processes is static, and the number of processes is pm.max_children specified, no longer increased or decreased.
Pm.max_children = 300; Number of PHP-FPM processes open in static mode
Pm.start_servers = 20; Number of start PHP-FPM processes in dynamic mode
Pm.min_spare_servers = 5; The minimum number of PHP-FPM processes under dynamic mode
Pm.max_spare_servers = 35; Maximum number of PHP-FPM processes in dynamic mode
Value setting, refer to your actual hardware configuration, you can refer to the total memory/30m to calculate.
If the DM is set to static, then only pm.max_children this parameter takes effect. The system turns on the number of PHP-FPM processes that are set.
If the DM is set to dynamic, then the Pm.max_children parameter is invalidated and the next 3 parameters take effect. The system starts the Pm.start_servers PHP-FPM process at the beginning of the PHP-FPM run and then dynamically pm.min_spare_servers and Pm.max_spare_ according to the system's requirements Adjusts the number of PHP-FPM processes between servers.
How can I tell if I choose pm = dynamic or PM = static? Which one is better?
In fact, like Apache, the running PHP program will have more or less memory leaks after the execution is completed.
This is why the beginning of a PHP-FPM process only consumes about 3M of memory, running for a period of time will rise to 20-30m reason.
For memory-heavy servers (such as 8G or more), it is actually more appropriate to use static Max_children because it does not require additional process number control to increase efficiency. Because the frequent switch PHP-FPM process will also sometimes lag, so the memory is large enough to open the static effect will be better. The amount can also be obtained according to total memory/30m , such as 8GB memory can be set to 100, then PHP-FPM memory can control the appearance of 2g-3g.
If the memory is slightly smaller, such as 1~2G, then specifying the number of static processes is more advantageous to the stability of the server. This ensures that the PHP-FPM only gets enough memory and allocates a limited amount of memory to other applications, which makes the system run more smoothly.
For small memory servers, such as 256M of memory VPs, even according to a 20M of memory to calculate, 10 php-cgi process will consume 200M of memory, the system crash should be very normal.
Therefore, the number of PHP-FPM processes should be controlled as much as possible, and when the memory used by other applications is generally clear, assigning a small amount of static to it will make the system more stable.
Or use dynamic mode, because the dynamic mode will end the redundant process, you can reclaim some memory, so it is recommended to use on a server or VPS with less memory, the maximum amount is based on total memory/20m .
For example, 512M VPS, recommended pm.max_spare_servers set to 20. As for Pm.min_spare_servers, it is recommended to set the load on the server and compare the appropriate values between 5~10.
Summary: Memory-Small recommendations with dynamic (PM = dynamic), memory-large recommendations with static (PM = static).
Configure a slow PHP log for monitoring
Request_slowlog_timeout = 10s
Slowlog = log/$pool. Log.slow
Configures the maximum number of file handles that the PHP-FPM process can open,
Rlimit_files = 1024
By default 1024, this value can not be configured
PHP-FPM consuming high CPU and memory 100% workaround