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.
- Login to cPanel WHM
- Type "apache" in the search box on top left
- Click EasyApache(Apache update)
- Let's play safe. Select Previously Saved Config
- Click Start customizing based on profile
- Click next until you see Exhaustive Options List
- Check the Deflate box under Apache built-in modules
- 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.
- Again, type "apache" in the search box on top left
- Apache Configuration
- Include Editor
- Find Post VirtualHost Include
- Select All Versions from dropdown - so that you won't have to do this again if/when you update your Apache in the future.
- 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
# high 9 – low 1 - We choose 4 because generally the most efficient and balanced choice
# Don’t compress these files. Already compressed, not worth it or would cause problems.
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
Header append Vary: Accept-Encoding
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.
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.
// Show all information, defaults to INFO_ALL
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.