Gzip compression shrinks text responses before Apache sends them across the network, which lowers bandwidth use and makes HTML, CSS, JavaScript, JSON, XML, and SVG assets reach the browser faster on slower links.

On Apache 2.4, mod_deflate inserts the DEFLATE output filter only when the client advertises Accept-Encoding: gzip and the response MIME type matches the configured filter list. When compression is used, Apache returns Content-Encoding: gzip, and mod_deflate also adds Vary: Accept-Encoding so caches keep compressed and uncompressed responses separate.

Examples below follow the current Debian and Ubuntu layout with /etc/apache2, the apache2 service unit, and the a2enmod plus a2enconf helper commands. Keep the filter list limited to compressible text content, and remember that HTTPS compression can contribute to BREACH-style side-channel exposure when a response reflects secrets.

Steps to enable gzip compression in Apache:

  1. Ensure the deflate module is enabled.
    $ sudo a2enmod deflate
    Considering dependency filter for deflate:
    Module filter already enabled
    Module deflate already enabled

    Current Debian and Ubuntu Apache packages often ship mod_deflate already enabled, so rerunning a2enmod deflate is a safe idempotent check.

  2. 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/json application/xml text/xml application/xhtml+xml application/rss+xml application/atom+xml image/svg+xml
    </IfModule>
    EOF

    Limit the filter list to compressible types; adding JPEG/PNG/ZIP wastes CPU and can increase response size.

    mod_deflate already adds Vary: Accept-Encoding for normal gzip negotiation, so no separate mod_headers rule is needed unless compression decisions also depend on another request header such as User-Agent.

  3. Enable the gzip configuration snippet.
    $ sudo a2enconf gzip
    Enabling conf gzip.

    a2enconf creates the symlink from /etc/apache2/conf-enabled to the matching file in /etc/apache2/conf-available and does not enable missing module dependencies automatically.

  4. Validate the Apache configuration syntax before applying the new rules.
    $ sudo apache2ctl configtest
    Syntax OK

    If Syntax OK is not returned, do not reload; the gzip rules remain unapplied until the configuration error is fixed.

  5. Reload Apache to apply the gzip rules.
    $ sudo systemctl reload apache2

    If step 1 reported Enabling module deflate instead of already enabled, use sudo systemctl restart apache2 once so Apache loads the newly enabled module.

  6. Confirm that mod_deflate is loaded.
    $ apache2ctl -M 2>/dev/null | grep deflate
     deflate_module (shared)
  7. Confirm that a text response is served with gzip compression.
    $ curl -sS -D - -o /dev/null -H 'Accept-Encoding: gzip' -H 'Host: example.com' http://127.0.0.1/
    HTTP/1.1 200 OK
    Date: Thu, 09 Apr 2026 03:55:15 GMT
    Server: Apache/2.4.58 (Ubuntu)
    Last-Modified: Thu, 09 Apr 2026 03:55:13 GMT
    ETag: "82-64efefbce6880-gzip"
    Accept-Ranges: bytes
    Vary: Accept-Encoding
    Content-Encoding: gzip
    Content-Length: 122
    Content-Type: text/html

    If Content-Encoding: gzip is missing, test a text URL that matches the active virtual host. Binary assets and content types outside the AddOutputFilterByType list will not be compressed, and a missing Content-Length alone is not a gzip failure because Apache can switch to chunked transfer encoding.