Optimizing MySQL and Apache for Low Memory Usage, Part 1

MySQL and Apache can consume quite a bit of memory, if you’re not careful. This post discusses how to reduce the amount of memory they use without killing performance. The caveat, of course, is that you’re not going to be able to run a site with a large database and large amount of traffic with these settings. I’m going to try to explain the WHY more than the WHAT. All of this is in conjunction with my goal of reducing the amount of ram I use on my Xen based virtual server, as discussed previously in, Low Memory Computing.
Before I begin, I’d like to say that you should also look at various system utilities that consume ram. Services like FTP and SMTP can and should be passed off to xinetd. Also, you should look at shells besides bash, such as dash. And, if you’re really serious about low memory, you might look at using something like BusyBox, which brings you into the realm of real embedded systems. Personally, I just want to get as much as I can out of a standard linux distribution. If I need more horsepower, I want to be able to move to bigger, faster virtual machines and/or dedicated servers. For now, optimizing a small virtual machine will do.
First off, Apache. My first statement is, if you can avoid it, try to. Lighttpd and thttpd are both very good no frills webservers, and you can run lighttpd with PHP. Even if you’re running a high volume site, you can seriously gain some performance by passing off static content (images and javascript files, usually) to a lightweight, super-fast HTTPd server such as Lighttpd.

The biggest problem with Apache is the amount of ram is uses. I’ll discuss the following techniques for speeding up Apache and lowering the ram used.

  • Loading Fewer Modules
  • Handle Fewer Simultaneous Requests
  • Recycle Apache Processes
  • Use KeepAlives, but not for too long
  • Lower your timeout
  • Log less
  • Don’t Resolve Hostnames
  • Don’t use .htaccess

Loading Fewer Modules

First things first, get rid of unnecessary modules. Look through your config files and see what modules you might be loading. Are you using CGI? Perl? If you’re not using modules, by all means, don’t load them. That will save you some ram, but the BIGGEST impact is in how Apache handles multiple requests.

Handle Fewer Simultaneous Requests

The more processes apache is allowed to run, the more simultaneous requests it can serve. As you increase that number, you increase the amount of ram that apache will take. Looking at TOP would suggest that each apache process takes up quite a bit of ram. However, there are a lot of shared libraries being used, so you can run some processes, you just can’t run a lot. With Debian 3.1 and Apache2, the following lines are the default:

StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 20
MaxRequestsPerChild 0

I haven’t found documentation on this, but prefork.c seems to be the module that’s loaded to handle things w/ Apache2 and Debian 3.1. Other mechanisms could or could not be much more memory efficient, but I’m not digging that deep, yet. I’d like to know more, though, so post a comment and let me know. Anyway, the settings that have worked for me are:

StartServers 1
MinSpareServers 1
MaxSpareServers 5
MaxClients 5
MaxRequestsPerChild 300

What I’m basically saying is, “set the maximum amount of requests that this server can handle at any one time to 5.” This is pretty low, and I wouldn’t try to do this on a high volume server. However, there is something you can and should do on your webservers to get the most out of them, whether you’re going for low memory or not. That is tweak the keepalive timeout.

Recycle Apache Processes

If you noticed, I changed the MaxRequestsPerChild variable to 500, from 0. This variable tells Apache how many requests a given child process can handle before it should be killed. You want to kill processes, because different page requests will allocate more memory. If a script allocates a lot of memory, the Apache process under which it runs will allocate that memory, and it won’t let it go. If you’re bumping up against the memory limit of your system, this could cause you to have unnecessary swapping. Different people use different settings here. How to set this is probably a function of the traffic you receive and the nature of your site. Use your brain on this one.

Use KeepAlives, but not for too long

Keepalives are a way to have a persistent connection between a browser and a server. Originally, HTTP was envisioned as being “stateless.” Prior to keepalive, every image, javascript, frame, etc. on your pages had to be requested using a separate connection to the server. When keepalives came into wide use with HTTP/1.1, web browsers were able to keep a connection to a server open, in order to transfer multiple files across that same connection. Fewer connections, less overhead, more performance. There’s one thing wrong, though. Apache, by default, keeps the connections open for a bit too long. The default seems to be 15 seconds, but you can get by easily with 2 or 3 seconds.

This is saying, “when a browser stops requesting files, wait for X seconds before terminating the connection.” If you’re on a decent connection, 3 seconds is more than enough time to wait for the browser to make additional requests. The only reason I can think of for setting a higher KeepAliveTimeout is to keep a connection open for the NEXT page request. That is, user downloads page, renders completely, clicks another link. A timeout of 15 would be appropriate for a site that has people clicking from page to page, very often. If you’re running a low volume site where people click, read, click, etc., you probably don’t have this. You’re essentially taking 1 or more apache processes and saying, “for the next 15 seconds, don’t listen to anyone but this one guy, who may or may not actually ask for anything.” The server is optimizing one case at the expense of all the other people who are hopefully hitting your site.

