When installing php, those who are careful can find that we also installed the php-fpm module. The fpm module is responsible for processing the request process. The related operations of fpm will directly affect how many requests the server can respond to. Proper settings will effectively improve processing efficiency, otherwise 502 gateway timeout or 504 error will occur.
Let's open the www.conf file under /etc/php/7.2/fpm/pool.d and find the following parameters:
pm=dynamicpm.max_children=5pm.start_servers=2pm.min_spare_servers=1pm.max_spare_servers=3;pm.max_requests=500
These are the default parameters after the author installed PHP. I will explain them one by one below:
The first dynamic indicates that the processing mode of the process is dynamic, and there is also a static mode corresponding to it.
If pm is set to staic, then only the parameter pm.max_childern takes effect.
If pm is set to dynamic, the next four parameters will take effect. The system will start pm.start_servers php-fpm processes when php-fpm starts running, and then dynamically adjust the number of php-fpm processes between pm.min_spare_servers and pm.max_spare_servers according to the needs of the system.
The second parameter pm.max_children indicates the maximum number of processes that can be forked (started). Obviously, the larger the amount, the more requests will be able to be made at the same time. Naturally, there will be no slow connections or long request waiting times, but the CPU or memory will still be available. The occupancy is not high. This value refers to the number of opened php-fpm processes in static mode, and limits the maximum number of php-fpm processes in dynamic mode.
The third parameter pm.start_servers indicates that if it is dynamic mode, several processes will be started in the initial state, and then dynamically adjusted as needed, and is determined by the two parameters pm.min_spare_servers and pm.max_spare_servers.
The fourth parameter pm.min_spare_servers represents the minimum number of php-fpm processes in idle state in dynamic mode, and the number retained after cleaning up idle processes
The fifth parameter pm.max_spare_servers represents the maximum number of php-fpm processes in the idle state in dynamic mode, that is, when the idle processes reach this value, they are cleaned up.
The sixth parameter pm.max_requests indicates the number of requests before destroying the process and restarting it, which can avoid memory leaks.
Obviously, max_children here is 5, which is enough at the beginning. As the traffic increases, it will definitely not be enough. Let’s talk about the specific value settings. The most direct impact is the pm.max_children value. The most basic principle is: Memory CPU system If the system resources allow, the bigger the better. Here is a calculation method. For example, an fpm process is about 15~25M. Then when the server has 4G memory, only under ideal conditions, it can be about 150~300 at full capacity. Then observe the server status and make adjustments
Generally speaking, large memory servers (such as 32 and 64G and above) can consider static mode, which has good concurrency effects. Small memory servers can consider dynamic mode, which can be automatically controlled on demand.
You can stress test according to your own situation and make adjustments according to your own server conditions.