Tuesday, March 7, 2017

memory - Monit is restarting apache2, should I fix the monit config, or the server config?



Monit is restarting apache2 frequently, based on the totalmem > 200m test in my apache2 stanza. I'm not sure that I actually have a problem, or if I should increase the totalmem test in my monit config.



This configuration was fine until we added a second wordpress site, when apache2 started triggering the monit totalmem > 200m condition. Before I begin considering the particular construction of the second wordpress site, I'd like to understand if I shouldn't just increase the totalmem limit in the monit test, and move on.



My monit stanza for the apache2 server was copied from a monit install tutorial. I deemed it a reasonable value to start with, and figured that I would determine a particular value for my particular server if I ran into an apache memory issues. Well, now I've hit the issue, and figuring out what that value should be has proven more challenging than I expected. I haven't found anything that tells me how to figure out what that value should be for a particular server configuration.



The host is a VPS running Ubuntu server 10.04LTS. Apache2 is running a survey app (mod_php5), two single wordpress installs (mod_php5), and a fork of Redmine (mod_passenger). We began to set up webdav, for intra-company file sharing, but are not actually using it. [NOTE: I know (now) that I can save memory by switching to REE instead of using ruby1.8.]




The server gets extremely low traffic - ~20 page views per hour, across all the apps, excluding bots and random hacker sniffing, and is unlikely to exceed 1000 page views per hour, for the forseeable future. Here are the details:




$ free -m
total used free shared buffers cached
Mem: 496 354 142 0 39 100
-/+ buffers/cache: 214 282
Swap: 1023 6 1017




top - 17:36:27 up 34 days, 2:23, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 69 total, 1 running, 68 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.2%us, 0.1%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 508272k total, 362944k used, 145328k free, 40448k buffers
Swap: 1048572k total, 6284k used, 1042288k free, 103028k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15257 www-data 20 0 228m 37m 4472 S 0 7.5 0:01.20 apache2
15258 www-data 20 0 225m 34m 4580 S 0 6.9 0:00.76 apache2

14680 www-data 20 0 224m 33m 4560 S 0 6.7 0:00.79 apache2
19146 www-data 20 0 222m 33m 3900 S 0 6.7 0:00.75 apache2
15255 www-data 20 0 222m 32m 3780 S 2 6.6 0:00.60 apache2
19733 mysql 20 0 160m 31m 6396 S 0 6.4 0:00.34 mysqld
9379 root 20 0 199m 12m 7368 S 0 2.6 0:43.30 apache2
10289 root 20 0 46500 9680 1516 S 0 1.9 7:23.87 ruby1.8
10292 nobody 20 0 72376 3656 2888 S 0 0.7 0:00.66 PassengerLoggin
19385 root 20 0 81348 3552 2680 S 0 0.7 0:00.04 sshd
19401 hostexsa 20 0 19556 2256 1592 S 0 0.4 0:00.10 bash
10287 root 20 0 97808 2164 1896 S 0 0.4 0:02.19 PassengerHelper

10282 root 20 0 23512 1908 1648 S 0 0.4 0:00.01 PassengerWatchd
25370 root 20 0 105m 1876 1260 S 0 0.4 3:58.88 monit
19400 hostexsa 20 0 81348 1784 912 S 0 0.4 0:00.25 sshd
3616 root 20 0 245m 1700 1152 S 0 0.3 0:00.21 console-kit-dae
19926 hostexsa 20 0 19248 1220 932 R 0 0.2 0:00.05 top



:/etc/monit/conf.d $ cat apache2.conf
check process apache2 with pidfile /var/run/apache2.pid

start program = "/etc/init.d/apache2 start" with timeout 20 seconds
stop program = "/etc/init.d/apache2 stop"

if cpu is greater than 60% for 2 cycles then restart
if cpu > 80% for 5 cycles then restart
if totalmem > 200.0 MB for 5 cycles then restart
if children > 250 then restart
if loadavg(5min) greater than 10 for 8 cycles then stop
if 3 restarts within 5 cycles then timeout


if failed host sub.example.net port 80 protocol http
and request "/monit/token" then restart

group server



$ /usr/sbin/apache2 -v
Server version: Apache/2.2.14 (Ubuntu)
Server built: Nov 18 2010 21:19:09


/etc/apache2/mods-enabled$ ls
alias.conf autoindex.load env.load reqtimeout.conf
alias.load cgi.load mime.conf reqtimeout.load
auth_basic.load dav_fs.conf mime.load rewrite.load
authn_file.load dav_fs.load negotiation.conf setenvif.conf
authz_default.load dav.load negotiation.load setenvif.load
authz_groupfile.load deflate.conf passenger.conf status.conf
authz_host.load deflate.load passenger.load status.load
authz_user.load dir.conf php5.conf

autoindex.conf dir.load php5.load


From my apache2.conf:




KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 10



StartServers 1
MinSpareServers 1
MaxSpareServers 5
MaxClients 50
MaxRequestsPerChild 5000





:/etc/mysql/conf.d$ cat mysqld_small_footprint.cnf
allowed_packet = 1M
thread_stack = 64K
table_cache = 4
sort_buffer = 64K
net_buffer_length = 2K
skip-innodb


UPDATE




Just found the following guideline in this article on basic apache server tuning It offers exactly the kind of guidance I had been seeking. For example:




For optimum performance, you’ll want to tweak Apache’s MaxClients setting such that Apache can consume no more than 70% of your available physical memory. Any higher and you risk the box starting to dip heavily into swap space, which will drastically increase IO and CPU wait and can easily cause the server to fall to its knees.



Answer



In and of itself, using more than 200MB of RAM isn't necessarily an issue for your apache server. But I think that you just need to figure out what is acceptable for your setup.



If there's nothing else on that VPS, I think it's safe to allow apache more than 200M as an upper limit.




If you have the time and know-how, you can certainly find ways to bring that number down, but it may be more efficient for you to just bump up your monit threshold and not have it keep restarting. For all you know, it might stabilise at 201MB of usage, right (unlikely, but you see my point).



If I were in your shoes, I wouldn't consider it acceptable to have apache restarted every once in a while... stability is desirable... more so than an arbitrary amount of free RAM available on the server.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...