How to Enable GZIP Compression on a Linux Server Running Apache

How to Enable GZIP Compression on a Linux Server Running Apache


GZIP compression is a method of bundling (zipping) web pages on a server before delivering them to visitors. This process reduces bandwidth usage and significantly improves page loading speeds. Once the compressed page reaches the visitor’s browser, it is automatically unzipped. The entire process of compression and decompression takes only a fraction of a second and is seamless for the end user.

What Does GZIP Do?

GZIP compression is highly effective for text-based files and is recommended for the following file types:

  • HTML files, including static (.html) and dynamic (.php, .aspx) formats.
  • Text files with the .txt extension.
  • CSS and JavaScript files (.css and .js extensions).
  • Web service data, such as WSDL, REST, and JSON responses.

However, GZIP compression is not recommended for non-text files, such as images, videos, or pre-compressed files (e.g., .zip files). Since these file types are already optimized or compressed, applying GZIP may not save additional space and could even increase loading times.

Why You Should Use GZIP

  • Bandwidth savings: GZIP compression reduces file sizes by 50% to 80%.
  • Improved performance: Faster website loading times enhance user experience.
  • Transparency: The compression and decompression process is quick and invisible to the end user.

How to Enable GZIP in Apache

To enable GZIP compression, you need to activate the mod_deflate module in Apache. This module handles the compression of web pages before they are sent to the client. The exact steps to enable this module may vary depending on your Linux distribution. Refer to your distribution’s documentation for specific instructions on activating mod_deflate.

Once the mod_deflate module is active, add the following directive to your website’s .htaccess file to enable GZIP compression:

<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>

This approach works seamlessly with popular hosting control panels such as CWP and cPanel.

How to Verify GZIP is Working

After enabling GZIP compression, you can verify that it is working using an online GZIP test tool. Follow these steps:

  1. Visit Gift of Speed – GZIP Test.
  2. Enter your website’s URL in the provided field.
  3. Click the Check button.

If GZIP compression is successfully enabled, the tool will confirm it and display details about the compressed files.