Apr 02 2008

Using GZip on Wordpress 2.5

Published by Lord TCT at 11:54 am under Wordpress

Wordpress

Gzip compression is among the missing features in Wordpress 2.5. To restore the functionality, you have but not limited to the following choice:

1. Application wide using Apache/Litespeed .htaccess

2. Gzip-ing via Index.php
3. Site wide using your webserver’s modules
4. PHP wide using php.ini

1. Application Wide Using .htaccess

Edit Wordpress’s .htaccess or create it if it isn’t there and insert the following code in:

php_value output_handler ob_gzhandler

Not all hosts will allow .htaccess though…

Pros: Permanent solution. Triggers PHP to compress anything PHP on your application in just one line.

Cons: May cause high CPU load and may break certain pages on certain browsers. Does not compress other non-PHP files. Not always supported by your web host.

2. Gzip-ing via Index.php
(Suggested by Matej)

Add the following line on the very top of /index.php

ob_start(”ob_gzhandler”);

This can be repeated on all the PHP files that you want to compress.

Pros: Lower CPU utilisation since only pages served via /index.php is compressed. Does not affect other PHP pages. Easy to set up. Works on any webservers, be it shared, virtual or dedicated hosting.

Cons: Does not compress non-PHP files and pages not bearing the code above. May cause maintenance headache.

3. Using your webserver’s modules

Life is VERY easy on Litespeed:

Login to your Litespeed administration console, edit your blog virtual host:

Save the settings, restart the virtual host and you’re done!

For Apache’s mod_gzip, please visit; http://www.bluestream.org/Networking/gzip.htm

For Lighttpd, you will need to compile with gzip enabled (./configure –with-gzip), then edit the configuration file:

server.modules = (… “mod_compress” …)
compress.cache-dir = “/tmp/”
compress.filetype = (”text/html”, “text/css”, “text/javascript”, “text/plain”, “application/x-php”)

Restart Lighttpd and verify that Gzip has been set up correctly.

Again, not everyone has access to these luxury settings, especially shared hosters.

Pros: Instantly compress all static and dynamic contents of predefined MIME types. Effectively reduce long Javascript serving time (especially JQuery, Spry etc)

Cons: May cause high CPU load spikes, but may stabilise after a while. MIght break certain files on certain browsers. Poor performance reported on Apache 1.3.x.

4. PHP.ini

Edit it such that:

zlib.output_compression_level = <any value between 1 to 9, with 1 being lowest compression, recommended 6>

Pros: Same as method 1 but this is system wide (affects all Virtual Hosts, unless disabled through .htaccess)

Cons: Same as method 1. Not available to shared hosts usually.

Verifying that Gzip has been done correctly

My favourite tool is Rex Swain’s HTTP Viewer. Just plug in the URL of your page, enter “gzip” (without quotation marks) in Accept-Encoding and submit.

This is how a Gzipped page raw output might look like in HTTP Viewer:

ad2(CR)(LF)
(1F,8B,08,00,00,00,00,00,00,FF,EC)Ymo(DB,C8,11,FE,1C,03,F9,0F)s(LF)
(10,D8)=(93,B4,EC)s(E2)$(B2,AF,8E,E2)k(D2,C6,89,11)+(97,F4)S(B1)“W(E2,C6)$(97,B7,BB,B4,AC)+(FA)_(FC)[(FC,CB,FA,CC,F2)E(B2,AD,CB)K(9B,16,CD,A1)A·K$wfvf(F6)yf(86,83,EF,9E,BD,1E,8E,FE)zrD(A9,CB)3:y(FB,F4,E5,8B)!(F5,82)(z(B7)3(8C,A2)g(A3)g(F4,FE,F9,E8,F8)%(F5,C3)-(1A,19)QX(E5,94).D(16)EG(AF)z(D4)K(9D)+(1F)G(D1)l6(0B)g;(A1)6(D3)h(F4)&(BA)`Y}^(DC)|(CR)
(DC,D2,CA)0qI(EF,E0,EE,DA,C0)k(BC,C8,B3,C2,EE,AF,90,D3,7F,F4,E8)Q(BD,BC)~X(8A,84)J(A3)‘*(93,DD,E3,D3,BC,9C)z(A5,17,93)(EA,F3)sw(06)N(B9)L(1E,8C)RIO(95,B3,B4)(CR)
(BB,07)Q}(91)o(E7,D2)(HT)(EC,D5,95,81,FC,A5)R(E7,FB,BD,A1).(9C),\0(9A,97,B2)Gq(FD)k(BF,E7,E4,85,8B,D8,C2)(14,A7,C2)X(E9,F6)+7(HT)(F6)z(14)y5^N!r(18)3(95,85)4(C2)i(B3,B4,FC,9D)6(C9,89,91,96)(CR)

And the non-Gzipped page would look something like:

<!DOCTYPE·html·PUBLIC·”-//W3C//DTD·XHTML·1.0·Transitional//EN”·”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>(CR)(LF)
<html·xmlns=”http://www.w3.org/1999/xhtml”>(CR)(LF)
<head·profile=”http://gmpg.org/xfn/1″>(CR)(LF)

The reason to use GZip for your wordpress blog is very obviously shown in the graph below:

Gzip saves more than 60% of your bandwidth on average! This means your pages might load (assuming that page download time IS a constrain factor) on average 60% faster! 

10 Responses to “Using GZip on Wordpress 2.5”

  1. michaelfooon 03 Apr 2008 at 11:23 pm

    Hey, good work in your server & WP performance tweaking!

    My goodness, I couldn’t believe this is the performance on WP, looks like many bloggers out there would be interested to get your consultation service :D

  2. Lord TCTon 04 Apr 2008 at 12:49 am

    Thanks for the encouragement. I’ll start posting WP tweaking tips soon :)

  3. Matejon 13 Apr 2008 at 10:24 pm

    What about the obvious solution?

    putting the following line in the beggining of index.php
    ob_start(”ob_gzhandler”);

    Matej

  4. Lord TCTon 14 Apr 2008 at 5:40 am

    Hi Matej,

    Doing so would only enable gzip for pages served via index.php and will not enable gzip on the admin pages.

    Cheers

  5. Marcon 17 Apr 2008 at 4:55 am

    I agree with Matej. Enabling on the page via php is best. Does it really matter if admin pages are gzipped. Get real. If you want to gzip other pages it’s no big deal to insert on a few more. I use this method.

  6. Lord TCTon 17 Apr 2008 at 5:26 am

    Marc,

    It’s a matter of personal preference. If your bandwidth is narrow and connection slow, it wouldn’t hurt to have all the pages gzipped would it?

  7. Bobon 19 Apr 2008 at 4:07 pm

    I’m trying to set this up on hostgator. Can anyone help me out?

  8. Lord TCTon 19 Apr 2008 at 5:19 pm

    what platform are they on? I think solution No.2 will most probably work out for you

  9. Carlton Baleon 27 Apr 2008 at 3:20 pm

    Thanks for the tip! I was surprised that this option disappeared from WordPress 2.5 and appreciate the info on how to re-enable it.

    I was using the CSS compress plugin to gzip all of the images and CSS files together, but the .htaccess method you show causes this plugin not to work. But this plugin gave a small benefit compared what you have listed. Thanks for the sharing the info!

  10. Lord TCTon 27 Apr 2008 at 3:46 pm

    Hi Carlton,

    AFAIK, gzip-ing images will not help…at all. It will only put unnecessary load on your server. For CSS and JScript, u can just gzip it before hand. IE7 and Firefox will happily accept pre-gzipped CSS and Jscript.

    Or a more radical approach would be to let php parse the CSS and JS file (and hence compressing it). This is done by adding:

    Addhandler php-script .js .css

    in your .htaccess file

Trackback URI | Comments RSS

Leave a Reply

Anti-spam challenge:
Sorry but you forced me to do this!