Page 1 of 2

Web server running out of RAM

Posted: Mon Mar 06, 2017 2:44 pm
by vesta_mtl
I have a 4 GB DigitalOcean droplet and I have found that it runs out of RAM after 24-48 hours.

Someone on another forum suggested I limit the amount of processes spawned like this:

In this file: /etc/httpd/conf.d/fcgid.conf
Add this: FcgidMaxProcesses 35

In this file: /etc/httpd/conf/httpd.conf
Add this:
<IfModule prefork.c>
ServerLimit 35
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 35
MaxConnectionsPerChild 75
</IfModule>


However, someone was kindly helping me in another post on this forum, and suggested this was a bad idea since I would eventually be kicking out users:
viewtopic.php?f=11&t=14033&p=57492#p57486

So my question is this: What is the best way to ensure my VPS doesn't run out of RAM? Is there a way to make the spawned processes recycle themselves more efficiently? Or is there something else that I should try?

Thanks in advance for your help.

Re: Web server running out of RAM

Posted: Tue Mar 07, 2017 8:43 am
by skurudo
* Optimization configuration of php5-fpm and mysql of course
* Check your php code, maybe you have memory leak.

Re: Web server running out of RAM

Posted: Tue Mar 07, 2017 8:52 am
by newtron
Check if your webpages is sending spam (or users), a way to detect, use this in terminal:

Code: Select all

tail -f /var/log/exim4/mainlog

Re: Web server running out of RAM

Posted: Tue Mar 07, 2017 11:08 am
by skurudo
newtron wrote:Check if your webpages is sending spam (or users), a way to detect, use this in terminal:
tail show you only last 10 lines of file? It's not so much for analysis, ic ;-)

Re: Web server running out of RAM

Posted: Wed Mar 08, 2017 6:03 am
by plutocrat
VestaCP is quite happy running in 1Gb, or even 512Mb DO droplet if you really squeeze it, so I think you might have to look around to see what's causing the problems.
mysqltuner.pl (from )https://github.com/major/MySQLTuner-perl is good for diagnosing database problems. In particular look at the number of connections. This is usually set waaaay too high. mysqltuner will tell you you've only used a max of, say, 10 out of 300 connections, so in this case try brining it down to 50 or so. Experiment.

htop and ps_mem.py are also useful. It will tell you what is using the memory in your system. Get familar with them.
(htop can be installed with your package manager (apt-get install htop). ps_mem.py is from here https://github.com/pixelb/ps_mem

You could also look at putting php-fpm into 'ondemand' mode, and limit the number of child processes and servers is spawns.

OK, those should get you started. I'd be interested if you posted some of your results.

Re: Web server running out of RAM

Posted: Wed Mar 08, 2017 1:13 pm
by vesta_mtl
Thanks everyone for the helpful advice. It's enough to get me started.

Thanks again.

Re: Web server running out of RAM

Posted: Mon Mar 13, 2017 2:22 pm
by SS88
If you have a default install of MySQL you will be fine but they can always be optimized. Kudos to plutocrat.

The usual culprit is PHP-FPM and I bet the setting is set to dynamic.

If you are using a default.tpl for PHP-FPM then edit this:

/usr/local/vesta/data/templates/web/php-fpm/default.tpl

Change to:

Code: Select all

[%backend%]
listen = 127.0.0.1:%backend_port%
listen.allowed_clients = 127.0.0.1

user = %user%
group = %user%

pm = ondemand
pm.max_children = 50
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 10
pm.process_idle_timeout = 10s
pm.max_requests = 500

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
If you are using socket.tpl, edit this:

/usr/local/vesta/data/templates/web/php-fpm/socket.tpl

Change to:

Code: Select all

[%backend%]
listen = /var/run/php5-%backend%.sock
listen.allowed_clients = 127.0.0.1

user = %user%
group = %user%

listen.owner = %user%
listen.group = nginx

pm = ondemand
pm.max_children = 50
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 10
pm.process_idle_timeout = 10s
pm.max_requests = 500

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp = /tmp

Re: Web server running out of RAM

Posted: Mon Mar 13, 2017 5:13 pm
by vesta_mtl
Thanks for the advice.

I am using the "sk-php70" web template from here:
viewtopic.php?t=10854

It is FastCGI, but not php-fpm, so I don't know if your advice would be applicable.

I made some optimizations in this file:
/etc/httpd/conf.d/fcgid.conf

As follows (modified from my original post):

Code: Select all

FcgidMinProcessesPerClass 0
FcgidMaxRequestsPerProcess 0
FcgidInitialEnv PHP_FCGI_MAX_REQUESTS 0
FcgidInitialEnv PHP_FCGI_CHILDREN 0
FcgidMaxProcesses 120
FcgidMaxProcessesPerClass 20
FcgidFixPathinfo 1
FcgidIdleTimeout 30
FcgidBusyTimeout 300
FcgidProcessLifeTime 60
FcgidIOTimeout 300
FcgidIdleScanInterval 15
FcgidErrorScanInterval 15
FcgidZombieScanInterval 15
This, combined with the Apache tuning done in /etc/httpd/conf/httpd.conf (see original post), has resulted in my RAM usage being much more stable and normal. Always around 50%.

I am not an expert, and perhaps there are flaws in my logic, but this is what I have found to be a functional solution for me.

Re: Web server running out of RAM

Posted: Mon Mar 13, 2017 5:21 pm
by SS88
Yes, scrap my idea as it won't work for you.

You can always reduce "FcgidProcessLifeTime" from 60 to 30 if you like. This will kill processes that are not being used after 30 seconds. Technically, this is always best with SSD disks.

Re: Web server running out of RAM

Posted: Mon Mar 13, 2017 5:30 pm
by vesta_mtl
Thanks, I had that initially, and it was good. So I tried going to 60 and it is still good, so I left it there.

From what I found online, FcgidIdleTimeout should be about half of FcgidProcessLifeTime, which is why I have FcgidIdleTimeout set to 30.