How to Enable Gzip Compression in Apache

How to enable gzip compression in Apache

If there’s one change I recommend to almost every client whose website feels sluggish, it’s this: turn on gzip compression. I still remember the first time I ran a before-and-after test on a client’s WordPress site — page weight dropped by nearly 70%, and their bounce rate improved within two weeks. Compression is one of those “set it up once, benefit forever” wins, and in this guide I’ll walk you through exactly how I configure it on Apache servers.

What Gzip Compression Actually Does

When a browser requests a page from your server, Apache normally sends back raw HTML, CSS, JavaScript, and other text-based files exactly as they are stored. Gzip compression squeezes those files down before sending them over the wire, and the browser decompresses them on arrival. Text compresses extremely well — I typically see 60-80% size reduction on HTML, CSS, and JS files.

The trade-off is a small amount of CPU overhead on the server to compress the files. In practice, on any modern server, that cost is negligible compared to the bandwidth and load-time savings you get.

Why I Bother With This On Every Project

Prerequisites

Before you start, make sure you have:

Checking Whether mod_deflate Is Already Enabled

On Debian/Ubuntu systems, I always check first before assuming anything needs installing:

apache2ctl -M | grep deflate

If you see deflate_module (shared) in the output, the module is already loaded. On CentOS/RHEL systems, mod_deflate is usually compiled in by default, so you can check with:

httpd -M | grep deflate

Enabling mod_deflate on Ubuntu/Debian

If the module isn’t enabled yet, I use the built-in helper:

sudo a2enmod deflate
sudo systemctl restart apache2

That’s it on Debian-based systems — the a2enmod command creates the necessary symlink and the restart applies it.

Enabling mod_deflate on CentOS/RHEL

On RHEL-based systems, mod_deflate is typically compiled into the Apache binary already. You just need to confirm it’s loaded in your httpd.conf:

LoadModule deflate_module modules/mod_deflate.so

If that line is missing or commented out with a #, uncomment it and restart:

sudo systemctl restart httpd

Configuring Gzip Compression

Once the module is active, I add a dedicated configuration block. I usually put this in a new file so it’s easy to find later — on Debian systems that’s /etc/apache2/mods-enabled/deflate.conf, or you can add it directly inside your virtual host file.

Here’s the configuration I use as my baseline:

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

I deliberately avoid compressing images like JPG, PNG, and GIF, along with already-compressed formats like ZIP or PDF — trying to gzip those wastes CPU cycles without any real benefit since they’re already compressed.

Setting the Compression Level

By default, mod_deflate uses a compression level of 6, which I’ve found to be the sweet spot between compression ratio and CPU usage. If you want to tune it, you can add:

DeflateCompressionLevel 6

Level 9 gives marginally smaller files but takes noticeably more CPU time. Unless you’re running a server that’s brutally out of resources, I stick with the default.

Restarting Apache and Testing

After saving your configuration, always test the syntax before restarting:

sudo apachectl configtest

If you see “Syntax OK,” restart Apache:

sudo systemctl restart apache2   # Debian/Ubuntu
sudo systemctl restart httpd     # CentOS/RHEL

Verifying Gzip Is Working

I never trust a configuration until I’ve actually confirmed it in the browser. My go-to method is curl:

curl -H "Accept-Encoding: gzip" -I https://yourdomain.com

Look for Content-Encoding: gzip in the response headers. If it’s there, compression is working.

You can also use online tools like GTmetrix, Google PageSpeed Insights, or check-gzip-compression.com to visually confirm the savings and see exactly which assets are being compressed.

Real-World Use Case

On a recent e-commerce site I managed, the homepage HTML was around 180KB uncompressed. After enabling gzip, it dropped to roughly 42KB — a 77% reduction. Combined with browser caching, the site’s Time to First Byte and Largest Contentful Paint both improved measurably in our Lighthouse audits.

Common Mistakes to Avoid

Troubleshooting Tips

If gzip doesn’t seem to be working after your changes:

  1. Double-check the module is actually loaded with apache2ctl -M | grep deflate.
  2. Make sure your <IfModule> block is inside the correct scope (virtual host, directory, or global config) and isn’t being overridden elsewhere.
  3. Check for typos in MIME types — a mismatched type simply won’t get compressed.
  4. Clear any CDN or proxy cache sitting in front of your server, since it may be serving stale, uncompressed responses.

Security and Performance Best Practices

Frequently Asked Questions

Does gzip compression affect image files? No, and it shouldn’t — images are already compressed in their native formats, so applying gzip on top provides no benefit and can even slightly increase file size.

Is mod_deflate better than mod_gzip? Yes. mod_gzip is the older, deprecated module. mod_deflate is the modern standard included with Apache 2.x and is what you should use.

Will gzip compression break my website? No, if configured correctly. Modern browsers all support gzip decompression natively, and the process is transparent to end users.

Does gzip compression slow down my server? There’s a small CPU cost to compress files, but on virtually any modern server this is negligible compared to the bandwidth and speed benefits.

Summary and Key Takeaways

Enabling gzip compression is one of the fastest, lowest-risk performance wins you can make on an Apache server. It takes minutes to set up, requires no application-level changes, and delivers real, measurable improvements to page load times and bandwidth usage. My recommendation: enable mod_deflate, apply it to all text-based MIME types, test with curl, and monitor real-world results with tools like PageSpeed Insights.

References

Exit mobile version