How To Enable Gzip Compression Apache on Ubuntu 18.04 & 16.04

Channel: Linux
Abstract: AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/plain text/xml application/javascriptStep 2 – Configur

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.

  • Read: How to enable Gzip Compression on Nginx Server
  • Step 1 – Enable Gzip Module

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

    sudo a2enmod deflate
    
    Step 2 – Configure Gzip Compression

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

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

    Add following configuration in Apache Virtual Host to enable gzip compression for your website. You can also add this code to websites .htaccess file under the main document root.

    Apache VirtualHost Configuration
    <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 3 – Testing Compression

    Now as your site has been enabled with 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/

    Ref From: tecadmin

    Related articles