Two kinds of process management methods and optimization of PHP-FPM used by Nginx (turn)

Source: Internet
Author: User
Tags vps

PHP-FPM currently has two branches, which correspond to the php-5.2.x version and the php-5.3.x version. In the 5.2.x version, php-fpm.conf uses XML format, and in the new 5.3.x version, it is the same configuration style as php.ini.

In the 5.2.x version, php-fpm.conf has two styles for process management, one static and one similar to Apache style (apache-like).

Process Manager Settings<value Name= "PM" >sets style of controling worker process count. Valid values is ' static ' and ' Apache-like ' <value name= "style" >static</value>


As documented in the documentation, if the style of PM is Apache-like, the number of processes started should be the same as that specified by Startservers. However, after several attempts, it will be found that the PM's style is actually configured here to Apache-like without any effect. In other words, the apache-like is not implemented here.

However, in the latest 5.3.x companion php-fpm, Apache-style process management has been implemented.

; 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


The above paragraph shows that there are two styles of--static and dynamic for the management of the process. And the previous version of the process management is actually the same, just changed the apache-like to dynamic, so easier to understand.

If the number of STATIC,PHP-FPM processes is set to Pm.max_children, the number specified is no longer increased or decreased.

If set to dynamic, then the number of PHP-FPM processes is dynamically, the first is the number specified by Pm.start_servers, if 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, The corresponding cleanup is also done to ensure that no more processes than pm.max_spare_servers.

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

Here are some of the parameters that relate to this, namely pm,pm.max_children,pm.start_servers,pm.min_spare_servers and pm.max_spare_servers.

pm means that there are two values that can be selected, either static or dynamic, in that way. In older versions, dynamic is called Apache-like. This should be noted in the description of the configuration file.

The meanings of the following 4 parameters are:

Pm.max_children: The number of PHP-FPM processes that are open in static mode.
Pm.start_servers: The number of start PHP-FPM processes under dynamic mode.
Pm.min_spare_servers: The minimum number of PHP-FPM processes under dynamic mode.
Pm.max_spare_servers: The maximum number of PHP-FPM processes under dynamic mode.

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.

So, for our server, which method of execution 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 specify a 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 based on 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 1G, 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 way will end off the redundant process, can reclaim some memory, so it is recommended to use on the server or VPS with less memory. The specific maximum number is based on the 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.

Two kinds of process management methods and optimization of PHP-FPM used by Nginx (turn)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.