Two kinds of process management methods and optimization _nginx used by Nginx PHP-FPM

Source: Internet
Author: User
Tags configuration php fpm vps

PS: The previous period of time configuration php-fpm, inadvertently found that it has two kinds of process management methods. Like Apache, its number of processes can be divided into dynamic and static depending on the settings.

PHP-FPM currently has two main branches, corresponding to the php-5.2.x version and the php-5.3.x version respectively. In the 5.2.x version, php-fpm.conf uses an XML format, and in the new 5.3.x version, it is the same as the php.ini configuration style.
In the 5.2.x version, php-fpm.conf has two styles for process management, one static and one similar to the Apache style (apache-like).

Copy Code code 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>

As described in the documentation, if PM's style is apache-like, the number of processes to start should be the same as the startservers specified. But after several attempts, it turns out that there's actually no effect in configuring PM's style into apache-like here. In other words, the apache-like here is not being implemented.
However, in the latest 5.3.x matching PHP-FPM, Apache-style process management has been implemented.

Copy Code code as follows:

; Choose How the Process manager would 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 in 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 would 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 would be killed.
; Note:this value is mandatory.
;p m = dynamic
PM = static

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

If set to static,php-fpm the number of processes is pm.max_children the specified quantity, no longer increases or decreases.
If set to dynamic, the number of PHP-FPM processes is dynamically, starting with the number of pm.start_servers specified, and automatically increases if more requests are made to ensure that the number of idle processes is not less than pm.min_spare_servers, if the number of processes is greater, It will also be cleaned up to ensure that there are no more processes than pm.max_spare_servers.

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

Here are some of the parameters involved in this, namely, PM, Pm.max_children, Pm.start_servers, Pm.min_spare_servers and Pm.max_spare_servers.
PM indicates that in that way, there are two values that can be selected, either static (static) or dynamic. In older versions, dynamic is called Apache-like. This should be noted in the description of the configuration file.

The following 4 parameters mean:

Copy Code code as follows:

Pm.max_children: The number of PHP-FPM processes that are open in static mode.
Pm.start_servers: The number of starting PHP-FPM processes in 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 is in effect. The system will turn on the set number of PHP-FPM processes.
If the DM is set to dynamic, then the Pm.max_children parameter fails and the following 3 parameters take effect. The system starts the Pm.start_servers PHP-FPM process at the start of the PHP-FPM operation, and then dynamically pm.min_spare_servers and pm.max_spare_ according to the requirements of the system Adjusts the number of PHP-FPM processes between servers.

So, what's the best way to do this for our servers? In fact, as with Apache, the running PHP program is more or less a problem with memory leaks after the execution is complete. This is why the beginning of a PHP-FPM process consumes only about 3M of memory, running for a period of time will rise to 20-30m reasons.
for servers with large memory (such as 8G or more), it is actually more appropriate to specify static Max_children, because it does not require additional process-number control to improve efficiency. Because the frequent switch php-fpm process will sometimes lag, so the memory is large enough to open the static effect will be better. The number 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 a static number of processes is more conducive to server stability. This can ensure that php-fpm only get enough memory, will not allocate a lot of memory to other applications to use, will make the system more smooth operation.
for small memory servers, such as 256M memory VPS, even according to a 20M of memory to calculate, 10 php-cgi process will consume 200M memory, the system crash should be very normal. Therefore, you should try to control the number of PHP-FPM processes, the general clarity of other applications occupied by the memory, give it a static small number, will make the system more stable. Or use dynamic mode, because the dynamic way will end off redundant processes, you can reclaim some memory, so the recommended in less memory servers or VPS. The specific maximum quantity is obtained according to 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 server's load, the appropriate value between 5~10.

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.