How To Enable GZIP Compression in Apache

Last updated on September 21st, 2021 at 11:06 am

If you enable GZIP compression in Apache web server, then it will compress data while sending responses, improve website speed & performance. Here’s how to enable GZIP compression in Apache. You can use these steps to enable GZIP compression for Apache in Ubuntu, CentOS. You can also use them to enable GZIP compression in WordPress

 

How To Enable GZIP Compression in Apache / WordPress

Here’s how to optimize website with GZIP compression. After you enable Gzip compression in Apache, you may want to use a dashboard software to monitor the key metrics about your website/application such as signups, traffic, sales, revenue, etc. using dashboards & charts, to ensure everything is working well.

 

1. Enable GZIP Module in Apache

First, we need to enable Gzip module in Apache. Debian/Ubuntu users can do this by running the following command in terminal

$ sudo a2enmod deflate

For RHEL/CentOS systems, you can open Apache configuration file at /etc/httpd/conf/httpd.conf

$ sudo vi /etc/httpd/conf/httpd.conf

and uncomment the following line by removing # at the beginning.

LoadModule deflate_module modules/mod_deflate.so

If you don’t  find the above line, just add it.

Bonus Read : How to Install mod_evasive in Apache

 

2. Enable GZIP compression in .htaccess

If you have setup Apache Virtual Host, you can open the .htaccess file for your website and add the following lines in it. Else you can create a blank .htaccess file under main document root (/var/www/html)

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

Add the following lines to it

# GZIP compression for text files: HTML, CSS, JS, Text, XML, fonts
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
</IfModule>

We use AddOutputFilterByType directive to tell Apache as to which file types to compress.

Bonus Read : How to Disable Apache Directory Listing

 

3. Restart Apache Server

Restart Apache server

## CentOS ##

sudo service httpd restart

## Ubuntu ##

sudo /etc/init.d/apache2 restart

 

How to Check If GZIP Compression is Enabled or Not

There are many free third-party tools online to test if Gzip compression is enabled on your site or not. Go to one of these tools and enter your website

You’ll see a output like the following

enable gzip compression in apache wordpress

That’s it! Now Apache will compress server response before sending them to users. By the way, if you want to create charts & dashboards to monitor your business or website, you can try Ubiq. We offer a 14-day free trial.