Home > Article > Backend Development > The php-fpm process management method and optimization used by Nginx
PS: When configuring php-fpm some time ago, I accidentally discovered that it has two process management methods. Similar to Apache, its number of processes can also be divided into dynamic and static according to settings.
php-fpm currently has two main branches, corresponding to the version of php-5.2.x and the version of php-5.3.x. In the 5.2.x version, php-fpm.conf uses the xml format, but in the new 5.3.x version, it has the same configuration style as php.ini.
In the 5.2.x version, php-fpm.conf
claims to have two styles for process management, one is static (static), and the other is similar to apache style (apache -like).
Related learning recommendations: php programming (video)
The code is as follows:
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>
Follow According to the document, if the pm style adopts apache-like
, the number of started processes should be the same as specified by StartServers. However, after several attempts, you will find that actually configuring PM's style to apache-like has no effect. In other words, apache-like here has not been implemented.
However, in the latest 5.3.x supporting php-fpm, apache-style process management has been implemented.
The code is as follows:
; 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
As can be seen from the above paragraph, there are two styles of process management-static and dynamic. The process management is actually the same as the previous version, except that apache-like is changed to dynamic, which is easier to understand.
If set to static, the number of php-fpm processes will always be the number specified by pm.max_children and will not increase or decrease.
If set to dynamic, the number of php-fpm processes is dynamic. It starts with the number specified by pm.start_servers. If there are more requests, it will automatically increase to ensure that the number of idle processes is not less than pm. min_spare_servers
, if the number of processes is large, it will be cleaned accordingly to ensure that the number of redundant processes is no more than pm.max_spare_servers.
These two different process management methods can be adjusted according to the actual needs of the server.
Let’s first talk about several parameters involved in this. They are pm
, pm.max_children
, pm.start_servers
, pm.min_spare_servers
and pm.max_spare_servers
.
pm indicates which method to use. There are two values to choose from, namely static (static) or dynamic (dynamic). In older versions, dynamic was called apache-like
. Please pay attention to the description of the configuration file for this.
The meanings of the following four parameters are:
The code is as follows:
pm.max_children:静态方式下开启的php-fpm进程数量。 pm.start_servers:动态方式下的起始php-fpm进程数量。 pm.min_spare_servers:动态方式下的最小php-fpm进程数量。 pm.max_spare_servers:动态方式下的最大php-fpm进程数量。
If dm is set to static, then there is actually onlypm.max_children
This parameter takes effect. The system will start a set number of php-fpm processes.
If dm is set to dynamic, then the pm.max_children parameter will be invalid and the next three 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.
So, which execution method is better for our server? In fact, like Apache, the running PHP program will more or less have memory leaks after execution. This is also the reason why a php-fpm process only occupies about 3M of memory at the beginning, and it will increase to 20-30M after running for a period of time.
For servers with large memory (such as 8G or more), it is actually more appropriate to specify static max_children, because this does not require additional process number control and will improve efficiency. Because frequent switching of the php-fpm process will cause lag, so if the memory is large enough, the static effect will be better.
The quantity can also be obtained based on memory/30M. For example, 8GB memory can be set to 100, then the memory consumed by php-fpm can be controlled at 2G-3G. If the memory is slightly smaller, such as 1G, then specifying a static number of processes is more conducive to the stability of the server. This can ensure that php-fpm only obtains enough memory, and allocates a small amount of memory to other applications for use, which will make the system run more smoothly.
For a server with small memory, such as a VPS with 256M memory, even if it is calculated based on a 20M memory, 10 php-cgi processes will consume 200M of memory, and the system crash should be very serious. Normal. Therefore, you should try to control the number of php-fpm processes as much as possible. After roughly clarifying the memory occupied by other applications, assigning it a static small number will make the system more stable.
Or use the dynamic method, because the dynamic method will end redundant processes and can recycle and release some memory, so it is recommended to use it on servers or VPS with less memory. The specific maximum amount is obtained based on memory/20M. For example, for a 512M VPS, it is recommended to set pm.max_spare_servers to 20. As for pm.min_spare_servers
, it is recommended to set it according to the load of the server. The more appropriate value is between 5 and 10.
Related recommendations: Programming video course
The above is the detailed content of The php-fpm process management method and optimization used by Nginx. For more information, please follow other related articles on the PHP Chinese website!