Optimizing Website Performance On Apache

There are three easy things that you can do to optimize a website’s load time on an Apache server. The first thing is to compress the web page output that is sent to the client with mod_deflate. The second thing is to enable or allow browser caching on the client’s end. The third is to enable http keep-alive requests. The following will be a brief example of how to enable these features.

Enabling output file compression

Most installations of Apache that are available through your Linux distro’s repository will include mod_deflate. This Apache module will be used to compress the file output before it is sent to the client browser. This not only saves bandwidth, but time as well. Less time will be spend downloading web page bits and more time looking at them. File compression can save anywhere from 20 to 90% of bandwidth from unnecessarily being wasted. This will not cover how to install mod_deflate, but how to “turn it on.”

To enable file output compression, add the following lines to the bottom of Apache’s main configuration file (httpd.conf). Save, restart Apache, and test.

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE application/x-httpd-eruby
AddOutputFilterByType DEFLATE text/html

Enabling browser caching headers

Browser caching is a useful tool. It allows content to be opened from a cached file on the client’s computer. This saves bandwidth since the file does not need to be downloaded again. What makes this seem like a bad idea is that things may update on the server side, but the client will still pull the local, cached version. This is where Etags and content expiration comes in handy. The Etag is a unique hash of a file that the browser will use to compare with the server to ensure that the client browser has the current version of a file. The expiration header is the EOL of a cached document. After that time, a mandatory download of a new copy of the remote file is required.

Most dynamic programing languages provide the ability to modify the output headers of the page requested. However, static documents like images, flat html, and css files do not. That is where the server side default caching rules come in handy. Most of these static documents don’t change all too often anyhow. Expiration dates of more than a month are usually fine. Upwards of a year is also okay, but never more.

Here are a few example browser headers that are used to control client side browser caching:

Response Headers
Date Wed, 06 Jul 2011 13:56:59 GMT
Server Apache/2
Content-Location Home.html
Vary negotiate,accept,Accept-Encoding
TCN choice
Last-Modified Wed, 06 Jul 2011 02:00:16 GMT
Etag "7b1b-4a75cf62fcc00;89-3f26bd17a2f00"
Accept-Ranges bytes
Cache-Control max-age=600
Expires Wed, 06 Jul 2011 14:06:59 GMT
P3P policyref="http://www.w3.org/2001/05/P3P/p3p.xml"
Connection close
Content-Type text/html; charset=utf-8

A couple of lines of code need to be added in one of three places. You can add this code to either Apache’s main configuration file (httpd.conf), each virtual host’s configuration file, or to the .htaccess file within each site’s root web folder. The code will behave the same in all three places. Open one of the three files and add the following code. It will set default caching of all documents to a week and a month long cache for jpg, gif, png, css, or js file types.

<ifmodule mod_expires.c>
  ExpiresActive on
  ExpiresDefault "access plus 1 week"
  <filesmatch "\.(jpg|gif|png|css|js)$">
    ExpiresDefault "access plus 6 months"

Enable keep-alives

Keeps-alives are also a very useful tool. Keep-alives allow a browser to make multiple requests for web page components during a single connection instead of a single connection for each part. One connection per file, graphic, script, etc. This also cuts down bandwidth overhead and reconnect times as well. To enable, open Apache’s main configuration file and locate the line containing “KeepAlive Off” and change to On. Save, restart, and test your website.

Your Apache server is now able to server compressed content over fewer TCP connections to client’s in less time. Don’t forget that the bandwidth savings is also compounded since the browser is caching some rarely updated content. Don’t worry about skewed analytics for your website. Much older analytic solutions may have trouble with the caching, but most modern solutions will not allow caching so that is has proper results.