Home      Technologies      Contact Us  
Date: Mon Oct 07, 2002 12:33 am
Topic: Article 2: Site optimization
Welcome to my 2nd article on PHP4.COM..... This article is going to focus on site optimization, 
how to make things load faster, take less bandwidth, and alleviate bandwidth/connection 
bottlenecks from your primary web server. This article is more of a system design/deployment
article.. Whereas the next will be all about programming style. Doing all of these elements 
correctly will help deliver a good product/site.
Let's start with something basic... Server side compression.. Now some people don't know that 
most newer browsers support gzip compresses pages. When the browser requests the page 
from the server, it also sends with the request a list of browser capabilities. If it supports 
gzip, the server if configured correctly, can generate the page (in PHP), compress it on the fly 
and transfer the compressed file down to the client browser. The browser then uncompresses 
the file and renders the HTML. This can be very effective in some cases dropping the actual 
data transferred significantly. In addition, the webpage often loads much faster due to the 
time it takes to download the HTML, in most cases if you add up (page generation time by 
PHP + compression time by mod_Gzip + compressed file download time), it is less than just 
the (page generation time + uncompressed file download). 
Take a look at some snippets from my log:
[06/Oct/2002:16:57:19 -0400] "GET / HTTP/1.1" 200 2231
mod_gzip: DECHUNK:OK In:7989 Out:2231:73pct.
[06/Oct/2002:16:27:45 -0400] "GET /page1.php HTTP/1.1" 200 4926
mod_gzip: DECHUNK:OK In:19243 Out:4926:75pct.
[06/Oct/2002:16:29:44 -0400] "GET / HTTP/1.1" 200 2797
DECHUNK:OK In:14805 Out:2797:82pct.
[06/Oct/2002:16:22:31 -0400] "GET /javascript.php HTTP/1.1" 200 3813
mod_gzip: DECLINED:EXCLUDED In:0 Out:0:0pct.
[02/Oct/2002:21:50:29 -0400] "GET /imagesen/bg.gif HTTP/1.1" 200 78 "-"
mod_gzip: DECLINED:EXCLUDED In:0 Out:0:0pct.
-----
Now you can see there the 3 pages got processed by the mod_Gzip compression module for 
apache. With very good results in compression sizes (73-82% smaller) than the original HTML 
file.
With the last 2 examples, I have configured apache to ignore and not compress certain files 
such as the JavaScript include, it appears the browser cannot handle this from my testing, 
also it ignores all graphic files. It is possible to compress them and save some space, but 
I have already optimized the images using other software.
-----
Just before we get into installing the module, note, that with the latest PHP 4.2.x you can 
configure php.ini, and PHP will compress it's own output stream. I have yet to play with this 
new feature as the apache module have been working so perfect thus far. For more info on 
using built in PHP page compression visit (http://www.php.net/manual/en/printwn/ref.zlib.php) 
Also I'll make a note that if you are running Apache 2.x it's built in, so you just need to 
make sure you compile it with "--enable-deflate" ..
Let's start with the basics of installing this module.. You can get the module at:
http://www.remotecommunications.com/apache/mod_gzip/
And of course download apache at:
http://www.apache.org/dist/httpd/
And PHP at:
http://ca.php.net/downloads.php
-----
Now.. I always upgrade apache/php/etc. by compiling it from all the sources.. This way you 
can customize things a bit.. perhaps optimize the binary slightly, instead of getting a 
precompiled/RPM binary..
Sorry to anyone on the win32 platform.. I don't have much experience on that platform 
and would never consider running with it for a production environment.. I'd suggest looking 
at one of the *NIX's... Let's begin.. Running under Linux:
## First download all the required files
root@insomnia:/usr/src/phpupgrade#> 
wget http://www.remotecommunications.com/apache/mod_gzip/src/1.3.19.1a/mod_gzip.c.gz
root@insomnia:/usr/src/phpupgrade#> 
wget http://www.apache.org/dist/httpd/apache_1.3.27.tar.gz
root@insomnia:/usr/src/phpupgrade#> wget 
"http://ca.php.net/do_download.php?mr=http%3A%2F%2Fus.php.net%2F&df=php-4.2.3.tar.gz"
## Then uncompress all the files
root@insomnia:/usr/src/phpupgrade# tar xvzf apache_1.3.27.tar.gz
root@insomnia:/usr/src/phpupgrade# tar xvzf php-4.2.3.tar.gz
root@insomnia:/usr/src/phpupgrade# gzip -d mod_gzip.c.gz
## Initially configure apache
root@insomnia:/usr/src/phpupgrade# cd apache_1.3.27
root@insomnia:/usr/src/phpupgrade/apache_1.3.27# 
./configure --prefix=/apache --enable-module=headers --enable-module=expires 
--enable-module=rewrite --add-module=../mod_gzip.c
root@insomnia:/usr/src/phpupgrade/apache_1.3.27# cd ../php-4.2.3
## Configure PHP
root@insomnia:/usr/src/phpupgrade/php-4.2.3# ./configure --with-apache=../apache_1.3.27 
--with-mysql --with-config-file-path=/apache/ --enable-ftp --disable-session --disable-xml
root@insomnia:/usr/src/phpupgrade/php-4.2.3# make
root@insomnia:/usr/src/phpupgrade/php-4.2.3# make install
## Now integrate apache & PHP
root@insomnia:/usr/src/phpupgrade/php-4.2.3# cd ../apache_1.3.27
root@insomnia:/usr/src/phpupgrade/apache_1.3.27# ./configure --prefix=/apache 
--enable-module=headers --enable-module=expires --enable-module=rewrite 
--add-module=../mod_gzip.c --activate-module=src/modules/php4/libphp4.a
## Compile final binary
root@insomnia:/usr/src/phpupgrade/apache_1.3.27# make
root@insomnia:/usr/src/phpupgrade/apache_1.3.27# make install
root@insomnia:/usr/src/phpupgrade/apache_1.3.27# cd /apache
root@insomnia:/apache# pico conf/httpd.conf
## Now add the following config to the top of httpd.conf (apache config)
## MOD GZIP
mod_gzip_on yes
mod_gzip_item_include file \.htm$
mod_gzip_item_include file \.html$
mod_gzip_item_include file \.php$
mod_gzip_item_include mime text/.*
mod_gzip_item_include mime httpd/unix-directory
mod_gzip_maximum_file_size 1200000
mod_gzip_dechunk yes
mod_gzip_temp_dir /tmp
mod_gzip_keep_workfiles No
-----
Now fire up apache:
/apache/bin/apachectl start
And you should be all set! ...
-----
Now let's move onto suggestion #2 about optimizing sites/bandwidth. Remote 
linking of images. This idea is quite simple, your webpage(s) most likely require 
images to make them look better.. Those images consume a lot of bandwidth on 
high sites.. So if possible you need to offload them. Offloading them to 3rd party servers.
In this 1 example, let's say you have a hosted website somewhere, but you have 
transfer limitations (most web hosts have these and they are between 1-5GB/month).. 
Now if you can find somewhere free or very cheap to host the images, you can link 
to them there... But how to do it very easily? Just add a few more things to your website 
config file:
Code:
<?
// Site config file
$dbserver = "localhost";
$dbuser = "www";
$dbpass = "Secret";
// Image location settings
$IMGROOT = "http://freeserver.com/path/images/";
//$IMGROOT= "http://www.mysite.com/images/";
?>
Note that there are 2 IMGROOT's (1 commented out).. that's because if for some 
reason this 3rd party server is down/unavailable or you cannot host the files there 
freely anymore you can just switch those 2 variables in the site config, 
and all the graphics get downloaded from the local server again.
Then in your HTML code you use:
Code:
here's a pic of me:
<img src="<?= $IMGROOT ?>mypic.gif">
-----
So it's a very basic config but works very well.. Plus there is no limit to where you 
can take this type of design. In one of my systems, this entire process is controlled 
by a mySQL database. I list all the servers/locations that the site images are available 
to be linked to, the server status (online/offline), and also (Very cool) the site weight. 
That is you can weight all the servers as a 1, and the startup file for the site will 
randomly select one of the active servers and use it.. but if you set the weight
of a particular server(s) higher than the others, there is much more of a chance it 
will be randomly selected, so it will typically receive that much more requests.
This type of weight selection is particularly used on hardware/software load balancer 
that have clusters of web servers behind them, some of then with varying CPU power, 
you cannot realistically send the same amount of requests to a P2-400MHZ server 
as a DUAL P3/1000 server.. You'd end up with 1 crashed server.
-----
I'm going to end things on that note... Hopefully this article will get people to at 
least take a look at or install mod_gzip.. Or even better yet you could upgrade to 
Apache 2.x which has it built right in! A lot of people/programmers/etc. have to come 
up with unique ways to make things work, without spending tons of cash on a very 
expensive industrial solution.. And that's where it gets interesting.. 
Cost effective solutions that work well in the real world.
Latz.
	
 
Managed With Tymbrel