Recently I have been running in to a lot of issues with Apache performance on both CentOS installs and Debian installs. This is most likely due to a ton of Apache Modules and such, but there are some ways to tweak your httpd.conf so that you have the best performance for your traffic – this varies among different sites and hosts, so for this I will be writing more or less how to optimize it for a 512MB Virtual Private Server (VPS).
If you want to do some quick modifications to make your Apache server run a little bit better, I would suggest doing this:
Find your httpd.conf file (in Debian / Ubuntu based distros, it’s /etc/apache2/apache2.conf, and for RPM based distros its /etc/httpd/conf/httpd.conf). In that file you will find, if you scroll down, a place that says:
Straight from Apache, – “This Multi-Processing Module (MPM) implements a non-threaded, pre-forking web server that handles requests in a manner similar to Apache 1.3. It is appropriate for sites that need to avoid threading for compatibility with non-thread-safe libraries. It is also the best MPM for isolating each request, so that a problem with a single request will not affect any other.”
This is what most (modern) web servers are running. If you want to be sure it is what you are running, you can run this on RPM based distros:
# /usr/sbin/httpd -l
And on Debian based distros:
# /usr/sbin/apache2 -l
That will list all your modules that your web server is loading. You should get a list back that is something like:
Compiled in modules:
That will tell you if you are using prefork or worker mpm. In any event, back to the issue at hand.
In your httpd.conf file, you’ll see something underneath prefork mpm that looks something like this:
This is what we want to tweak. Let’s start with “StartServers”.
StartServers is basically the number of child server processes created at startup (or at least, when you start the Apache service). For prefork, 5 is pretty standard. If you have some extra RAM hanging around, it may be alright to increase this, but for our little 512MB, it would be prudent to leave this at or below 5.
MinSpareServers is more or less the minimum number of idle child server processes. Straight from Apache again – tuning of this parameter should only be necessary on very busy sites. Setting this parameter to a large number is almost always a bad idea. I’ve seen some people drop it to 2 or 3, and I’ve seen others leave it at 5. I’ve had no problems with it at 5 personally.
MaxSpareServers is the maximum number of idle child server processes. This should usually be at or double your MinSpareServers. On a 512mb server, it should be at 10. If you have a gig or two gigs, you could probably up this to 20 or 25.
MaxClients pretty much sets the limit on the number of simultaneous requests that will be served at any one moment. Any more connections that are over limit will be queued. Once a process is freed at the end of a different request, the queued connection will connect. This is EXTREMELY dependent on how much traffic your server / sites have. If you are receiving 40 or 50 independent hits per minute, it’s probably a good idea to get a better VPS than 512mb of RAM and to crank this up a bit.
MaxRequestsPerChild sets the limit on the number of requests that an individual child server process will handle. After the number of requests reaches the value specified, the child process will die. When this value is set at 0, the process will never expire and just sit in RAM. This and MaxClients are somewhat related. Personally, on a VPS, I would suggest setting it between 150 – 500, though I have seen some people who set it as high as 5000 – 10000.
And here it is in a nutshell. The first thing that happens when you start Apache is that it reads the StartServers and starts that many Apache Processes (if its 10, it starts 10 processes) – the second thing it reads is the MinSpareServers and Max Spare Servers and adds the MinSpareServers to your StartServers (2 + 10). Next if you have 2 as your MinSpareServers and 5 as your MaxSpareServers and if your server is under no load, Apache drops down to 2 – 5 processes. The last thing it reads is MaxClients – Apache will spawn up to this number of processes as the server needs them at a rate of 2^N per second (1, 2, 4, 8, 16) until it reaches your MaxClients. It’s a good idea to make this a value divisible by 2, and honestly, by 16.
MaxRequestsPerChild is the total number of incoming connections to your server. Some pages have a few things to load, some pages have only one (like say, photos, a style sheet, a link, etc…). Some browsers split this up and will call it in more than one connection. Other browsers will only open one connection. This is the max allowed connection per any given time. If this is full, then clients are queued until more connections are available.
Really, my honest suggestion here is to not just copy and paste my settings and hope it works for you, but to change yours slightly and monitor your server. Watch your processes and see what works best for you and your particular situation. If you have a ton of PHP on your server, maybe you should also look in to PHP APC. In my opinion though, each server is a little bit different, just as a lot of web sites are a little different – so play around with it and see what works best for you.