Improving Apache Web Server Performance

June 9th, 2009 by

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/
AddOutputFilterByType DEFLATE application/
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.

  • Share this post

Customizing Zen Cart, Part 2

Backups Part 1: Creating a Backup of your Web Site


Jason Engraving
# 9th June, 2009

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

Dave Snyder
# 10th June, 2009

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

Dan Ostler
# 10th June, 2009

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

Nick Venturella
# 10th June, 2009

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.

Dan Wise
# 15th June, 2009

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.

# 17th June, 2009

Great post – thank you.

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

Nick Venturella
# 17th June, 2009

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.

Definite Web Hosting
# 24th June, 2009

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

Jeff C
# 30th June, 2009

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?

# 1st July, 2009

Please answer Rob’s first question.

Nick Venturella
# 1st July, 2009

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.

# 1st July, 2009

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?

Steve D
# 1st July, 2009

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.

Jeff C
# 2nd July, 2009

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?

Nick Venturella
# 5th July, 2009

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.

# 27th July, 2009

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.


# 7th October, 2009

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

# 13th February, 2010

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

Nick Venturella
# 16th February, 2010

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/ application/ image/svg+xml

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

# 20th May, 2011

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?

Dustin Olson
# 20th May, 2011

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:

To get a better idea of which browsers can and cannot handle compression, the following page may also be of some use:

# 20th May, 2011

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