To administer your WestHost account, please enter your
Domain Name or Server Manager Username.

Improving Apache Web Server Performance

Even to most Internet professionals, the Apache web server software can be as perplexing as it is ubiquitous. Although not as comprehensive as the official 685-page documentation, this post outlines two useful features you can enable in the httpd.conf file to improve the performance of your web server: persistent connections and gzip compression.

Important note: An error in your Apache configuration can take all of your web sites offline. Please make a backup copy of the /etc/httpd/conf/httpd.conf file before you begin making modifications. Also remember that it is necessary to restart your account to apply either of the changes described below.

Persistent Connections (Keep-Alive)

In addition to your web page itself, web browsers must also download each stylesheet, script, image, and other file referenced by your page. Historically, this meant that browsers would connect to a server, download the page, and disconnect. The browser would then initiate a new connection, download the first referenced file, and disconnect, repeating the process to fetch each additional file. As web pages became increasingly complex, they began using more files and the slow connect/download/disconnect method became noticeably time-consuming. This problem was solved with the introduction of persistent connections, as described below in the Apache documentation:

The Keep-Alive extension to HTTP/1.0 and the persistent connection feature of HTTP/1.1 provide 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.

Under the default settings, persistent connections are enabled on your account, but the KeepAliveTimeout is set to 15 seconds (near line 86 of /etc/httpd/conf/httpd.conf). This means that Apache connections will stay active for 15 seconds after visitors have downloaded (and possibly even left) the web page. Apache connections use server resources and your account has a limited number of Apache connections, so this is generally undesirable.

  • For the best performance on most websites, leave KeepAlive On but reduce the KeepAliveTimeout to 1 or 2 seconds. This allows visitors to benefit from faster downloads through persistent connections, but helps prevent Apache from overloading the server and keeping too many connections open at once.
  • In rare cases, on websites with extremely heavy traffic, turn KeepAlive Off. Persistent connections guarantee faster page loads for your visitors but come at the cost of increased resource utilization by requiring more Apache connections to remain open concurrently on the server. If you are frequently reaching the maximum number of allowed Apache connections, it is probably necessary to upgrade your hosting package, but turning off persistent connections is one final step you can take to squeeze a little bit more out of your current plan.

Gzip Compression (mod_deflate)

HTML, CSS, and many other programming languages used on the web are highly redundant, allowing them to be compressed effectively. mod_deflate, Apache’s compression engine, is installed but not enabled by default. To enable automatic gzip compression, add the following lines of code to the end of your httpd.conf file:


SetInputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/rtf
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom+xml
AddOutputFilterByType DEFLATE application/rdf+xml
AddOutputFilterByType DEFLATE application/postscript
AddOutputFilterByType DEFLATE application/msword
AddOutputFilterByType DEFLATE application/vnd.ms-excel
AddOutputFilterByType DEFLATE application/vnd.ms-powerpoint
AddOutputFilterByType DEFLATE image/svg+xml
AddType image/svg+xml .svg

After you have edited the httpd.conf file and restarted your account, test whether compression is working properly.

Persistent connections significantly improve loading multimedia-rich sites with large numbers of images and other files, while gzip compression reduces HTML, CSS, JavaScript, and other text by an average of 80 percent, but these two changes are just the beginning! Read Yahoo’s list of Best Practices for Speeding Up Your Web Site for even more ways to make your pages load faster.

This entry was posted in Technical Support, Web site Development. Bookmark the permalink. Both comments and trackbacks are currently closed.

