Nginx USES php fpm for two process management methods and optimization

  • 2020-05-12 06:51:07
  • OfStack

PS: while configuring php-fpm some time ago, I came across two different ways of managing processes. Like Apache, the number of processes can be divided into dynamic and static by setting.

php-fpm currently has two main branches, corresponding to the version of php-5.2.x and the version of php-5.3.x, respectively. In version 5.2.x, php-fpm.conf USES the xml format, while in the new 5.3.x version, the configuration style is the same as php.ini1.
In version 5.2.x, there are two styles for process management claimed in php-fpm.conf, one is static (static) and one is similar to the apache style (apache-like).


Process manager settings
<value name= " pm " >
Sets style of controling worker process count.
Valid values are 'static' and ' apache-like'
<value name= " style " >static</value>

According to the documentation, if pm's style is apache-like, the number of processes started should be the same as that specified by StartServers. However, after several attempts, it turns out that configuring style of pm to apache-like doesn't actually work here. In other words, apache-like is not implemented here.
However, apache style process management has been implemented in the latest 5.3.x companion php-fpm.


; Choose how the process manager will control the number of child processes.
; Possible Values:
; static - a fixed number (pm.max_children) of child processes;
; dynamic - the number of child processes are set dynamically based on the
; following directives:
; pm.max_children - the maximum number of children that can
; be alive at the same time.
; pm.start_servers - the number of children created on startup.
; pm.min_spare_servers - the minimum number of children in 'idle'
; state (waiting to process). If the number
; of 'idle' processes is less than this
; number then some children will be created.
; pm.max_spare_servers - the maximum number of children in 'idle'
; state (waiting to process). If the number
; of 'idle' processes is greater than this
; number then some children will be killed.
; Note: This value is mandatory.
;pm = dynamic
pm = static

According to the passage above, there are two styles of process management -- static and dynamic. The process management is the same as the previous version, only apache-like is changed to dynamic, which is easier to understand.

If set to static, the number of php-fpm processes is always the number specified by pm.max_children, and no longer increases or decreases.
If set to dynamic, the number of php-fpm processes is dynamic, starting with the number specified by pm.start_servers. If there are more requests, the number will be automatically increased to ensure that the number of idle processes is not less than pm.min_spare_servers.

These two different process management methods can be adjusted according to the actual requirements of the server.

So let's start with 1 and then the parameters involved are pm, pm.max_children, pm.start_servers, pm.min_spare_servers and pm.max_spare_servers.
pm means that there are two values to choose from in that way, static (static) or dynamic (dynamic). In the older version, dynamic is called apache-like. Note the configuration file description.

The meaning of the following four parameters are:


pm.max_children : opened in static mode php-fpm Number of processes.
pm.start_servers : start in dynamic mode php-fpm Number of processes.
pm.min_spare_servers : minimum in dynamic mode php-fpm Number of processes.
pm.max_spare_servers : maximum in dynamic mode php-fpm Number of processes.

If dm is set to static, then only the parameter pm.max_children actually takes effect. The system starts the php-fpm process with a set number.
If dm is set to dynamic, then the pm.max_children parameter is invalid and the next three parameters take effect. The system will start pm.start_servers php_fpm processes at the beginning of php-fpm operation, and then dynamically adjust the number of php-fpm processes between pm.min_spare_servers and pm.max_spare_servers processes according to the requirements of the system.

So, which execution is better for our server? In fact, like Apache1, the running PHP program has more or less memory leaks after execution. This is why a single php-fpm process only takes up around 3M memory at the beginning, and then rises to 20-30M after 1 run.
For servers with large memory (such as 8G and above), it is actually better to specify static max_children, as this does not require additional process control and is more efficient. Since switching on and off php-fpm processes frequently also has a time lag, it is better to turn them on statically if memory is large enough. The amount can also be based on memory /30M. For example, 8GB can be set to 100, so the amount of memory consumed by php-fpm can be controlled to 2G-3G. If memory is slightly smaller, say 1G, then specifying the number of static processes is more conducive to server stability. This can ensure that php-fpm only gets enough memory and allocates a small amount of memory to other applications, which will make the system run more smoothly.
For servers with small amounts of memory, such as 256M for VPS, 10 php-cgi processes will consume 200M of memory even at the amount of memory of 1 20M, and the system should crash normally. Therefore, you should try to control the number of php-fpm processes as much as possible, and specify a small static number for the php-fpm process to make the system more stable. Or use the dynamic mode, because the dynamic mode will end the redundant process and can reclaim some memory, so it is recommended to use it on a server with less memory or on VPS. The maximum number is based on memory /20M. For example, VPS for 512M, pm.max_spare_servers is recommended to be set to 20. As for pm.min_spare_servers, it is recommended to set it according to the load of the server, and the appropriate value is between 5 and 10.


Related articles: