PDA

View Full Version : TIP: Easy performance tweak for Apache/PHP sites



KLB
07-17-2006, 09:12 PM
After spending the weekend tweaking and optimizing my site, I started to run out of ways to make it faster, but I read about gziping html/php files on the fly which will significantly reduce their download size and signficantly speed up the download of webpage especially on dialup connections. While there are several different server addins that can do this, if one is running PHP on Apache as a module there may not be any additions needed. All one needs to do is add the following to their .htaccess file:

php_value output_handler ob_gzhandler

This assumes that the webpages are being parsed by PHP so if the webpages are static HTML file one might need to add the following before the instruction above:
AddType application/x-httpd-php .html

It will work for any file extension that is set to be parsed by PHP. Trying to gzip CSS files might cause havoc on older browsers. This will signficantly speed up the download of sites for almost all browsers and the gzip files are only sent to browsers that can support them. I found it works great for MSIE6, Firefox and Opera. In fact based on some tests I ran, I think my users on 56K connections will see the load time of my pages go from around 20 seconds to around 11 seconds. Obviously your mileage will vary. More documentation on this can be found at:
http://us2.php.net/ob_gzhandler

You can test your site to see how this is working at: http://leknor.com/code/gziped.php

Another really useful tool for analyze and optimize one's site is at:
http://www.websiteoptimization.com/services/analyze/

We all know how important it is to make our pages load as quickly as possible and this is a really cheap trick.

ogito
07-18-2006, 12:34 AM
Thanks for the tip Ken, but what about the cpu load by gziping html/php ?

AndyH
07-18-2006, 01:29 AM
It does use a tad more CPU power.

For me, it halves my (outgoing) bandwidth usage.

KLB
07-18-2006, 07:03 AM
I have no way of measuring CPU usage. I assume it adds a slight bit of CPU overhead on both the server and the client. Unless, however, your server is running on the red line anyways, I doubt the CPU cost would cause problems. As AndyH pointed out, it will save a tremendous amount of bandwidth. It will also dramatically increase the speed at which your pages load even with the extra CPU load at both ends.

Blue Cat Buxton
07-18-2006, 02:02 PM
Thank you for this Ken.

This is one of those tips that should be in the " must do things to do when setting up your new web site" guide.

KLB
07-18-2006, 03:37 PM
This is one of those tips that should be in the " must do things to do when setting up your new web site" guide.
I agree, if I knew about this years ago, I could have saved lots of $$$ in excess bandwidth charges. :( This simple detail will also greatly improve the user expierence.

AndyH
07-18-2006, 06:47 PM
Oh and it doesn't need to be in a .htaccess, you can simply change it in php.ini.

I say this because, turning of .htaccess lookups is another performance tweek.

KLB
07-18-2006, 07:51 PM
Oh and it doesn't need to be in a .htaccess, you can simply change it in php.ini.

I say this because, turning of .htaccess lookups is another performance tweek.
I'm on a shared hosting account so I don't have access to php.ini, but I do have access to .htaccess.

Chris
07-18-2006, 08:29 PM
I've been loathe to do this because I do have processor problems... but maybe I'll give it a try. The speed increase from saved bandwidth might be more than the speed loss from increased processor usage.

Chris
07-19-2006, 07:05 AM
whoops... I don't think this'll work with the custom error page method of search engine friendly urls... the wrong http header gets sent & the browser doesn't know to unzip it.