Saturday, November 28, 2020
Home Web Servers Apache Enable Apache Gzip Compression - Ubuntu 18.04 & 16.04

Enable Apache Gzip Compression – Ubuntu 18.04 & 16.04

Enable Apache Gzip Compression – Ubuntu 18.04 & 16.04 – gzip is a file format and a software application used for file compression and decompression. The program was created by Jean-loup Gailly and Mark Adler as a free software replacement for the compress program used in early Unix systems, and intended for use by GNU.

Enable Apache Gzip Compression – Ubuntu 18.04 & 16.04

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

Step 1 – Enable Gzip Module

Debian based operating systems can enable Gzip module (mod_deflate) using the following command.

$ sudo a2enmod deflate

Related Articles

Increase WordPress Page Speed using htaccess

Step 2 – Configure Gzip Compression using .htaccess

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  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

  # Remove browser bugs (only needed for really old browsers)
  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
</IfModule>

Step 3 – Configure Gzip Compression using Apache Virtual Host

We can define which file types we need to compressed with below identifier in configuration file.

<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 4 – Testing Compression

Now as your site has been enabled with gzip compression. You can test GZip compression using below sites

Gzip Compression Test

Check Gzip (& Brotli) Compression

3 COMMENTS

  1. Wow that was unusual. I just wrote an very long
    comment but after I clicked submit my comment didn’t appear.

    Grrrr… well I’m not writing all that over again. Anyway, just wanted to say
    excellent blog!

    Take a look at my blog – CBD for Sale

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

What’s New in PHP 8 (Features & Improvements)

What’s New in PHP 8 (Features & Improvements) - it is a major update of the PHP language. It contains many new...

How to add Facebook Customer Chat with GTM

How to add Facebook Customer Chat with GTM | The Plugin allows you to integrate your Messenger experience directly into your website....

Implement Facebook pixel with Google Tag Manager-GTM

What is Facebook Pixel How to Implement Facebook pixel with Google Tag Manager-GTM - The Facebook pixel is...

Magento 2.3.5 Content Security Warnings | How to Fix

Magento 2.3.5 Content Security Warnings | How to Fix - You are working on a Magento 2.3.5 project and you have a...

Recent Comments