Enabling Brotli compression in Apache shrinks HTML, CSS, JSON, and JavaScript responses so pages load faster on constrained networks and serve more traffic with the same bandwidth budget.

Apache uses HTTP content negotiation to decide when to compress: browsers advertise supported algorithms in the Accept-Encoding request header and the server replies with Content-Encoding: br when mod_brotli is enabled for the response MIME type. mod_brotli plugs into the output filter chain, compressing only the content types explicitly listed in the configuration.

Brotli trades CPU for smaller payloads, so conservative quality values and a tight MIME list prevent wasted work on already-compressed formats. Shared caches should see Vary: Accept-Encoding to avoid mixing compressed and uncompressed variants. The steps use a Debian or Ubuntu layout (/etc/apache2, a2enmod, a2enconf, and the apache2 systemd unit) and build mod_brotli from source when the distro package is missing.

Steps to enable Brotli compression in Apache:

  1. Install build dependencies for mod_brotli.
    $ sudo apt install --assume-yes build-essential git cmake ninja-build libtool autoconf automake apache2-dev libpcre3-dev libssl-dev

    If /etc/apache2/mods-available/brotli.load already exists from a package, you can skip the source build.

  2. Clone and build mod_brotli from source.
    $ cd /home/user
    $ git clone https://github.com/kjdev/apache-mod-brotli.git
    $ cd apache-mod-brotli
    $ git submodule update --init --recursive
    $ ./autogen.sh
    $ ./configure
    $ make -j4
    $ sudo make install
  3. Create the module LoadModule entry when the package does not provide one.
    $ echo 'LoadModule brotli_module /usr/lib/apache2/modules/mod_brotli.so' | sudo tee /etc/apache2/mods-available/brotli.load
  4. Create a Brotli configuration snippet in /etc/apache2/mods-available/brotli.conf.
    $ sudo tee /etc/apache2/mods-available/brotli.conf >/dev/null <<'EOF'
    <IfModule brotli_module>
        BrotliCompressionLevel 5
        BrotliWindowSize 22
        BrotliAlterEtag AddSuffix
        AddOutputFilterByType BROTLI text/html text/plain text/css text/xml application/xml application/json application/javascript text/javascript application/rss+xml application/atom+xml image/svg+xml
    </IfModule>
    EOF

    BrotliCompressionLevel ranges from 0 to 11; values around 4–6 are a common balance between CPU cost and size savings.

  5. Enable the brotli module and configuration.
    $ sudo a2enmod brotli
    Enabling module brotli.
    To activate the new configuration, you need to run:
      systemctl restart apache2
    
    $ sudo a2enconf brotli
    Enabling conf brotli.
    To activate the new configuration, you need to run:
      systemctl reload apache2
  6. Validate the Apache configuration syntax and restart.
    $ sudo apachectl configtest
    Syntax OK
    
    $ sudo systemctl restart apache2
  7. Verify mod_brotli is loaded.
    $ apachectl -M 2>/dev/null | grep brotli
     brotli_module (shared)
  8. Confirm Brotli compression by requesting a URL with Accept-Encoding: br.
    $ curl -sSI -H 'Accept-Encoding: br' http://127.0.0.1/
    HTTP/1.1 200 OK
    Date: Sun, 11 Jan 2026 00:19:12 GMT
    Server: Apache/2.4.58 (Ubuntu)
    Last-Modified: Sat, 10 Jan 2026 21:15:28 GMT
    ETag: "29af-6480f24215cc8-br"
    Accept-Ranges: bytes
    Vary: Accept-Encoding
    Content-Encoding: br
    Content-Length: 2727
    Content-Type: text/html

    If Content-Encoding is missing, confirm the response Content-Type matches one of the configured MIME types.