How to Enable GZIP Compression in Apache2 on Ubuntu
GZIP compression helps to reduce the size of HTTP responses, which can improve website load times and overall performance. Here’s a detailed guide to enabling GZIP compression in Apache2 on Ubuntu:
1. Install and Enable the mod_deflate
Module
Apache uses the mod_deflate
module for GZIP compression. This module is typically included with Apache by default.
Step 1.1: Check if mod_deflate
is Installed
Run the following command to ensure the module is available:
apache2ctl -M | grep deflate
If you see deflate_module
, it is already installed and enabled. If not, continue with the next steps.
Step 1.2: Enable the Module
Run the following command to enable the module:
sudo a2enmod deflate
This command enables the mod_deflate
module. If it’s already enabled, you’ll see a message stating that the module is already active.
Step 1.3: Restart Apache
After enabling the module, restart Apache to load the changes:
sudo systemctl restart apache2
2. Configure the Deflate Module
You need to configure which file types should be compressed and exclude any unnecessary files.
Step 2.1: Open the deflate.conf
File
The configuration file for the mod_deflate
module is located in /etc/apache2/mods-enabled/deflate.conf
. Open it for editing:
sudo nano /etc/apache2/mods-enabled/deflate.conf
Step 2.2: Add or Update Configuration
Ensure the following lines are included in the file. These settings define what content types will be compressed:
<IfModule mod_deflate.c>
# Enable compression for common text-based file types
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
AddOutputFilterByType DEFLATE application/javascript application/json
AddOutputFilterByType DEFLATE application/x-javascript application/xhtml+xml
AddOutputFilterByType DEFLATE image/svg+xml
# Exclude certain file types from compression
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|woff|woff2|zip|gz)$ no-gzip dont-vary
# Compress text-like responses only for modern browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.[0678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent env=!dont-vary
</IfModule>
Here’s what these settings do:
- AddOutputFilterByType DEFLATE: Specifies the MIME types of files to compress, such as HTML, CSS, JavaScript, JSON, etc.
- SetEnvIfNoCase Request_URI: Excludes certain file types (e.g., images and pre-compressed files like
.gz
or.zip
) from compression because these are already optimized. - BrowserMatch: Ensures compatibility with older browsers that might not handle GZIP compression properly.
- Header append Vary: Prevents caching issues with proxies and ensures proper content delivery based on the browser.
Step 2.3: Save and Exit
Save the file (in Nano, press CTRL + O
, then Enter
, and CTRL + X
to exit).
3. Restart Apache
After making configuration changes, restart Apache to apply them:
sudo systemctl restart apache2
4. Test GZIP Compression
You can verify that GZIP compression is working by checking the HTTP response headers for your website.
Option 1: Use a Browser Developer Tool
- Open your website in a browser (e.g., Chrome).
- Open the Developer Tools (F12 or
Ctrl + Shift + I
). - Go to the Network tab and reload the page.
- Select a resource (e.g., an HTML file) and look for the
Content-Encoding
header under the Headers section.- If GZIP is enabled, it will say
Content-Encoding: gzip
.
- If GZIP is enabled, it will say
Option 2: Use an Online Tool
You can use tools like Check GZIP Compression to verify compression.
Option 3: Use curl
Command
Run the following command to test if GZIP compression is active:
curl -H "Accept-Encoding: gzip" -I http://yourwebsite.com
If GZIP is enabled, you will see Content-Encoding: gzip
in the response headers.
5. Advanced Configuration (Optional)
5.1: Enable Compression for Dynamic Content
Dynamic content such as PHP responses can also benefit from GZIP compression. Add the following directive in your Apache Virtual Host or .htaccess
file:
<FilesMatch "\.(html|php)$">
SetOutputFilter DEFLATE
</FilesMatch>
5.2: Monitor Compression Performance
To monitor the effectiveness of compression, use tools like Google PageSpeed Insights or Lighthouse to evaluate your site’s performance.
By following these steps, you should have GZIP compression enabled and optimized for your Apache2 server on Ubuntu.