Gzip compression in Apache reduces the size of text responses, which speeds up page loads and cuts bandwidth for both clients and servers.
Apache negotiates compression per request using the client’s Accept-Encoding header, then applies the DEFLATE output filter provided by mod_deflate to eligible responses. When compression is active, the server returns a Content-Encoding: gzip header, and caches should treat compressed and uncompressed variants as separate representations.
Compression trades CPU for bandwidth, so it belongs on compressible MIME types (HTML, CSS, JavaScript, JSON, XML, SVG) and not on already-compressed formats (JPEG, PNG, ZIP). The commands and paths below assume a Debian or Ubuntu layout using /etc/apache2 and the apache2 systemd unit.
Steps to enable gzip compression in Apache:
- Enable the deflate module.
$ sudo a2enmod deflate Considering dependency filter for deflate: Module filter already enabled Module deflate already enabled
- Enable the headers module to add a safe Vary: Accept-Encoding header for caches.
$ sudo a2enmod headers Enabling module headers. To activate the new configuration, you need to run: systemctl restart apache2
- Create a compression rules file in /etc/apache2/conf-available/gzip.conf.
$ sudo tee /etc/apache2/conf-available/gzip.conf >/dev/null <<'EOF' <IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/javascript application/x-javascript application/json application/xml text/xml application/xhtml+xml application/rss+xml image/svg+xml </IfModule> <IfModule mod_headers.c> Header append Vary Accept-Encoding </IfModule> EOFLimit the filter list to compressible types; adding JPEG/PNG/ZIP wastes CPU and can increase response size.
- Enable the gzip configuration snippet.
$ sudo a2enconf gzip Enabling conf gzip. To activate the new configuration, you need to run: systemctl reload apache2
- Validate the Apache configuration syntax.
$ sudo apachectl configtest Syntax OK
If Syntax OK is not returned, do not reload; the gzip rules remain unapplied until the configuration error is fixed.
- Reload the Apache service to apply the changes.
$ sudo systemctl reload apache2
- Confirm gzip compression in the response headers.
$ curl -sD - -o /dev/null -H 'Accept-Encoding: gzip' -H 'Host: example.com' http://127.0.0.1/ HTTP/1.1 200 OK Date: Sat, 10 Jan 2026 04:57:11 GMT Server: Apache/2.4.58 (Ubuntu) Last-Modified: Sat, 10 Jan 2026 04:10:01 GMT ETag: "29af-64800d0d6e15b-gzip" Accept-Ranges: bytes Vary: Accept-Encoding Content-Encoding: gzip Content-Length: 3121 Content-Type: text/html
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
