Optimizing the process of PHP-FPM processes under Linux

Source: Internet
Author: User
Tags fpm vps

Background

Recently moved WordPress to Aliyun. Because its own server is a cloud server, hard disk and memory are relatively small, so the memory is often not enough, through the Linux command view, found that the number of boot PHP-FPM process has more than 20, occupy nearly 1G of memory, The entire server is only 1.5G of memory, and finally through the PHP-FPM process number optimization To solve this problem, the server to save more 600M of memory, the PHP-FPM optimization method and everyone to share under.

Note: At present, according to Nginx, fpm-php for memory optimization, see the relevant information

PHP-FPM optimization

1, php-fpm optimization parameter introduction

They were: PM, Pm.max_children, Pm.start_servers, Pm.min_spare_servers, Pm.max_spare_servers.

PM: 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:

The code is as follows Copy Code

The number of child processes created in pm.max_children:static mode or the maximum number of php-fpm that are allowed at the same time in dynamic mode.
The maximum number of requests that the PM.MAX_REQUESTS:PHP-FPM process can handle.
Pm.start_servers: The number of starting PHP-FPM processes in dynamic mode.
Pm.min_spare_servers: The minimum number of php-fpm processes in a dynamic way when the server is idle.
Pm.max_spare_servers: The maximum number of php-fpm processes in a dynamic way when the server is idle.


As you can see from the description of several parameters in the configuration file, the following three parameters only work with the dynamic PM. It is generally thought that the performance of the static server is better, otherwise the frequent creation of the recycle process is also a drain on server resources.

Difference:

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 run.

Then the number of php-fpm processes between Pm.min_spare_servers and Pm.max_spare_servers is dynamically adjusted according to the requirements of the system

2, the server specific configuration
For our server, which implementation is better to choose? 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 a static Max_children, since 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 obtained according to the memory/30m, such as 8GB memory can be set to 100,
Then the memory that PHP-FPM consumes 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 using dynamic methods,
Because the dynamic way will end the redundant process, can reclaim some memory, so it is recommended to use 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 that the server's load situation to set, such as the server is just the deployment of PHP environment, the more appropriate value between 5~10.

This server configuration
1, server basic information:
Hard disk: Data disk 30G, system disk 20G
Memory: 1.5G
CPU: Dual-core
System: CentOS 6.3 64-bit
Bandwidth: Exclusive 2M

2, deployment of the application
Git, SVN, Apache, Tomcat, PHP, Nginx, Mysql, JDK

3, after the optimization of the parameters

The code is as follows Copy Code
PM = dynamic
Pm.start_servers = 5
Pm.min_spare_servers = 2
Pm.max_spare_servers = 8


add: Then for the general server, how to set these values, but also have a value to refer to.

Generally a php-cgi process occupies 20~30m about the memory, php-fpm a little more, press 30m calculate it. Your physical mem size/30m is almost the number of PHP-FPM processes you can set up, but it's certainly less than that. For example, I 8G of memory, probably set 100~200 between, that is, pm.max_children=100 is a more appropriate value.

There is another problem, php-fpm because some third-party libraries may cause memory leaks, a long time, the memory will be more, like our server is now about 50m, fortunately, there are pm.max_requests this parameter, This parameter specifies how many times a PHP-FPM child process executes and then restarts the process. This may be adjusted according to your actual situation, my pm.max_requests=500.

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.