Every now and then my web sites’ traffic (and the load on the servers) increases to the point where I have to do something about it. I start finding out whatever I can about optimizing Apache, MySQL, PHP, and other components, simplify my code and database queries as much as possible, then if that fails, start pricing higher-powered servers.

Last time this happened, a few months ago, the main server was running out of memory and nearly crashing several times a day. I ran across this O’Reilly article on tuning Linux web servers and followed a few of the suggestions that were new to me.

One thing that really helped: it seems that Apache threads gradually increase their memory allocation as they run, depending on what dynamic code you run with them. Setting the MaxRequestsPerChild parameter in httpd.conf forces it to start new threads periodically, and this memory growth process starts over. (I used the value 200, but you need to experiment with your server.)

It’s only a temporary measure, but this kept the server running smoothly for the last few months. The server load reached 20 (very bad) on a typical day before the change, and stayed around 5 (slightly bad) after.

Now traffic levels and server load have nearly reached critical again, so it’s back to the drawing board… Anyone got any amazing apache speedups?

2 Responses to “Tuning Apache memory usage”

My website traffic is very bursty, so i found a good tweak was to set minspareservers and maxspareservers to 32 and 64 respectively. There are other tweaks you can do like disable htaccess lookups, disable logging allow persistent mysql connections (mysql_pconnect()). I actually boosted my MaxRequestsPerChild from 100 to 10000. It was running quite well but today it’s using up a lot of swap. 1000 might be a good number.