Beginner Fundamentals

Compression with mod_deflate

Compression shrinks responses before sending them, so pages load faster and use less bandwidth. Apache provides gzip compression through the mod_deflate module.

Enable the Module

sudo a2enmod deflate
sudo systemctl restart apache2

Compress Common File Types

Only text-based content benefits from compression. Images and videos are already compressed:

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/json
</IfModule>

AddOutputFilterByType DEFLATE applies gzip to each listed MIME type.

Verify Compression Works

Ask for a gzipped response and check the headers:

curl -H "Accept-Encoding: gzip" -I http://localhost/style.css

Look for this line in the output:

Content-Encoding: gzip

Apply Changes

sudo apache2ctl configtest
sudo systemctl reload apache2

Compression is one of the easiest wins for performance, often cutting text file sizes by more than half.