Skip to main content
How to enable Gzip compression in Apache

How to enable Gzip compression in Apache

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.

http://checkgzipcompression.com/
http://www.whatsmyip.org/http-compression-test/