HOW TO ENABLE GZIP COMPRESSION IN APACHE

by: Karal Max 

The mod_deflate module provides the DEFLATE output filter that allows the output from your server to be compressed before being sent to the client over the network. mod_deflate is the replacement of mod_gzip which was used with an older version of Apache.

ENABLE GZIP MODULE IN APACHE

       By default, a mod_deflate module is enabled in Apache. To make sure that it is enabled, check following line in Apache configuration file.

LoadModule deflate_module modules/mod_deflate.so

CONFIGURE GZIP COMPRESSION

      Now edit your apache configuration and add the extensions you want to compress.

nano /etc/httpd/conf/httpd.conf

Add the following lines at end of the file

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

Restart Apache service,

## CentOS ##

sudo service httpd restart

## Ubuntu ##

sudo /etc/init.d/apache2 restart

Add the following configuration in Apache Virtual Host to enable gzip compression for your website. You can also add this code in website’s .htaccess file in the site 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>
TESTING COMPRESSION

We have enabled gzip compression, let’s use one of below online tools to verify gzip is working correctly.

https://www.giftofspeed.com/gzip-test/

https://www.whatsmyip.org/http-compression-test/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.