Sunteți pe pagina 1din 8

Apache Performance T uning http://www.devside.net/articles/apache-performance...

Tools Developers Use!™

Home WampDev eloper Dow nloads Contacts

Apache Performance Tuning


DeveloperSide.NET Artic les

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].

General [in order of importance]

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...

prefork [default MPM for Apache 2.0 and 1.3]:


Apa che 1.3-ba se d.
Multiple proce sse s, 1 thre a d pe r proce ss, proce sse s ha ndle re que sts.
Use d for se curity a nd sta bility.
Ha s highe r m e m o ry consum ption a nd lowe r pe rform a nce ove r the ne we r Apa che
2.0-ba se d thre a de d MP Ms.

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

MaxClients, for prefork MPM


MaxClients sets a limit on the number of simultaneous c onnec tions/requests that will be served.

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.

MaxClients ≈ (RAM - size_all_other_processes)/(size_apache_process)

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:

Syste m : VP S (Virtua l P riva te Se rve r), C e ntO S 4.4, with 128MB R AM


Apa che : v2.0, m pm _pre fo rk , m o d_php, m od_re write , m o d_ssl, a nd othe r m odule s
O the r Se rvice s: MySQ L, Bind, Se ndMa il
R e porte d Syste m Me m ory: 120MB
R e porte d httpd pro ce ss size : 7-13MB
Assum e d m e m o ry a va ila ble to Apa che : 90MB

Optimal settings:

Sta rtSe rve rs 5


MinSpa re Se rve rs 5
Ma x Spa re Se rve rs 10
Se rve rLim it 15
Ma x C lie nts 15
Ma x R e que stsP e rC hild 2000

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.

ThreadsPerChild, for winnt MPM


On the Windows side, the only useful direc tive is ThreadsPerChild, whic h is usually set to a value of
250 [defaults to 64 without a value]. If you expec t more, or less, c onc urrent c onnec tions/requests,
set this direc tive appropriately. Chec k proc ess siz e with Task Manager, under different values and

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 .

Requests vs. Client Connections


On any given c onnec tion, to load a page, a c lient may request many URLs: page, site c ss files,
javasc ript files, image files, etc .

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].

Separate Static and Dynamic Content


Use separate servers for static and dynamic c ontent. Apac he proc esses serving dynamic c ontent will
c arry overhead and swell to the siz e of the c ontent being served, never dec reasing in siz e. Eac h
proc ess will inc ur the siz e of any loaded PHP or Perl libraries. A 6MB-30MB proc ess siz e [or 10% of
server's memory] is not unusual, and bec omes a waist of resourc es for serving static c ontent.

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:

lighttpd [ha s e x pe rim e nta l win32 builds]


tux [pa tche d into R e dHa t, runs inside the Linux k e rne l a nd is a t the top of the
cha rts in pe rform a nce ]

The S erver handling the static c ontent goes up front.

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.

LoadModule deflate_module modules/mod_deflate.so


<Location />
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
application/x-javascript

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.

Note that this c an also be implemented with mod_headers.

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

worker and winnt MPMs:

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.

Avoid c ontent negotiation for faster response times.

Make sure log files are being rotated. Apac he will not handle large (2gb+) files very well.

Gain a signific ant performanc e improvement by using S S L session c ac he.

Outsourc e your images to Amaz on's S imple S torage S ervic e (S 3).

6 of 8 12/09/2010 11:07 AM
Apache Performance T uning http://www.devside.net/articles/apache-performance...

Measuring Web Server 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

In the Spotlight Developers++ Newsletter Signup


Developing and Hosting WampDeveloper is more than Enter y our e-mail address:

P HP +MySQL based websites just a Windows based


and applications has never Apache|MySQL|P HP P latform.
Subscribe
been simpler. WampDeveloper Suitable for novices and
provides a P latform & professionals alike, a complete
Framework on which to create Hosting Solution is provided
standards-based C SS, XSLT, that is capable of creating and
and XHTML sites running on deploying websites and web
P HP and MySQL. Save your applications both locally and on
time, focusing on core business. the internet. C reate, test,
Reduce complexity and publish and maintain your
maximize your productivity. websites in-house, on your own
Howto Install WordP ress server, where you are in full
control.

Copy right © 2003-2010 Dev eloperSide.NET | All Rights Reserv ed


Our Blog | Dev eloperSide.NET Forums | Our Archiv es |

7 of 8 12/09/2010 11:07 AM
Apache Performance T uning http://www.devside.net/articles/apache-performance...

DeveloperSide.NET™, Tools Developers Use!™ and WampDeveloper™ are trademarks of


Dev eloperSide.NET.

* 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

S-ar putea să vă placă și