Apache Performance Tuning

This is a variety of tips complied from various places listed below. This is the notes that I took for myself while researching apache performance on several different machines. Read more to see the rest.

Hardware

  • The single biggest hardware issue affecting webserver performance is RAM. A webserver should never ever have to swap, as swapping increases the latency of each request. You can control this by modifying the MaxClients setting in the apache conf file.
  • A simple calculation for MaxClients on a system that does only Drupal would be:
    • (Total Memory – Operating System Memory – MySQL memory) / Size Per Apache process.
  • If your server is configured with all sorts of bells and whistles (like mod_perl, mod_python, in addition to mod_php), then Apache can easily be 21 MB per process. If your server has 512MB, then you can fit some 20 Apache processes. If you tune Apache well, and remove all the unneeded modules, and install a PHP op-code cache/accelerator, then you can make each Apache process take as little as 12 MB. These figures depend on how many modules you have loaded, how big they are, so there is no hard and fast rule. Even if one has 1GB of memory, and leaves 250 MB for the system and MySQL, with an Apache process of 15MB, this means 50 Apache processes can fit in the remaining 750MB.
server type hostname processor hardware ram apache process size
MAMP piro 1.33Ghz PPC G4 Mac Mini 512mb 31mb
MAMP yellow 1.83Ghz Intel Core 2 Duo Mac Mini 1gb 12mb
10.5.4 kimiko 1.83Ghz Intel Core 2 Duo Mac Mini 1gb 12mb
10.5.6 miho 2.4Ghz Intel Core 2 Duo MacBook Pro 2gb 540k

Operating System / Software

  • Use Apache 2.x (MAMP installs this)
  • If your OS supports a sendfile(2) system call, make sure you install the release and/or patches needed to enable it. On systems where it is available, sendfile enables Apache 2 to deliver static content faster and with lower CPU utilization.
    • From what I can tell this supposedly shipped on 10.5.x, but I am not sure how to confirm this or verify it is working, we can test performance with this by turning on/off sendfile via EnableSendfile

Apache Settings

MaxClients

  • You can, and should, control the MaxClients setting so that your server does not spawn so many children it starts swapping.
  • determine the size of your average Apache process, by looking at RSHRD in top, and divide this into your total available memory, leaving some room for other processes.

HostnameLookups

  • Make sure this is Off
  • This adds latency to every request because it requires a DNS lookup to complete before the request is finished. In Apache 1.3 this setting defaults to Off. If you need to have addresses in your log files resolved to hostnames, use the logresolve program that comes with Apache, or one of the numerous log reporting packages which are available.
  • Note that it’s possible to scope the directives, such as within a <Location /server-status> section. In this case the DNS lookups are only performed on requests matching the criteria. Here’s an example which disables lookups except for .html and .cgi files:
HostnameLookups off
<Files ~ "\.(html|cgi)$">
HostnameLookups on
</Files>

FollowSymLinks / SymLinksIfOwnerMatch

  • Wherever in your URL-space you do not have an Options FollowSymLinks, or you do have an Options SymLinksIfOwnerMatch Apache will have to issue extra system calls to check up on symlinks.
  • For highest performance, and no symlink protection, set FollowSymLinks everywhere, and never set SymLinksIfOwnerMatch.

AllowOverride

  • Wherever in your URL-space you allow overrides (typically .htaccess files) Apache will attempt to open .htaccess for each filename component.
  • For highest performance use AllowOverride None everywhere in your filesystem.

EnableSendfile

  • If this is available on the system it should be on, otherwise set EnableSendfile off
  • All Mac OS X boxes that are not 10.5.x should have this off

Apache Modules

  • Since memory usage is such an important consideration in performance, you should attempt to eliminate modules that you are not actually using. If you have built the modules as DSOs, eliminating modules is a simple matter of commenting out the associated LoadModule directive for that module. This allows you to experiment with removing modules, and seeing if your site still functions in their absense.
  • If, on the other hand, you have modules statically linked into your Apache binary, you will need to recompile Apache in order to remove unwanted modules.
  • An associated question that arises here is, of course, what modules you need, and which ones you don’t. The answer here will, of course, vary from one web site to another. However, the minimal list of modules which you can get by with tends to include mod_mime, mod_dir, and mod_log_config.

Multi-Processing Modules (MPM)

  • There are two MPMs to choose from in Apache 2.x
    • The worker MPM uses multiple child processes with many threads each. Each thread handles one connection at a time. Worker generally is a good choice for high-traffic servers because it has a smaller memory footprint than the prefork MPM.
    • The prefork MPM uses multiple child processes with one thread each. Each process handles one connection at a time. On many systems, prefork is comparable in speed to worker, but it uses more memory. Prefork’s threadless design has advantages over worker in some situations: it can be used with non-thread-safe third-party modules, and it is easier to debug on platforms with poor thread debugging support.
  • There are a couple of ways to tell what MPM your apache server was compiled with.
    • In the phpinfo configure block there should be a -with-mpm= that tells.
    • from the command line do $ httpd -l (thats a lower case L)
  • MAMP installs prefork

Need more info on / Questions

  • MinSpareServers, MaxSpareServers, StartServers, MaxRequestsPerChild, and KeepAliveTimeout
  • How can we determine if sendfile is working or not?

References

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">