Enable server compression. Faster speeds, lower bandwidth usage

Enable server compression. Faster speeds, lower bandwidth usage
Enable server compression. Faster speeds, lower bandwidth usage 5.0 out of 5 based on 2 votes.
There are many reasons to enable server-side data compression and none not to take advantage of such a good deal. Processors are getting faster and will continue to multiply in power but bandwidth is still somewhat valuable and will continue to be, at least for the foreseeable future.

So, beside the obvious improvement in page load speeds your site visitors get, you will be saving tons of bandwidth in exchange for some processor power. A reduction of 40 to 85% is a good deal for sure!

Here's how to enable gzip deflate, on a server running Apache 2.xx and WHM. This isn't an account specific setting and enables compression for all accounts on the server.

The boring part is the recompilation of Apache and PHP with mod_deflate module on. If you're unsure or unwilling to proceed, your hosting company should provide that little support for your VPS or dedicated server account. If not, ditch them already.

A - Skip this step if your server has mod_deflate already set to ON. You can find out by using the good old phpinfo.php script.

  1. Login to cPanel WHM
  2. Type "apache" in the search box on top left
  3. Click EasyApache(Apache update)
  4. Let's play safe. Select Previously Saved Config
  5. Click Start customizing based on profile
  6. Click next until you see Exhaustive Options List
  7. Check the Deflate box under Apache built-in modules
  8. Finally, click Save and Build

Note: mod_headers must be enabled by default but check that too.

This will take some time depending on your server's capabilities and if there's any Apache or PHP updates. If there are, go ahead and update them BUT only to the stable versions.

B - The easier, less boring part.

  1. Again, type "apache" in the search box on top left
  2. Apache Configuration
  3. Include Editor
  4. Find Post VirtualHost Include
  5. Select All Versions from dropdown - so that you won't have to do this again if/when you update your Apache in the future.
  6. Paste the code below and make sure you're pasting it in the POST VIRTUALHOST INCLUDE box!

This is different than the code posted on official Apache site and all the forums re-posting the same old. I believe it's more complete and doesn't contain unnecessary stuff like support for really old browsers. If you find someone who is still using Netscape 4, leave them alone. And move away, silently...

 


 

# G-zip / Deflate / by flocked.net
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript text/x-js application/xhtml+xml application/xml application/rss+xml application/atom_xml application/x-javascript application/x-httpd-php application/x-httpd-fastphp application/x-httpd-eruby application/javascript

# high 9 – low 1 - We choose 4 because generally the most efficient and balanced choice
DeflateCompressionLevel 4

# Don’t compress these files. Already compressed, not worth it or would cause problems.
<IfModule mod_setenvif.c>
SetEnvIfNoCase Request_URI .(?:gif|jpeg|jpg|png|ico|pdf|flv|avi|mov|mp3|mp4|rm|swf|mp?g|exe|t?gz|zip|bz2|sit|rar|7z)$ no-gzip dont-vary
</IfModule>

<IfModule mod_headers.c>
  <FilesMatch "\.(js|css|xml|gz)$">
    Header append Vary: Accept-Encoding
  </FilesMatch>
</IfModule>
</IfModule>

 


 

This is tested on Apache 2.22 and 2.24. It works.

How to enable compression on Ampps server WampServer and similar:

We simply edit the httpd.conf file by adding the lines above at the end of the file. Also uncomment(remove the # sign) these lines:

LoadModule deflate_module modules/mod_deflate.so

LoadModule headers_module modules/mod_headers.so

You now have a better web server. Congratulations! Next step is to see the performance increase with the help of these tools: Yahoo's YSlow and Google's Pagespeed plugins. I tried the Firefox plugins but they have versions for other browsers.(Except, guess which one?) Both require Firebug plugin installed for Firefox and add their own tabs to Firebug.

YSlow and Pagespeed will show you how much bandwidth you saved your hosting company, yourself and your visitors by downloading both the uncompressed and compressed versions of your website files along with other recommendations.

Clear your browser cache between tests.

 

Y U NO choose maximum level compression?

Thanks to the charts by Taka's Runloop, we can see that using the highest compression ratio isn't very efficient. Level 3 or 4 compression is the logical choice here.

On my test, a 876kb page with CSS and JS files compressed to:

  • 368.9kb on level 1 compression,
  • 347.5kb on level 4
  • 340.9kb on level 9.

If your server has practically no lack of processing power ever, level 7 should still be the max setting I think but a choice between 2-4 seems to be the best for an average web server. It's still so much better than serving raw data even on level 1.

deflate download time

deflate file size

Taka's test server was slow; netbook slow, but he also mentions getting similar results on a Xeon machine and that totally makes sense. If you run it on a super computer, ratios would stay the same, only processing time would decrease. You will see a typical graph with all compression tools even on desktop with WinRAR, 7z or WinZip.

Conclusion: High compression ratios are for storage/backup purposes; when processing time doesn't matter nowhere near as much as decrease in file size. It's like trying to reach absolute zero. I remember a scientist say that it would require all the energy in the universe to cool something to absolute zero. The closer you get, the further you realize how far away it actually is. Not quite a similar example but it means don't try to compress a file to 0 bytes maybe. :)

 

Here's the phpinfo.php to save you one more Google search. Copy & paste it into a txt file then rename it to phpinfo.php. Upload it to your server somewhere then visit it to see your server config details.

 


 

<?php

// Show all information, defaults to INFO_ALL
phpinfo();

?>

 


 

While you're reading on server optimization, check our guide on image optimization too. > Optimizing images. Reducing file sizes with no loss of image quality.

|
Rate it
(2 votes)
More stuff from this category Remove HTML links in WordPress comments »