The
Step 1 – Enable Gzip Module
Debian based users can enable Gzip module (mod_deflate) using the following command.
sudo a2enmod deflate
Step 2 – Configure Gzip Compression
We can define which file types we need to compressed with below identifier in configuration file.
AddOutputFilterByType DEFLATEtext/html text/plain text/css application/javascript
Add following configuration in Apache Virtual Host to enable gzip compression for your website. You can also add this code to websites .htaccess file under the main document root.
<Directory /var/www/html/> <IfModule mod_mime.c> AddType application/x-javascript .js AddType text/css .css </IfModule> <IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/plain text/xml application/javascript <IfModule mod_setenvif.c> BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4.0[678] no-gzip BrowserMatch bMSIE !no-gzip !gzip-only-text/html </IfModule> </IfModule> Header append Vary User-Agent env=!dont-vary </Directory>
Step 3 – Testing Compression
Now as your site has been enabled with gzip compression, let’s use one of below online tools to verify gzip is working correctly.
8 Comments
Thanks, useful tutorial.
This is good, but I highly recommend everyone check the HTML5 Boilerplate example for a bit more detail: https://github.com/h5bp/server-configs-apache
Go to the /dist folder you’ll find a .htaccess with best practices for many things Apache, including gzip
i followed above things but js/css not compressed- > google speed test tool -> http://www.namomithrarealestate.com….
Hi I am Sanjay Raturi from India.
I have to enable Gzip compression for my website yogainrishikesh.in
Please help me. please.
I have 100/100 page speed on pagespeed insights google tool.
See http://www.yogainrishikesh.in on Google pagespeed insights.
You have a typo! sudo a2enmod defalte should be sudo a2enmod deflate!
Thanks SAM!!!
Very useful, thank you.