Book HomeApache: The Definitive GuideSearch this book

3.6. Controlling Virtual Hosts on Unix

When started without the -X flag, which is what you would do in real operation, Apache launches a number of child versions of itself so that any incoming request can be instantly dealt with. This is an excellent scheme, but we need some way of controlling this sprawl of software. The necessary directives are there to do it.

3.6.1. MaxClients




MaxClients number
Default number: 150
Server config

This directive limits the number of requests that will be dealt with simultaneously. In the current version of Apache, this effectively limits the number of servers that can run at one time.

3.6.2. MaxRequestsPerChild




MaxRequestsPerChild number
Default number: 30
Server config

Each child version of Apache handles this number of requests and dies (unless the value is 0, in which case it will last forever or until the machine is rebooted). It is a good idea to set a number here so that any accidental memory leaks in Apache are tidied up. Although there are no known leaks in Apache, it is not impossible for them to occur in the system libraries, so it is probably wise not to disable this unless you are absolutely sure the code is byte-tight.

3.6.3. MaxSpareServers

MaxSpareServers number
Default number: 10
Server config

No more than this number of child servers will be left running and unused. Setting this to an unnecessarily large number is a bad idea, since it depletes resources needlessly. How many is too many depends on which modules you have used and your detailed configuration. You can get some clues by studying memory consumption with ps, top, and the like.

3.6.4. MinSpareServers

MinSpareServers number
Default number: 5
Server config

Apache attempts to keep at least this number of spare servers running. If fewer than this number exist, new ones will be started at an increasing rate each second until MAX_SPAWN_RATE is reached. MAX_SPAWN_RATE is defined to be 32 by default, but can be overridden at compile time. If no new servers are needed, the number to be added is reset to 1. Setting number unnecessarily high is a bad idea because it uses up resources needlessly.

3.6.5. StartServers

StartServers number
Default number: 5
Server config

Although the number of servers is controlled dynamically (see MaxSpare-Servers), you may have a heavily used site and want to make sure that it starts up with lots of servers, rather than waiting for demand to set them going.

In older versions of Apache, new servers were only started at the rate of one per second, so careful consideration had to be given to these numbers on heavily loaded systems. However, in Apache 1.3 new servers are started more aggressively, so fine tuning of StartServers, MinSpareServers, and MaxSpare-Servers should be considerably less important. To cope with sudden bursts of traffic on heavily loaded systems, it is worth having a few spare servers available. Experience has shown that servers handling one million hits per day work well with MaxSpareServers set to 64 and MinSpareServers set to 32. Startup performance can be optimized by setting StartServers somewhere in the range of MinSpareServers to MaxSpareServers. It may also be worth increasing MaxRequestsPerChild in order to avoid unnecessary overhead from process restarts, but note that you increase the risk of damage by memory leaks if you do this. Do make sure you have enough memory available to actually run this many copies of Apache!

3.6.6. Unix File Limits

If you were doing this for real, you would expect the number of virtual httpds running to increase to cope with our various spin-off businesses. This may cause trouble. Some Unix systems will allow child processes to open no more than 64 file descriptors at once. Each virtual host consumes two file descriptors in opening its transfer and error log files, so 32 virtual hosts use up the limit. The problem shows up in "unable to fork" messages in the error logs, though this is not actually because Unix is unable to fork but because it can't create the pipes.[31] The solution is to use a single log and separate it out later.

[31]This particular error can be caused by various resource shortages, particularly open file limits and process limits; unfortunately, Apache doesn't generally tell you what caused the problem, which can be very frustrating. A particularly irritating pitfall is caused by restarting the server from a shell that sets the limits to different values from those used when the server started automatically at system boot. tcsh, for example, tends to do this.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.