Lower Your Timeout

Also, just in case, since you’re limiting the number of processes, you don’t want one to be “stuck” timing out for too long, so i suggest you lower your “normal” Timeout variable as well.

Log Less

If you’re trying to maximize performance, you can definitely log less. Modules such as Mod_Rewrite will log debugging info. If you don’t need the debugging info, get rid of it. The Rewrite log is set with the RewriteLog command. Also, if you don’t care about looking at certain statistics, you can choose to not log certain things, like the User-Agent or the Http-Referer. I like seeing those things, but it’s up to you.
Don’t Resolve Hostnames

This one’s easy. Don’t do reverse lookups inside Apache. I can’t think of a good reason to do it. Any self respecting log parser can do this offline, in the background.

HostnameLookups Off

Don’t Use .htaccess

You’ve probably seen the AllowOverride None command. This says, “don’t look for .htaccess files” Using .htaccess will cause Apache to 1) look for files frequently and 2) parse the .htaccess file for each request. If you need per-directory changes, make the changes inside your main Apache configuration file, not in .htaccess.
Well, that’s it for Part 1, I’ll be back soon with Part 2, where I’ll talk about MySQL optimization & possibly a few other things that crop up.


I’d like to give credit to a few articles that were helpful in putting this information together.  I’m not a master at this, I’m just trying to compile lots of bits and pieces together into one place.  Thanks go to:

Published by


I'm a software engineer in New Orleans interested in making things, growing things, big fast computers, media convergence, and pugs.

38 thoughts on “Optimizing MySQL and Apache for Low Memory Usage, Part 1”

  1. Great article! I’m definitely going to put some of this into practice. I just moved to a low-memory VPS and I am feeling the memory “crunch”. Looking forward to the MySQL followup.

  2. GREAT !! Can’t recommend this further, although I would recommend setting:

    MaxSpareServers 5
    MaxClients 5

    To about 6-7

    but yeah, follow the above guide, CAN’T Go wrong..

  3. One thing I haven’t seen addressed is how to detect if the number of spare servers configured in apache2.conf is too low. Is there any way to tell?

  4. If it’s too low, browser requests will have to wait til there’s a spare. In other words, you’ll probably need to do some stress testing.

  5. > If it’s too low, browser requests will have to wait til there’s a spare.

    MaxSpareServers says how many warm daemons to keep hanging around for requests. The Apache listerner will still create new daemons on command but it will kill these children off after usage. MaxClients governs how many simultaneous requests can be handled. I would set this to higher than MaxSpareServers.

  6. A quick test if you have enough meory for Apache and N servers

    StartServers N
    MinSpareServers N
    MaxSpareServers N

    I have only 128MB and use N=15

    Test with the following and observe swapping

    apache2 -k start; sleep 30; apache2 -k stop; vmstat 1

    Break when swapping ends… 😉

    Now try

    apache2 -k start; sleep 30; apache2 -k graceful-stop; vmstat 1

    Do “killall -s KILL apache2″ and weep…. ;-D

    A gracefull stop is costly.

  7. Pingback: Not So Wise
  8. Hello,

    Our customers would find this article useful. With your permission, can we feature this article on our support forums, giving you proper credit and a link back?


  9. Hi there! Followed your instructions, and now my virtual server literally is “on fire!” I was having services stopping and restarting abruptly when traffic spiked, and it was because memory was running out…

    I went from 712mb ram consumption to around 348mb with your tips, and the site is quite fast and responsive… Thanks!

  10. Thanks a lot for this clear and straight to the point article. My server was regularly becoming totally unresponsive because Apache would go crazy and trigger memory pages swapping, but following your directives it seems that I have finally solved my problem. My memory usage has been divided by 3, and at the same time requests are being served much more rapidly.

  11. I really doubt that turning off hostname resolving would increese performance even noticeably and I have a good reason for you right here on why you’d want it enabled: you can make access rules based on hostnames. If you know how DNS works you’d understand why it’s not a performance bump: All DNS lookups (even reverse lookups) are cached, and all OS do it locally. So for every unique IP it would only need to do the lookup once. After that the OS has the lookup cached in memory.

    Also .htaccess is very useful and I don’t see on how some file lookups could justify disabling it. I can only see why that would make sense in extreme cases: eg if you’re hosting some kind of file server with a lot of traffic and no special per directory rewriting or acceess control or applications running on it.

    If you’re really looking for conserving memory I would recommend turning KeepAlive off completly. Having it enabled gives clients the ability to lock your children and with a maximum ammount of children set, that’s a very bad idea unless you actually want people to time out. Turning it off worked great for me.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>