linux

How to prevent PHP-FPM from consuming too much RAM in Linux

 

If you have enabled a LEMP (Linux, NGINX, MySQL / MariaDBand PHP) stack, you’ve probably used FastCGI proxying inside NGINX (As an HTTP server), for PHP processing. PHP-FPM (abbreviation for FastCGI process management) is a widely used and high-performance option PHP FastCGI implementation.

Here are helpful installation instructions LEMP stack On Linux.

Recently, everyone PHP websites on one of our sites LEMP the web servers slowed down and eventually stopped responding by logging on to the server. we noticed that the system was low on RAM: PHP-FPM had consumed most of the RAM, as shown in the following screenshot (at a glance – the system monitoring tool).

$ glances
Overview - a system monitoring tool
Overview – a system monitoring tool

In this article, we will show you how to prevent PHP-FPM consuming too much or all of the system memory (RAM) On Linux. At the end of this guide, you will learn to reduce PHP-FPM memory consumption of at least 50%.

Reduce PHP-FPM memory usage

After doing some research on the internet, we found that we need to configure PHP-FPM process control and certain aspects of it to reduce PHP-FPM memory consumption in the pool configuration file.

 

The default pool is www and its configuration file is located at /etc/php-fpm.d/www.conf (on CentOS / RHEL / Fedora) or /etc/php/7.4/fpm/pool.d/www.conf (on Ubuntu / Debian / Mint).

$ sudo vim /etc/php-fpm.d/www.conf             [On CentOS/RHEL/Fedora]
$ sudo vim /etc/php/7.4/fpm/pool.d/www.conf    [On Ubuntu/Debian/Mint]

Find the following directives and set their value according to your use case. Comments must be deleted from the commented directives.

pm = ondemand
pm.max_children = 80
pm.process_idle_timeout = 10s
pm.max_requests = 200

A brief explanation of the above directives and their values ​​is given. pm the directive determines how the process manager manages the number of sub-processes. The default is dynamic, which means that the number of children (child processes) is set dynamically depending on some other directives pm.max_children which determines the maximum number of children alive at one time.

The most ideal process control is if necessary a system in which child processes are not created during the startup phase, but are created when needed. Subprocesses only branch when new requests are merged pm.max_children and pm.process_idle_timeout which determines the number of seconds after which idles are killed.

Last but not least, we need to determine pm.max_requests a parameter that specifies the number of requests that each child process should execute before calling. Note that this parameter can also be used as a workaround for memory leaks in third-party libraries.

Reference: A better way to run PHP-FPM.

After completing these configurations above, I noticed RAM usage is now great on our server. Do you have any thoughts related to this topic or questions? Contact us via the feedback form below.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button