My web site is gaining popularity quickly, but I can't afford to upgrade my hardware. What can I do to optimize Apache?
My web site is gaining popularity quickly, but I can’t afford to upgrade my hardware. What can I do to optimize Apache?
With the explosion of Internet use and its attendant traffic, keeping pace with rising demand is a common problem. Luckily, there are some concrete steps you can take to relieve system stress.
As with all performance problems, your first action should be to identify and ease bottlenecks. Use tools such as top, vmstat, and iostat to diagnose your system. Also, consider what kinds of content you are serving: if you use CGI, scripting, databases, or other tools to generate and serve dynamic content, analyze and optimize each of those subsystems, too.
Focus next on hardware. Lots of RAM is essential for smooth Apache performance — you must have enough memory to avoid hitting swap. If you serve static content, raw CPU power shouldn’t be a concern. However, if you serve dynamic content, the more processing power, the better. Also, make sure you have fairly fast disks, and, of course, enough network bandwidth.
There are also many ways to improve Apache’s own performance. In Apache’s configuration file, httpd.conf, it’s important that you disable HostnameLookups. If left on, Apache waits for a DNS response before completing each and every request. (If you want to use reverse lookups to collect statistics, you can easily derive them after the log’s been rotated. In fact, many log analysis programs even have this built-in.)
Whenever possible, enable FollowSymLinks and disable SymLinksIfOwnerMatch. This combination of settings avoids extra system calls for every request. Also, consider disabling the AllowOverride option. While .htaccess files are extremely convenient in mass hosting environments, all of its features are readily set in httpd.conf, and disabling it saves Apache from having to check for the files existence on every request.
You should also disable any unused Apache modules, which decreases the amount of memory that Apache uses. If an Apache module is built as a DSO, you can easily disable it by commenting out the module in httpd.conf and restarting Apache. However, if the module is compiled into Apache, you’ll need to recompile Apache to remove the module.
If you’re using Apache 1.3.x to serve a high-traffic web site using mod_perl or PHP, you can significantly improve performance by using lingerd. lingerd is a daemon designed to do nothing but properly close network connections from an HTTP server. Why is it needed? Due to the way TCP/IP works, Apache often spends a large amount of time doing a lingering_ close() for each connection. Using lingerd to handle this task allows Apache to handle more connections. Licensed under an Apache-style license, lingerd is available from http://www.iagora.com/about/software/lingerd.
Installing lingerd is straightforward, but it does require patching Apache (which, incidentally, is how the “a-patchy” server got its name). Assuming your Apache sources are in $APACHE, and you’ve unpacked the lingerd source tar file, do the following:
% cd lingerd
% cp Apache-1.3/ap_lingerd.c li_config.h
% patch -p0 -d $APACHE/src/ < Apache-1.3
(By the way, if you’re using mod_ssl, you’ll have to apply patch aplinger-ssl.diff instead of aplinger.diff.) Now, type make in the lingerd source directory and copy the resulting binary to your preferred system directory.
You may have noticed the lack of the usual ./configure step: if you need to modify any of lingerd‘s default options, simply edit the file li_config.h. You can now configure and install Apache as usual.
Next, start lingerd as the same user that Apache runs as, and then start Apache. If lingerd fails to start, it logs an error via syslog. Be sure to verify that the directory lingerd is trying to write its PID file to exists. After you’ve verified everything is functioning, setup lingerd to automatically start at boot.
To increase performance even further, turn off Keepalives, and serve all static content such as images using a lightweight HTTP server such as thttpd, available from http://www.acme.com/software/thttpd.
Apache is a powerful and flexible program and we’ve only scratched the surface of optimization. Visit the official Apache web site for the official guides on tuning Apache 1.3.x and 2.0.x.
Many tips you run across elsewhere may only apply to Apache 1.3.x or Apache 2.0.x, so be careful. With the exception of lingerd, all tips described here apply to both versions.
Find your bottleneck, eliminate it, find the next one, and so on. After making each change, verify that you really have improved performance by using the Apache Bench (ab) benchmarking utility that comes with Apache or any of the other myriad HTTP benchmark tools available.
Have fun and happy serving!
Jeremy Garcia is the founder and administrator of
LinuxQuestions.org, a free, friendly, and active Linux community. Please send questions and feedback to email@example.com.