How to solve PHP-FPM in Linux under too many processes causing memory exhaustion

Source: Internet
Author: User
Tags server memory
I found in the personal blog of the Linux service, the database of this service is often unable to use the need to restart to be able to access the normal, so give everyone the solution to this problem.

Analyze problems

After discovering the problem, first use the FREE-M directive to view the current server execution state:


Can see my server memory is 2G, but currently available memory only 70M, memory utilization of up to 92%, it is likely that memory usage is too high cause the database service hangs up.

To continue with the details, use the top command:


Then look at the process in detail in the output of the command, focusing on the 10th column memory usage ratio:


found that the CPU usage is not high, also ruled out the problem of the CPU, you can see that the database service consumes 15.2% of memory, memory usage is too high when the database process will be squeezed out (the most memory-intensive process), causing the service to hang up, so we need to see the detailed memory usage, What processes consume so much memory?

Instructions for use:

PS Auxw|head-1;ps auxw|sort-rn-k4|head-40


See the top 40 processes that consume the most memory:


Looking at the fourth column memory usage ratio, found that in addition to the MySQL database service, the PHP-FPM service pool opened too many child processes, consuming more than half of the memory, the problem was found, we began to solve the problem: set the number of control PHP-FPM process pool process.

Solve the problem

Through various search methods, it is found that you can control the number of PHP-FPM sub-processes by configuring the Pm.max_children property, first, open the PHP-FPM configuration file, execute the instructions:

Vi/etc/php-fpm.d/www.conf


Find the Pm.max_children field and find its value too large:


, the Pm.max_children value is 50, each process consumes 1%-2.5% of the memory, add up to consume more than half of memory, so we need to make its value smaller, bloggers here set it to 25, at the same time, check the following two properties:

Pm.max_spare_servers: This value represents the maximum number of idle processes that are guaranteed, and if the idle process is greater than this value, this is cleaned pm.min_spare_servers: The minimum number of idle processes is guaranteed, and if the idle process is less than this value, a new child process is created;

Both values cannot be greater than the Pm.max_children value, and the Pm.max_spare_servers value is typically set to 60%-80% of the Pm.max_children value.

Finally, restart PHP-FPM

Systemctl Restart PHP-FPM


To see memory usage again, use a lot less memory:


After the memory usage has been observed many times, it is found that the server memory resource consumption is greatly alleviated after this improvement.

PS: View the number of processes opened by PHP-FPM and the memory limit per process

1. See how many php-cgi processes are open on the server by command

Ps-fe |grep "php-fpm" |grep "Pool" |wc-l


2. See how many php-cgi processes have been used to process TCP requests

Netstat-anp|grep "PHP-FPM" |grep "tcp" |grep "Pool" |wc-l


3.linux+nginx+php environment, the memory limit for each PHP-FPM process

Setup method:

Edit php-fpm.conf configuration file

Php_admin_value[memory_limit] = 128M (configuration file on my server in/etc/php5/fpm/pool.d/www.conf this file is included in the php-fpm.conf) The number behind can be changed casually: 32m,64m,128m,256m,512m, this setting can be based on your server memory size and your needs to write, modified to load the PHP-FPM service.


Believe that you have seen these cases you have mastered the method, more wonderful please pay attention to the PHP Chinese network other related articles!

Related reading:

A detailed example of binary lookup and binary lookups in Java algorithms

JavaScript binary find detailed _javascript tips

JavaScript binary find the position of the character in the array (with sequence list) _javascript tips

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.