Apache2 — Reducing Memory Footprint for Low Traffic Servers

Apache2 eats up a lot of memory. It must be configured, and the configuration options are confusing. In fact, all server admin stuff is confusing. The work these guys do is ridiculous. I will learn more one day, but I do freak out once every while when I’m forced to deal with the server.

Suddenly, my dev server was CRAWLING and I could barely get anything done. It turned out it was because apache2 started spawning a lot of processes due to the load.

To check… type in the following

ps -auxf | sort -nr -k 4 | head -10

or alternatively, just use top and sort by memory usage.

Your apache processes should be up there at the top of the memory consumers. If you see multiple apache2 servers running, and it’s overkill, or if you’re on a small server (my dev server is 256mb, so apache2 needs to be scaled back)  reduce the number of servers it spawns.

The result should be like so:

www-data 31759  1.5 11.9 218156 31384 ?        S    02:53   0:03  \_ /usr/sbin/apache2 -k start
www-data 31750  1.3 11.2 218088 29492 ?        S    02:52   0:03  \_ /usr/sbin/apache2 -k start
www-data 31742  1.2 10.8 219516 28420 ?        S    02:51   0:03  \_ /usr/sbin/apache2 -k start
www-data 31743  1.3  8.6 220880 22736 ?        S    02:51   0:03  \_ /usr/sbin/apache2 -k start
www-data 31746  1.1  7.5 218056 19828 ?        S    02:51   0:03  \_ /usr/sbin/apache2 -k start
root     31733  0.0  1.2 140892  3176 ?        Ss   02:51   0:00 /usr/sbin/apache2 -k start
www-data 31741  0.0  0.3  27976   996 ?        S    02:51   0:00  \_ nginx: worker process
www-data 31739  0.0  0.3  27872   836 ?        S    02:51   0:00  \_ nginx: worker process
root     31786  0.0  0.3  14780  1000 pts/1    R+   02:56   0:00          \_ ps -auxf
root     30330  0.0  0.3  17620  1040 pts/1    Ss   Feb11   0:00      \_ -bas

If there are multiple apache processes at the top, you can get rid of most of them if your site doesn’t get much traffic. This is especially important if you are trying to squeeze out every last drop from your server.

So, to reduce the processes generated:

Edit your apache configs, usually in /etc/apache2/apache2.conf

In the mpm_prefork_module settings, set StartServers to 1, MinSpares to 1 or 0, and MaxSpares to something like 3.

For example, 5 processes running will destroy my 256mb server, which is the default minimum for Apache2.

<IfModule mpm_prefork_module>
    StartServers          1
    MinSpareServers       1
    MaxSpareServers       3
    MaxClients          150
    MaxRequestsPerChild   500

Change the above settings as you test traffic loads to see whether the new settings can handle it.  My setup is for very little traffic.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s