If you have followed someone’s guide like we have published How to Install Apache MPM Event and PHP-FPM on Ubuntu Server then in certain cases (depending on your setup) your server may consume near 100% RAM. This article discusses how to find the faulty settings for your setup and fix them to bring it back to 50% RAM consumption so that the server does not trigger OOM Killer (that means Apache or PHP-FPM will be killed and your site will be down).
The Faulty Settings is Usually www.conf of PHP-FPM
Usually, the configurations available Apache MPM Event module will not suck more than 1GB RAM. You can download a script and run it to get an idea of who is sucking the RAM:
curl https://raw.githubusercontent.com/pixelb/ps_mem/master/ps_mem.py --output ps_mem.py
chmod a+x ps_mem.py
sudo python ps_mem.py
You’ll get a chart like this one:
Private + Shared = RAM used Program
28.0 KiB + 7.5 KiB = 35.5 KiB none
208.0 KiB + 25.5 KiB = 233.5 KiB atd
312.0 KiB + 33.5 KiB = 345.5 KiB cron
15.9 MiB + 730.5 KiB = 16.6 MiB f2b/server
21.5 MiB + 14.5 KiB = 21.5 MiB snapd
11.8 MiB + 11.8 MiB = 23.6 MiB systemd-journald
135.8 MiB + 72.4 MiB = 208.2 MiB apache2 (6)
5.0 GiB + 587.5 MiB = 5.6 GiB php-fpm7.2 (101)
7.6 GiB + 262.5 KiB = 7.6 GiB mysqld
For a 16GB server, the total RAM usage should be around 10GB else during a sudden traffic spike, the site can fail. In the above example, you have two options – (a) re-tune MySQL to consume lesser RAM and (b) re-tune PHP-FPM to consume lesser RAM.
It is better to adjust PHP-FPM settings if you have set
pm = dynamic from the
/etc/php/7.x/fpm/pool.d/www.conf file. The settings we have mentioned in our earlier guide was for 16GB dedicated web server (with no MySQL server instance running) but should consume very less RAM. Maybe your settings were set for too high usage than our example. You can change
pm = dynamic to
pm = ondemand keeping the other settings unmodified.
Or, you can adjust the values to half for testing purposes or can try this setting:
pm = dynamic
pm.max_children = 128
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 50
pm.process_idle_timeout = 10s;
pm.max_requests = 1000
You have three options to test. Restart the services after adjustment:
sudo service php7.2-fpm restart
# sudo service phpx.y-fpm restart
sudo apachectl configtest
sudo systemctl restart apache2
Install Glances and launch it to monitor:
apt install glances
The above system will remain stable after publication of an article.
Also check with
free -m. If your server is consuming around 60-70% RAM then it is in safe condition. After a week’s monitoring, you can decide whether you’ll adjust MySQL settings or not. But PHP-FPM sucking or reserving too much is wastage of resource unless there is significant rise of traffic.