Support

Blog

Flattr this!

When I get time, I go through the logs and check out how the servers are doing.

One thing that I haven’t really done recently is to optimize the way things are configured.
The typical solution in most scenario’s is to throw faster hardware at things (something we do when necessary!), but sometimes a few minutes configuration can help speed things up tremendously.

Below are some tips for optimizing apache a little.

I’m going to assume you’re running apache2.  If not, why not?

First thing to do is to enable 2 modules.  Mod_Deflate and Mod_Expires

a2enmod deflate

a2enmod expires

Mod_Deflate is a nice Apache module that allows us to gzip files  when we send them to the browser.  This helps load times quite nicely – as network traffic is reduced.

In our systems Apache is over in /etc/apache2/
The module configuration is done in mods-enabled

First up, we’ll configure Mod_Deflate.

pico /etc/apache2/mods-enabled/deflate.conf

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
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 image/svg+xml

DeflateCompressionLevel 9

BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch bMSIEs(7|8) !no-gzip !gzip-only-text/html

SetEnvIf User-Agent “.*MSIE.*” nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0

</IfModule>

I’ll go over what we’ve done below;

If you examine the file above, you’ll see that the first thing we do, is to check if mod_deflate is loaded with the IfModule directive.

We then setup compression for various file types –

AddOutputFilterByType DEFLATE  ….

While we can compress everything, and just exclude certain files, its usually safer to specify whats compressed.

We then set compression to maximum – computers these days have ample excess CPU, so its not really a concern anymore.

DeflateCompressionLevel 9

The next lines are to specify exceptions to the compression rules.  Ancient versions of Netscape don’t work so well with compression, so we disable it.  Internet Explorer also has a few issues also, so we setup an appropriate rule.

BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch bMSIEs(7|8) !no-gzip !gzip-only-text/html

The last line is to force compatibility with older IE versions.  IE is a bit buggy, as we’ve noted.

BrowserMatch bMSIEs(7|8) !no-gzip !gzip-only-text/html

We’ll now check to see that we haven’t made any mistakes, and Apache2 runs ok.

apache2ctl configtest

If you see any errors, fix and retest.

Restart apache2

apache2ctl restart

Load up FireFox.  You’ll need to have some plugins installed to test.
Go get FireBug, and YSlow, install and come back.

Done?

Ok, now open a page from the site, and click YSlow.

You should see now that the css, js, and php files are compressed with GZip.

Our next step is to setup Mod_Expires

pico /etc/apache2/mods-enabled/expires.conf

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif A21600
ExpiresByType image/jpeg A21600
ExpiresByType text/css A21600
ExpiresByType application/x-javascript A21600
</IfModule>

Essentially, what we’re doing above is turning on Content Expiry.  Content Expiry is the time that a browser will cache a file for before it reloads it.  For your average site things like images, javascript and css don’t change that much, so we can tell the browser to explicitly cache them.

The number next to the Expiry is the time that the file is valid for in seconds.
(after that point, the browser should reload the file from the server).

In our setup above, we’ve set that images and css and javascript files should be kept for 6 hours.
A21600 means – after 6hrs (60seconds x 60minutes x 6 = 21600)

I’d suggest using a low, but similar time span to avoid caching issues.  This would mostly affect website developers, rather than end users though, as they’re more likely to upload and change files frequently during testing.

Restart apache, and check again in YSlow / Firebug to see that the images and CSS are being cached.  If they are, congratulations, you’re done.

You should see a nice improvement on site load times, and things should feel snappier to end users.
It will also reduce your server load, which is a nice benefit!

Archives

Categories

Tags

PHOTOSTREAM