How to Configure GZIP Compression in Apache?
Enabling GZIP compression in Apache is a highly effective way to
optimize your website’s performance by reducing file sizes before they
are sent to a visitor’s browser. This process lowers bandwidth usage and
significantly decreases page load times. This guide provides a
straightforward, step-by-step walkthrough to enable and configure GZIP
compression using Apache’s mod_deflate module, verify that
it is working correctly, and customize which file types get
compressed.
Step 1: Enable the mod_deflate Module
Apache utilizes the mod_deflate module to handle GZIP
compression. On most modern Linux distributions, this module is enabled
by default. However, you should verify and ensure it is active.
For Ubuntu/Debian systems, run the following command in your terminal:
sudo a2enmod deflateFor CentOS/RHEL systems, the module is typically loaded automatically via the main configuration file. You can confirm it is active by checking your configuration for this line:
LoadModule deflate_module modules/mod_deflate.soStep 2: Configure Compression Settings
Once the module is active, you need to specify which file types Apache should compress. It is best practice to compress text-based assets (like HTML, CSS, and JavaScript) while avoiding already-compressed formats like JPEG images or PDF files.
Open your main Apache configuration file (e.g.,
/etc/apache2/apache2.conf,
/etc/httpd/conf/httpd.conf) or your specific virtual host
file, and add the following configuration block:
<IfModule mod_deflate.c>
# Set the compression level (1-9; 6 is the recommended balance of speed and CPU)
DeflateCompressionLevel 6
# Compress specific file types
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
AddOutputFilterByType DEFLATE application/javascript application/x-javascript
application/json application/xml application/xhtml+xml
# Explicitly handle older, incompatible browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>Step 3: Restart the Apache Server
For the changes to take effect, you must restart your Apache web server. Always check your syntax before restarting to avoid downtime.
Test the configuration syntax:
sudo apache2ctl configtest
# Or for CentOS/RHEL: sudo httpd -tIf the output says “Syntax OK”, proceed to restart the service:
sudo systemctl restart apache2
# Or for CentOS/RHEL: sudo systemctl restart httpdStep 4: Verify GZIP Compression is Working
After restarting, you should confirm that Apache is successfully compressing your files. You can do this using a terminal tool or an online checker.
Using curl via the command line, run the following
command replacing example.com with your domain:
curl -I -H "Accept-Encoding: gzip" https://example.comLook for the Content-Encoding header in the response. If
it reads Content-Encoding: gzip, your Apache server has
been successfully configured for GZIP compression.