PHP-FPM Process Number Optimization method

Source: Internet
Author: User
Tags fpm vps
Background
Their server is a cloud server, the recent frequent php-fpm suspended animation state, hard disk and memory are relatively small, so the memory is often not enough, through the Linux command to 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:

Pm.max_children: Number of PHP-FPM processes opened in static mode
Pm.start_servers: Number of starting PHP-FPM processes in dynamic mode
Pm.min_spare_servers: Minimum number of PHP-FPM processes in dynamic mode
Pm.max_spare_servers: Maximum number of PHP-FPM processes in dynamic mode

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, it is better to choose which way to execute. 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
PM = dynamic
Pm.start_servers = 5
Pm.min_spare_servers = 2
Pm.max_spare_servers = 8

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.