PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with handling heavy traffic and heavy-loaded websites and requests. The PHP-FPM advanced process management with start/stop.

If you run php-fpm service on LEMP( Linux, NGINX, MySQL/MariaDB, PHP) stack, most probabaly use the php-fpm service with proxy, this is widely-used and produce high-performance.

Have installed LEMP stack on my instance for the WordPress application, but it's taking huge memory usage and very slow, then not able to view the webiste as a normal speed. Sometimes stopped or 502 bad gateway error. After we analysed the system was running low on RAM: PHP-FPM had consume most of the RAM space.

Enter the command top -c and hit the keys SHIFT+M can see the output like below,

Here, how do we prevent and resolve this problem php-fpm from consuming too much or all your system memory (RAM) in Linux.

Reduce PHP-FPM Memory Usage

We need to look at a file www.conf configuration file of php-fpm, that is located by default in differernt path based on your operating system.

On CentOS/RHEL/Fedora:

$ sudo vim /etc/php-fpm.d/www.conf             

On Ubuntu/Debian/Mint:
$ sudo vim /etc/php/7.4/fpm/pool.d/www.conf

On Bitnami stack:
$ sudo vim /opt/bitnami/php/etc/php-fpm.d/www.conf

Just add the lines below if you are not added previously,
pm = ondemand
pm.max_children = 80
pm.process_idle_timeout = 10s
pm.max_requests = 200

pm -> The default configuration is "dynamic" set to "ondemand" how the process manager will control the number of child processes.

pm.max_children -> It defines the maximum number of children process running at the same time.

pm.process_idle_timeout -> The number of seconds after which an idle process will be killed.

pm.max_requests ->  number of requests each child process should execute before respawning.

Restart the service on Bitnami,

$ /opt/bitnami/ restart php-fpm

Restart the service on Ubuntu/CentOS/RHEL/Debian,

systemctl restart php-fpm.service    # typical
systemctl restart php5-fpm.service   # uncommon
systemctl restart php7.0-fpm.service # uncommon PHP 7