Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Web Servers»Apache»Enabling GZIP Compression in Apache on Ubuntu & Debian

    Enabling GZIP Compression in Apache on Ubuntu & Debian

    By RahulMarch 23, 20233 Mins Read

    GZIP compression is a powerful tool that can significantly improve website performance by reducing the size of files transferred between the web server and client. In this article, we will explore what GZIP compression is, why it matters, and how to enable it on Apache running on Ubuntu.

    What is GZIP compression?

    GZIP compression is a method of compressing files before sending them over the network. It works by analyzing the content of a file and finding patterns that can be replaced with shorter symbols, reducing the overall size of the file. When the client receives the compressed file, it decompresses it and displays the original content.

    Why does GZIP compression matter?

    Reducing the size of files transferred over the network can significantly improve website performance. Smaller files mean faster transfer times, which can result in quicker page load times and a better user experience. Additionally, GZIP compression can reduce bandwidth usage and lower hosting costs, especially for websites with high traffic.

  • Read: How to enable Gzip Compression on Nginx Server
  • Enabling GZIP compression on Apache

    Enabling GZIP compression on Apache is a straightforward process. Here are the steps to follow:

    1. Check if GZIP is already enabled: Before enabling GZIP, check if it’s already enabled on your Apache server by running the following command:
      sudo apache2ctl -t -D DUMP_MODULES | grep deflate 
      

      If GZIP is already enabled, you will see the following output:

      Output
      deflate_module (shared)

      If you don’t see this output, you need to enable the deflate module, which is responsible for handling GZIP compression.

    2. Enable the deflate module: To enable the deflate module, run the following command:
      sudo a2enmod deflate 
      

      This command will create a symbolic link to the deflate configuration file in the /etc/apache2/mods-enabled/ directory.

    3. Configure GZIP compression: Once the deflate module is enabled, you need to configure GZIP compression in the Apache configuration file. Open the configuration file using your preferred text editor.
      sudo nano /etc/apache2/apache2.conf 
      

      To enable it for specific website, edit the site specific virtual host configuration file or create a .htaccess file under the document root. Then add/append the following snippet to the file.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      <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
       
        # Level of compression (highest compression level)
        DeflateCompressionLevel 9
       
        # Do not compress certain file types
        SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
        SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
        SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
       
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
      </IfModule>

      The above configuration file includes a list of file types that should be compressed and the level of compression (9 being the highest compression level). It also specifies file types that should not be compressed, such as images and archives, as compressing these types of files can actually increase their size.

    4. Restart Apache: After configuring GZIP compression, you need to restart Apache to apply the changes:
      sudo systemctl restart apache2 
      

    Verify Gzip Compression

    Now that your site has been enabled with gzip compression, let’s use one of below online tools to verify that Gzip is working correctly.

    • http://www.whatsmyip.org/http-compression-test/

    Enabling GZIP Compression in Apache

    Conclusion

    Enabling GZIP compression on Apache running on Ubuntu is an effective way to improve website performance and reduce bandwidth usage. By following the steps outlined in this article, you can easily enable GZIP compression and enjoy the benefits of faster website loading times and improved user experience.

    apache gzip gzip gzip compression mod_deflate
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    10 Quick Apache Optimization Hacks

    Mysqldump + Gzip: Commands with Practical Examples

    An Introduction to Apache MPM (Multi-Processing Modules)

    View 8 Comments

    8 Comments

    1. John on November 17, 2017 10:33 am

      Thanks, useful tutorial.

      Reply
    2. John on April 5, 2017 11:16 pm

      This is good, but I highly recommend everyone check the HTML5 Boilerplate example for a bit more detail: https://github.com/h5bp/server-configs-apache

      Go to the /dist folder you’ll find a .htaccess with best practices for many things Apache, including gzip

      Reply
    3. Umesh on February 14, 2017 2:49 pm

      i followed above things but js/css not compressed- > google speed test tool -> http://www.namomithrarealestate.com….

      Reply
    4. Sanjay Raturi on April 14, 2016 6:04 pm

      Hi I am Sanjay Raturi from India.
      I have to enable Gzip compression for my website yogainrishikesh.in

      Please help me. please.

      Reply
      • Sanjay raturi on July 22, 2016 12:36 am

        I have 100/100 page speed on pagespeed insights google tool.
        See http://www.yogainrishikesh.in on Google pagespeed insights.

        Reply
    5. Sam Dawson on April 14, 2016 3:36 pm

      You have a typo! sudo a2enmod defalte should be sudo a2enmod deflate!

      Reply
      • Rahul on April 16, 2016 2:56 am

        Thanks SAM!!!

        Reply
    6. Kostas on October 6, 2015 7:55 pm

      Very useful, thank you.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    • How to Enable Apache Rewrite (mod_rewrite) Module
    • What are Microservices?
    • Variable Expansion in ZSH
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.