How to Enable GZip Compression in Apache

how to enable GZIP Compression in apache

Apache allows you to compress data & files before sending them to the web browsers. This not only speeds up your website but also reduces bandwidth consumption. Let us look at how to enable GZip compression in Apache.


How to Enable GZip Compression in Apache

Apache uses mod_deflate module to perform gzip compression and send the compressed data to web browsers. mod_deflate has replaced mod_gzip, which was used in old Apache versions.

Here are the steps to enable GZip compression in Apache.


1. Enable mod_deflate

First, we enable the gzip module (mod_deflate). Open a terminal window and run the following command

$ sudo a2enmod deflate


2. Configure gzip compression

Next, you need to add compression configuration to Apache. It will specify which file types you want Apache to compress.

For example, if you want to compress only html, css, and js files then add this line.

AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript


You can add this line to mod_deflate’s config file (/etc/httpd/conf.d/mod_deflate.conf) or .htaccess file (/var/www/html/.htaccess) or Apache’s main configuration file (/etc/httpd/conf/httpd.conf)


For our example, we are adding the config to .htaccess file, under the main document root.

Open the file in a text editor

$ sudo vim /var/www/html/.htaccess


Add these lines

<Directory /var/www/html/>
  <IfModule mod_mime.c>
    AddType application/x-javascript .js
    AddType text/css .css
  <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
  Header append Vary User-Agent env=!dont-vary


Save and close the file.



3. Restart Apache Server

Restart Apache server to apply changes Ubuntu/Debian:

$ sudo service apache2 restart


$ sudo systemctl restart httpd


4. Test GZip Compression

You can use online third-party tools to easily check if gzip compression is enabled on your website or not. You just have to enter the URL of your website and it will tell you whether your website serves compressed content or not.

That’s it! You have enabled gzip compression in Apache.

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!