Apache and PHP compression (gzip/deflate encoding)
With internet speeds increasing and the users expecting fast and more responsive websites the technology we use has to adjust to these changes, when a user requests a website from a web server the server replies with the website content let’s say the website is my blog, last time I checked it was around 53,929 bytes for the home page which means the users web browser needs to download all 53,929 bytes before the website will display, only if there was a way to decrease the website size decreasing the download time for my visitors (giving them a faster loading website)….well there is.
A website can be compressed lowering the total bytes required to be downloaded by the visitor which also lowers the bandwidth usage for both the client and web server sending the content, for example my blogs homepage is around 53,929 bytes uncompressed as soon as I enable compression it goes to 9,437 bytes which is a dramatic decrease (82.5% decrease) allowing my visitors to now load my blog quicker and lower the bandwidth consumption, the only side effect really is a slight increase in CPU usage on the web server as it now has to compress the content every time there is a request (pages can be caches as static html files to increase speeds and decrease stress on the server but this is not going to be covered in this post), the only requirement on the visitors side is that the web browser they are using supports compressed content as it will need to uncompress the content to be able to view the website (all modern browsers support this feature so it shouldn’t be a problem, most server compression methods can tell if the browser doesn’t support compression and will send the data normally if no support is found). There are two methods of compression which I am going to go through today which is apaches deflate module (usually provided with a default installation of apache, some shared hosts may uninstall this module so you will not be able to use the apache method) and PHP compression, it is best to use the apache method if possible but if not the alternative will do fine.
Verifying Compression
Verifying that are website is being compressed is important there is no point doing all the changes to find out a couple of months later you did it wrong and it hasn’t been compressing anything, there are multiple methods of determining if your website is being compressed the easiest is to use a website such as http://www.gidnetwork.com/tools/gzip-test.php which allows you to quickly and easy check and also see some estimated compression results, the alternative is to view the header data send from the web server this is achieved differently in most browsers but with Firefox you can use a add-on called Live HTTP Headers to view the header data (you’re looking for a line which says Content-encoding: gzip).
Compressing Media
Most media is already stored in a compress format such as images, music and archived files and so will not benefit from any more compression (will just be a waste of CPU usage), its mainly text based files such as css, php, html and so on which will benefit from encryption.
Apache Compression (deflate)
Enabling apache deflate compression requires access to a file called .htaccess which is located in the base web directory normally public_html, this file allows us to adjust server side apache settings without needing access to the main apache configuration file (we can customize are setup for multiple websites using this file). To compress your whole website simply place the code below into your .htaccess file.
1 |
SetOutputFilter DEFLATE |
This will now compress your whole website and all the content including css, html, javascript and php content, images are not usually compressed due to them already being stored in a compressed format, if the following code didn’t work you may not be able to adjust settings using the .htaccess file or mod_deflate is not installed on the web server (you will have to use the PHP method further in this post or ask your hosting provider for help). Not everyone wants to compress all their content so you can specify the exact content to compress by using AddOutputFilterByType.
1 2 |
AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/html |
This code will now compress only HTML and CSS pages all other content javascript, xml files and such are sent uncompressed make sure to remove SetOutputFilter DEFLATE as that line will cause all content to be compressed, you have specify the MIME data types which can be found here. It’s possible to compress files by extension using the code below.
1 2 3 |
<Files *.html> SetOutputFilter DEFLATE </Files> |
This code will compress all files with the extension .html, the same code can be used to compress a specific file by changing *.html to the file name so for example if I want to compress only test.html I would use the code below.
1 2 3 |
<Files index.html> SetOutputFilter DEFLATE </Files> |
It’s possible to adjust the compression level by simply adding DeflateCompressionLevel.
1 |
DeflateCompressionLevel 9 |
This line of code can range from 1 to 9, 9 being the best compression method. By default 9 is used and there is rarely any reason to specify a compression level unless your CPU usage is high due to high website usage.
PHP Compression
It is highly recommended to use apache compression but if your unable to or unwilling its very simple to compress content using PHP, all that is required is to add the following code to the top of any PHP file needing compressed.
1 2 3 4 5 6 7 |
<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){ ob_start("ob_gzhandler"); }else{ else ob_start(); } ?> |
What this does it check if the browser requesting the website supports compression, if it does it compresses the data if not it just sends the content normally (uncompressed). This method will only compress content loaded into the PHP file so if we have a test.html page this page will not be compressed as PHP never loaded it.
Extra Reading
http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/
http://www.samaxes.com/2008/04/htaccess-gzip-and-cache-your-site-for-faster-loading-and-bandwidth-saving/
http://www.g-loaded.eu/2008/05/10/use-mod_deflate-to-compress-web-content-delivered-by-apache/