Speed up your Website

Posted by reto on 11 February, 2006 13:05

Compressed Websites are fasterAfter reading the discussion about php-based gzip compression of pages LifeType is serving, I thought I should dive deeper into the server based compression possibilities of websites. Read on for a short comparison between compression with PHP and compression with Apache2.


Compression with PHP:
One way to compress all your php pages is done by including the following code on the first line of all your pages.
<?php ob_start("ob_gzhandler"); ?>
but if unsure you should test if compression is already enabled via htaccess/php.ini because the output_handler needs to be empty if output_compression is activated - thanks Mark! :)
if ( !(@ini_get('zlib.output_compression') == '1' || 
    @ini_get('output_handler') == 'ob_gzhandler' || 
    @version_compare(PHP_VERSION, '4.2.0') == -1) )
{
    ob_start('ob_gzhandler');
}
This is of course not very handy if you have more than one entry point to your website (i.e. not every request is processed by index.php).
Another way is to activate the compression via .htaccess or directly in your php.ini (if you have access to it). This line would go in your .htaccess:

php_flag zlib.output_compression On

The documentation on php.net and several comments provide even some more fancy functions to handle compression.

Compression with Apache2:
Apache2 (which I'm using on my server) is capable of compressing all content before delivery. And because Apache is actually communicating with clients I thought it's best to let it do the task.
The Apache Documentation on mod_deflate has a handy example to work with, but be aware that you need mod_headers for it to work properly.

That's why I ended up with a slitly modified configuration:
# using mod Deflate
<Location />
  <IfModule mod_deflate.c>
        # Insert filter
        SetOutputFilter DEFLATE

        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html

        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4.0[678] no-gzip

        # MSIE masquerades as Netscape, but it is fine
        BrowserMatch bMSIE !no-gzip !gzip-only-text/html

        # Don't compress images and files that are already compressed
        SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png|rar|zip|gz|tar|exe)$ no-gz

  <IfModule mod_headers.c>
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
  </IfModule>
  </IfModule>
</Location>
Test your Speedimprovements:
You may go and test the speedimprovements for your website. I used PipeBoost but there are several other services online, providing you with the same service.

My test results on an uncompressed file with 7204 Bytes resulted in:
2135 Bytes with Apache mod_deflate
2141 Bytes with php gzip enabled

Quite a potential for speed improvement if you ask me ;)

BTW: If you are changing the apache config I recomment you test your new setup with apache2ctl configtest or apachectl configtest respectively to make sure everything will restart just fine. (actually apache2ctl restart will do a config check before restarting, too.)

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Related Articles
Poor Man's Website Monitor
Can you see the difference?
Getting Naked on the 5th of April
IE7 - New Hope for Standards Compliant Webdesign
SpamPoison