22 Comments

  1. Posted June 9, 2009 at 10:56 am | Permalink

    Can somebody please tell me the user side and server side factors that influence website performance?

  2. Posted June 10, 2009 at 1:45 am | Permalink

    Is this something that West Host customers need to do, as well? Or this meant mainly for those running their own Apache servers? Thanks!

  3. Posted June 10, 2009 at 9:31 am | Permalink

    There are many factors that impact website performance. Nick outlined just two strategies that can be implemented to make Apache more efficient given the type of traffic a site may observe. His suggestions don’t explain performance, only how to potentially improve performance.

    Site design is a big factor in site performance. Methods used in linking, image quality and size, thumbnail creation, and dynamic content coding efficiency are all factors that impact performance. This is a very abbreviated list but more information on site optimization can be found around the Internet. Commonly we utilize sites like http://www.websiteoptimization.com/. Database structure must also be considered. Luckily, MySQL is widely utilized and heavily documented.

    Server administration and configuration is a big factor as well. Nick’s post addresses just two configuration options. There is a great deal of information available on Apache optimization.

    Neither of Nick’s suggests have to be implemented with a standard WestHost account. They are simply suggestions to improve Apache’s ability to deal with increased load due to traffic and volume as a site begins to out grow its current hosting solution.

  4. Posted June 10, 2009 at 2:07 pm | Permalink

    To clarify, “the default settings” which I speak of in this post refer to the default settings in the WestHost installation of Apache.

    Although it is by no means necessary to make these changes, either the WestHost installation of Apache or a manually compiled and installed copy of web server software can benefit from these configuration adjustments.

  5. Posted June 15, 2009 at 3:28 pm | Permalink

    This really helped me. I was having slow issues and Apache would hang with all processes opened. Now there are only 4-10 processes running at most times and the speed is much improved. My customer may give me a raise over this. Thanks for posting this article. If you have any more tips, pass them on to us. Thanks.

  6. Posted June 17, 2009 at 12:10 pm | Permalink

    Great post – thank you.

    One question; Why are these options not the West Host defaults? Are there any important downsides?

  7. Posted June 17, 2009 at 4:48 pm | Permalink

    There is no downside to a decreased KeepAliveTimeout.

    mod_deflate is a trade-off between reduced bandwidth and increased memory/CPU usage. Obviously your web server will be working harder if it needs to compress every page on the fly. In general, though, gzip is relatively fast so the performance improvements from enabling compression outweigh the costs.

  8. Definite Web Hosting
    Posted June 24, 2009 at 4:23 am | Permalink

    Thanks for the nice info. I’m definitely gonna try it. Keep well.

  9. Posted June 30, 2009 at 6:04 am | Permalink

    Nick, appreciate your help with my apache settings. Before contacting you guys (westhost) I attempted to make the changes to httpd.conf and the.htaccess file (for wordpress), which brought my site down. You mention in this blog, “Also remember that it is necessary to restart your account to apply either of the changes described below.”

    With a reseller account, I am able to restart the account via my control panel, however that did not seem to work. Does it take some time to restart/propogate the changes to the server, or does the restart have to occur on your (westhost) end?

  10. Joe
    Posted July 1, 2009 at 12:37 pm | Permalink

    Please answer Rob’s first question.

  11. Posted July 1, 2009 at 12:45 pm | Permalink

    I’m not sure what you are referring to, Jeff. There is no restart function in the Reseller Server Manager.

    Essentially, the Apache web server just needs to be restarted. You can do this however you want. The two easiest ways are either to 1) click on “Restart Account” from within the Site Manager for a specific VPS account (resold or otherwise), or 2) run the command “apachectl restart” via an SSH command-line session. Either of these two methods will cause the Apache web server to restart.

  12. Joe
    Posted July 1, 2009 at 4:38 pm | Permalink

    I prefer the third choice – run the “restart” command in SSH.

    But please do answer Rob’s first question – why aren’t these things enabled by default at this point?

  13. Posted July 1, 2009 at 5:58 pm | Permalink

    It is not WestHost’s responsibility to perform this as each site is unique. It’s cool the subject was brought to our attention. Thanks.

  14. Posted July 2, 2009 at 8:46 am | Permalink

    Nick, I was referring to the restart account feature under the domain/manager control panel. However, after editing the .htaccess file I can no longer access the control panel. I was wondering how I can restart the apache server and was not sure if restarting the “account” was one in the same?

  15. Posted July 5, 2009 at 5:26 pm | Permalink

    Yes, Jeff, clicking “Restart Account” will restart Apache and it will make all changes in httpd.conf effective immediately.

    The “Restart Account” from within Site Manager is similar running the “restart” command in SSH — it will stop all of the services running within your account (e.g., Apache, MySQL) and then restart them. Any other the three methods (clicking on “Restart Account” in Site Manager to restart all services, running the “restart” command in SSH to restart all services, or running the “apachectl restart” command in SSH to restart just the web server) will restart the Apache web server and make changes to httpd.conf take effect. There is no waiting period — the changes are effective as soon as the Apache web server starts up again.

    On the note of .htaccess files: The directives to control persistent connections must appear within the httpd.conf file, so you can’t put these in an .htaccess file. The directives to enable mod_deflate (SetInputFilter and AddOutputFilterByType), however, can be used in an .htaccess file instead if you prefer to only compress a certain directory, although I’m not sure why you’d want to do this.

  16. Susan
    Posted July 27, 2009 at 4:05 am | Permalink

    I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

    Susan

  17. Posted October 7, 2009 at 12:26 am | Permalink

    Hey very useful piece of information. I’m going to try this stuff. Thanks for the post.

  18. Posted February 13, 2010 at 5:50 pm | Permalink

    Thanks a lot for the gzip compression code. It works!

  19. Posted February 16, 2010 at 6:25 pm | Permalink

    WestHost 4.0 clients with cPanel (hosting accounts created after November 2009) can enable gzip compression right from within the cPanel Control Panel!

    Simply login to your cPanel Control Panel, then click on “Optimize Website” (under the “Software / Services” section). Choose the option to “Compress the specified MIME types”. Erase what is currently in the text box and enter the following list of MIME types instead, as one long line:

    text/html text/css text/xml text/plain text/rtf text/javascript application/x-javascript application/xml application/xhtml+xml application/rss+xml application/atom+xml application/rdf+xml application/postscript application/msword application/vnd.ms-excel application/vnd.ms-powerpoint image/svg+xml

    After making this change, click on the “Update Settings” button.

  20. Posted May 20, 2011 at 11:19 am | Permalink

    I just came across this article yesterday and I’m not sure whether or not you might still be responding to comments. My website is on WestHost 3.0. What you have above for adding to your httpd.conf file looks like a very nice, simple and elegant solution for webpages compression. But how do you take care of the problem of older browsers that can’t handle zipped webpages?

  21. Posted May 20, 2011 at 12:19 pm | Permalink

    You can use the BrowserMatch directive in the Apache configuration file to specify which browsers will and will not receive compressed versions of the site. This way you can turn off the deflate filter for those older browsers. If you are wanting to utilize this functionality, I would recommend looking through both the Sample Configurations and the Enabling Compression sections at the following site:
    http://httpd.apache.org/docs/2.0/mod/mod_deflate.html#recommended

    To get a better idea of which browsers can and cannot handle compression, the following page may also be of some use:
    http://schroepl.net/projekte/mod_gzip/browser.htm

  22. Mary
    Posted May 20, 2011 at 12:58 pm | Permalink

    Dustin – thank you for your very useful info and quick reply. And Nick – thanks again for your article.

Copyright © 1998–2014, WestHost. All rights reserved.  |  WestHost / Believe in Better Hosting. Privacy Policy