Apache Optimization

All the important configuration options are stored by Apache in a config file called httpd.conf that is located at /usr/local/apache/conf/httpd.conf. We will start by opening this file in your favorite text editor. For example:

  • vi /usr/local/apache/conf/httpd.conf

MaxClients

Total number of concurrent connections.

Locate it in the configuration file. This should be set to a reasonable value. I suggest using this formula to determine the right value for your server.

  • MaxClients = 150 x RAM (GB)

So for example if you have 2 GB or RAM set this value to 300.
There is no reason for you to set it any higher unless you have a specific problem with this value. A high value can lead to a complete server hang in case of a DOS attack. A value too low can create timeout problems for your clients if the limit is reached.

ServerLimit

This value should be same as MaxClients

  • ServerLimit = 150 x RAM (GB)

MinSpareServers and MaxSpareServers

MaxSpareServers and MinSpareServers control how many spare (unused) child-processes Apache will keep alive while waiting for more requests to put them to use. Each child-process consumes resources, so having MaxSpareServers set too high can cause resource problems. On the other hand, if the number of unused servers drops below MinSpareServers, Apache will fork (an expensive operation) new child-processes until MinSpareServers is satisfied.

Leave those values to:

  • MinSpareServers 5
  • MaxSpareServers 10

If you have more them 2 GB of RAM and you run a resource intensive website consider increasing MaxSpareServers.

MaxRequestsPerChild

Controls the number of request the a child serves before the child is killed. This should not be set too low as it will put an unnecessary load on the apache server to recreate the child. I suggest setting it to:

  • MaxRequestsPerChild 1000 for 1 GB RAM

10,000 for 2 GB and 0 for more than 2 GB RAM

KeepAlive and MaxKeepAliveRequests

KeepAlive provides long-lived HTTP sessions which allow multiple requests to be sent over the same TCP connection. In some cases this has been shown to result in an almost 50% speedup in latency times for HTML documents with many images, but having keepalive on is also a resource intensive setting.

Here comes the big question: To KeepAlive or not to KeepAlive?

Well the opinions are mixed here, some say to KeepAlive some say not to.

  • KeepAlive off

If you want to hear my option I would say NOT to KeepAlive if you are running a shared hosting business or if you want to get the most out of your hardware. You should KeepAlive only if the loading time of your pages is the most important factor in your business and you have the money to invest in a more powerful hardware. If you decide to KeepAlive I suggest you set MaxKeepAliveRequest low to something like 2 seconds.

StartServers

Sets the number of child server processes created on startup. This setting depends greatly on the type of webserver you run. If you run low traffic websites on that server set it low to something like 5. If you have resource intensive websites on that server you should set it close to MaxClients.

  • StartServers 5

Timeout

The amount of time Apache will wait for three things: the total amount of time it takes to receive a GET request, The amount of time between receipt of TCP packets on a POST or PUT request, the amount of time between ACKs on transmissions of TCP packets in responses.

The default value is 300. You should set time to something a bit lower. A setting of 150 is probably ok. This will also help in case of small DOS attacks like to ones targeting some phpBB forums. Do NOT set it any lower then 10 as your users will start having timeout problems.

  • Timeout 150

After you have done all the necessary changes you can go ahead and restart Apache.

There is an extra step that you have to do so that the changes that you done to httpd.conf aren’t lost when a recompile is done.

To also save the changes in the database you will have to run:

/usr/local/cpanel/bin/apache_conf_distiller –update

You can check to see if the changes were accepted and will not be discarded at the next apache recompile by running

/usr/local/cpanel/bin/build_apache_conf

Sample values:

MinSpareServers 5
MaxSpareServers 10
ServerLimit 600
MaxClients 600
MaxRequestsPerChild 0
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 3

Timeout 30

20 comments on “Apache Optimization
  1. GhOsTi says:

    good post man¡¡¡ but the timeout not is good, i have timeout 30 in my servers, and its good fine

    Like

  2. Barbara27 says:

    Let me say this, Thank you for this very informative website. ,

    Like

  3. Incomplete information………still nice try

    Like

  4. Thank you for sharing a good idea about optimize Apache. Can you suggest any MySQL tuning ?

    Like

  5. Dalecosp says:

    For MySQL optimization, look into Major Hayden’s “mysqltuner”, a Perl script, from http://www.mysqltuner.com. It’s helped me a good deal with understanding how improving ‘my.cnf’ might help our DB-intensive project.

    Like

  6. Sudhakar Nunn says:

    Nice and very useful

    Like

  7. Could you please explain about CGI and Fast CGI, Does it has effect on Apache performance ?

    Like

  8. Random Surfer says:

    It appears you are confusing MaxKeepAliveRequests with KeepAliveTimeout, better correct that apparent mistake…

    Like

  9. [...] are lots of websites that talks about optimization of Apache HTTP server. To summarize, the following are the [...]

    Like

  10. [...] Apache Optimization: http://linuxstuffs.wordpress.com/apache-optimization/ [...]

    Like

  11. Tim Gurske says:

    Good info – thanks!

    Like

  12. Nik says:

    I say that if you if you set 150 per GB RAM and you haven’t implement any cache/Ngnix/Varnish etc your site will be down in a matter of seconds after a few people get to your website.

    Like

  13. manish says:

    pls reply me how We control the ‘amount of’ connections which apache accepts at a time?????

    Like

  14. Prashant.. says:

    very very useful blog.. I was looking for this since last 2 years…

    Like

  15. EmptySD says:

    Good explanation! Finally understand some things and made better configuration of apache. Mine cpu is finally used and everything is faster about 50%

    Like

  16. Deven says:

    Yes It was helpful, YET a small error i believe if you change asap, just before the main head ‘StartServer’ their is a like “to KeepAlive I suggest you set MaxKeepAliveRequest low to something like 2 seconds.”, Here i believe u mean KeepAliveTimeout 2 and NOT MaxKeepAliveRequest ?
    Help me if i am wrong.. :)

    Like

  17. Hmm it looks like your site ate my first comment (it was
    extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog.

    I as well am an aspiring blog blogger but I’m still new to everything. Do you have any tips for beginner blog writers? I’d really
    appreciate it.

    Like

  18. Shamal says:

    Thank you for this great info, i had many issues tweaking apache with my 4GB Ram server really help me a lot. is there any post about tweaking mysql

    Thank you

    Like

  19. Mohamamd Kazim Hanifi says:

    Nice post

    Like

  20. sahiljet786 says:

    Nice Post Sir.
    Could you please guide Mysql Server optimization ?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog Stats
  • 119,695 hits
Follow

Get every new post delivered to your Inbox.

Join 209 other followers

%d bloggers like this: