Optimizing Apache Web Server
Optimize your Apache Server
Once your web site becomes popular, you will have to start worry about another issue, how to get the most out of your server. With the traffic loads pounding it constantly, you will have to find a way to optimize it to handle the traffic a bit better.
That is where this part of the tutorial comes into play. Before we begin, I must state that all of these tips will be aimed at unix servers, as Apache is not optimized for Windows at all.
When looking at speed issues and Apache, we must look at the hardware/operating system, your configuration files, and how you compiled the server from the start.
For those of you that want to read all of your performance options, check out http://www.apache.org/docs/misc/perf-tuning.html and http://www.apache.org/docs/misc/perf.html. Also you might want to check out the O'Reilly book, Web Performance Tuning.
This is something you might not have even thought of. The two culprits here are simply
.htaccess files and symbollic links. For those of you that are looking at the page funny,
.htaccess files are a type of access control file. They can contain Apache Directives, and apply to the directory they are in and everything below it, overridden only by other
.htaccess files. By default, the Apache Directive AccessFileName is set to
.htaccess, so it is known as that unless you changed it.
So you can really think of it as just another Apache configuration file, though it is optional.
When returning a document to the client the server looks for an access control file with this name in every directory of the path to the document, if access control files are enabled for that directory. For example, say I was getting a document found at
http://www.mysite.com/test/test2/test3/test4.html and my DocumentRoot directive is set to
/usr/local/apache/htdocs. Apache would look for an
.htaccess file in the following directories;
Do you see what I'm getting at? With access control files enabled, Apache has to do quite a bit of checking for unnecessary files, which robs you of some performance.
So how do we fix this problem? We can use the Directory container to specify no overrides are acceptable within the container. In this case, we use the root directory.
After that, we may want to be able to still use
.htaccess files. If this is the case, you can enable them again,
And there you go. The AllowOverride directive has a few options actually, to see them, check out http://www.apache.org/docs-1.2/mod/core.html#allowoverride and http://www.apache.org/docs-1.2/mod/core.html#options.
Symbollic links have a similar issue, and the fix is nearly the exact same, except different directives are used.
Options -FollowSymLinks +SymLinksIfOwnerMatch
Now we got rid of the file lookup problem, as there are no unnecessary file checks.
Apache, by default, has the directive HostnameLookups set to off. This is a good thing, as, with it on, each client request to your server would result in a lookup request to the nameserver.
This is also a problem if you are restricting access to particular sections of your site with the order directive, accompanied by allow and deny. It is best if you use IP addresses in this case, as otherwise, you have to do another nameserver lookup.
With that out of the way, your Apache server doesn't have to do any unnecessary DNS stuff, and can stick to it's job.
If your CGI's, PHP or whatever else might require using hostname lookups, you can use a file container to specify this.
<Files ~ "\.(php3|cgi)$>
Here I go again with my fancy words, content negotiation. I know I suggested using it earlier, but that's only if it would benefit you. Otherwise, you should stay away from it.
Why? Since it requires searching for particular files, and not exact names, the searching part puts a little more load on the server, therefore slowing it down just a tad. Also, for directives that ask you for files, such as DirectoryIndex, instead of just putting index, put the actual file name. The example is below;
Index is a wild card, as there is no extension specified, and it has to do some searching again to find the file. Putting a list of names would be a much quicker, more efficient method. Put the most common name first, and in descending order.
DirectoryIndex index.html index.shtml index.cgi
As with most software, Apache needs RAM, especially if you get a lot of traffic. If it gets to the point that your operating system has to start swapping memory, with the hard drive, you must either get more RAM or set the directive MaxClients a little lower.
Swapping memory isn't necessarily slow, but for the client, it is. They will get irritated, hit stop-reload a few times, and further the load on your server. Not a good thing.
Besides that, with a fast enough hard drive, network card and CPU, your server should perform quite well (hardware-wise at least). You will have to experiment though to optimize it the best.
Settings such as MinSpareServers, MaxSpareServers, StartServers, and KeepAliveTimeout (if KeepAlive is set to
'On'). The SpareServers directives instruct the server how many 'child' processes to create. Apache uses these directives to determine the load it has to adapt to.
If your server has children waiting for requests, and the amount exceeds the MaxSpareServers setting, some of those child processes will be killed. If the amount is less than MinSpareServers, some child processes will be created.
The settings of these directives will directly relate to your server performance. If you don't have enough child processes to handle the current server load, some requests will be delayed, causing your server to be slower. Too many processes will also slow down your server, if you don't have enough RAM and CPU power to handle it.