Documente Academic
Documente Profesional
Documente Cultură
Forewarning:
"Premature optimization is the root of all evil." -- Donald Knuth.
...in other words, don't implement in extra c omplexity if you don't need it. A site handling a few
thousand requests per day will do fine on a default c onfiguration and just about any hardware. This
artic le is geared towards a site that needs to handle multiple c onc urrent requests [ten to several
hundred per sec ond].
RAM
The single biggest issue affec ting webserver performanc e is RAM. Have as muc h RAM as your
hardware, OS , and funds allow [within reason].
The more RAM your system has, the more proc esses [and threads] Apac he c an alloc ate and use;
whic h direc tly translates into the amount of c onc urrent requests/c lients Apac he c an serve.
Generally speaking, disk I/O is usually a c lose 2nd, followed by CPU speed and network link. Note that
a single PII 400 Mhz with 128-256 Megs of RAM c an saturate a T3 (45 Mbps) line.
Select MPM
Chose the right MPM for the right job:
1 of 8 12/09/2010 11:07 AM
Apache Performance T uning http://www.devside.net/articles/apache-performance...
worker:
Apa che 2.0-ba se d.
Multiple proce sse s, m a ny thre a ds pe r proce ss, thre a ds ha ndle re que sts.
Use d for lowe r m e m o ry consum ptio n a nd highe r pe rform a nce .
Do e s not provide the sa m e le ve l of isola tion re que st-to-re que st, a s a
pro ce ss-ba se d MP M doe s.
winnt:
The only MP M cho ice unde r W indo ws.
1 pa re nt proce ss, e x a ctly 1 child pro ce ss with m a ny thre a ds, thre a ds ha ndle
re que sts.
Be st so lution unde r W indows, a s on this pla tform , thre a ds a re a lwa ys "che a pe r" to
use ove r proce sse s.
Configure MPM
C ore Features and M ulti- P roc es s ing M odules
Default Configuration
<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
MaxClients 150
MaxRequestsPerChild 1000
</IfModule>
<IfModule worker.c>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
<IfModule mpm_winnt.c>
ThreadsPerChild 250
MaxRequestsPerChild 0
</IfModule>
2 of 8 12/09/2010 11:07 AM
Apache Performance T uning http://www.devside.net/articles/apache-performance...
Directives
I c onsider this direc tive to be the c ritic al fac tor to a well func tioning server. S et this number too low
and resourc es will go to waste. S et this number too high and an influx of c onnec tions will bring the
server to a stand still. S et this number just right and your server will fully utiliz e the available
resourc es.
An approximation of this number should be derived by dividing the amount of system memory
(physic al RAM) available by the maximum siz e of an apac he/httpd proc ess; with a generous amount
spared for all other proc esses.
Use 'ps -ylC httpd --sort:rss' to find proc ess siz e. Divide number by 1024 to get megabytes. Also try
'top'.
Use 'free -m' for a general overview. The key figure to look at is the buffers/c ac he used value.
Use 'vmstat 2 5' to display the number of runnable, bloc ked, and waiting proc esses; and swap in and
swap out.
Example:
Optimal settings:
With the above c onfiguration, we start with 5-10 proc esses and set a top limit of 15. Anything above
this number will c ause serious swapping and thrashing under a load; due to the low amount of RAM
available to the [virtual] S erver. With a dedic ated S erver, the default values [S erverLimit 256] will
work with 1-2GB of RAM.
When c alc ulating MaxClients, take into c onsideration that the reported siz e of a proc ess and the
effec tive siz e are two different values. In this setup, it might be safe to use 20 or more workers... Play
with different values and c hec k your system stats.
Note that when more c onnec tions are attempted than there are workers, the c onnec tions are plac ed
into a queue. The default queue siz e value is 511 and c an be adjusted with the ListenBac kLog
direc tive.
3 of 8 12/09/2010 11:07 AM
Apache Performance T uning http://www.devside.net/articles/apache-performance...
server load.
MaxRequestsPerChild
Direc tive MaxRequestsPerChild is used to rec yc le proc esses. When this direc tive is set to 0, an
unlimited amount of requests are allowed per proc ess.
While some might argue that this inc reases server performanc e by not burdening Apac he with having
to destroy and c reate new proc esses, there is the other side to the argument...
S etting this value to the amount of requests that a website generates per day, divided by the number
of proc esses, will have the benefit of keeping memory leaks and proc ess bloat to a minimum [both of
whic h are a c ommon problem]. The goal here is to rec yc le eac h proc ess onc e per day, as apac he
threads gradually inc rease their memory alloc ation as they run.
Note that under the winnt MPM model, rec yc ling the only request serving proc ess that Apac he
c ontains, c an present a problem for some sites with c onstant and heavy traffic .
Multiple requests from one c lient in rapid suc c ession c an have the same effec t on a S erver as
"c onc urrent" c onnec tions [threaded MPMs and direc tive KeepAlive taken into c onsideration]. If a
partic ular website requires 10 requests per page, 10 c onc urrent c lients will require MPM settings
that are geared more towards 20-70 c lients. This issue manifests itself most under a proc ess-based
MPM [prefork].
For a more effic ient use of system memory, either use mod_proxy to pass spec ific requests onto
another Apac he S erver, or use a lightweight server to handle static requests:
Note that c onfiguration settings will be quite different between a dynamic c ontent S erver and a
static c ontent S erver.
mod_deflate
Reduc e bandwidth by 75% and improve response time by using mod_deflate.
4 of 8 12/09/2010 11:07 AM
Apache Performance T uning http://www.devside.net/articles/apache-performance...
</Location>
Loaded Modules
Reduc e memory footprint by loading only the required modules.
S ome also advise to static ally c ompile in the needed modules, over building DS Os (Dynamic S hared
Objec ts). Very bad advice. You will need to manually rebuild Apac he every time a new version or
sec urity advisory for a module is put out, c reating more work, more build related headac hes, and
more downtime.
mod_expires
Inc lude mod_expires for the ability to set expiration dates for spec ific c ontent; utiliz ing the
'If-Modified-S inc e' header c ac he c ontrol sent by the user's browser/proxy. Will save bandwidth and
drastic ally speed up your site for [repeat] visitors.
KeepAlive
Enable HTTP persistent c onnec tions to improve latenc y times and reduc e server load signific antly
[25% of original load is not unc ommon].
prefork MPM:
KeepAlive On
KeepAliveTimeout 2
MaxKeepAliveRequests 80
KeepAlive On
KeepAliveTimeout 15
MaxKeepAliveRequests 80
With the prefork MPM, it is rec ommended to set 'KeepAlive' to 'Off'. Otherwise, a c lient will tie up an
entire proc ess for that span of time. Though in my experienc e, it is more useful to simply set the
'KeepAliveTimeout' value to something very low [2 sec onds seems to be the ideal value]. This is not a
problem with the worker MPM [thread-based], or under Windows [whic h only has the thread-based
winnt MPM].
With the worker and winnt MPMs, the default 15 sec ond timeout is setup to keep the c onnec tion open
for the next page request; to better handle a c lient going from link to link. Chec k logs to see how
long a c lient remains on eac h page before moving on to another link. S et value appropriately [do not
set higher than 60 sec onds].
SymLinks
Make sure 'Options +FollowS ymLinks -S ymLinksIfOwnerMatc h' is set for all direc tories. Otherwise,
5 of 8 12/09/2010 11:07 AM
Apache Performance T uning http://www.devside.net/articles/apache-performance...
Apac he will issue an extra system c all per filename c omponent to substantiate that the filename is
NOT a symlink; and more system c alls to matc h an owner.
<Directory />
Options FollowSymLinks
</Directory>
AllowOverride
S et a default 'AllowOverride None' for your filesystem. Otherwise, for a given URL to path translation,
Apac he will attempt to detec t an .htac c ess file under every direc tory level of the given path.
<Directory />
AllowOverride None
</Directory>
ExtendedStatus
If mod_status is inc luded, make sure that direc tive 'ExtendedS tatus' is set to 'Off'. Otherwise, Apac he
will issue several extra time-related system c alls on every request made.
ExtendedStatus Off
Timeout
Lower the amount of time the server will wait before failing a request.
Timeout 45
Other/Specific
Cac he all PHP pages, using S quid, and/or a PHP Ac c elerator and Enc oder applic ation, suc h as APC.
Also take a look at mod_c ac he under Apac he 2.2.
Convert/pre-render all PHP pages that do not c hange request-to-request, to static HTML pages. Use
'wget' or 'HTTrac k' to c rawl your site and perform this task automatic ally.
Pre-c ompress c ontent and pre-generate headers for static pages; send-as-is using mod_asis. Can use
'wget' or 'HTTrac k' for this task. Make sure to set z lib Compression Level to a high value (6-9). This will
take a c onsiderable amount of load off the server.
Use output buffering under PHP to generate output and serve requests without pauses.
Make sure log files are being rotated. Apac he will not handle large (2gb+) files very well.
6 of 8 12/09/2010 11:07 AM
Apache Performance T uning http://www.devside.net/articles/apache-performance...
Load Testing
A pac he H T T P s erver benc hmarking tool
httperf
T he G rinder, a J ava L oad T es ting Framework
Benchmarks
I have searc hed extensively for Apac he, lighttpd, tux, and other webserver benc hmarks. S adly, just
about every single benc hmark I c ould loc ate appeared to have been performed c ompletely without
thought, or with great bias.
Do not trust any posted benc hmarks, espec ially ones done with the 'ab' tool.
The only way to get a valid report is to perform the benc hmark yourself.
For valid results, note to test under a system with limited resourc es, and maximum resourc es. But
most importantly, c onfigure eac h httpd server applic ation for the spec ific situation.
Information
Sys tem T uning I nfo for L inux Servers
7 of 8 12/09/2010 11:07 AM
Apache Performance T uning http://www.devside.net/articles/apache-performance...
* Dev eloperSide.NET is not af f iliated with the registered trademark owners of Windows®, Apache®,
PHP, My SQL®.
Our Priv acy Policy
8 of 8 12/09/2010 11:07 AM