Apache is a popular web server used by millions of high-traffic websites and blogs. As our site gains traffic, it tends to slow down. In such cases, often, system administrators need to improve website speed and reduce network bandwidth. Enabling data compression in Apache web server is a simple yet powerful way to do this. In this article, we will learn how to enable GZIP compression in Apache server.
What is GZIP Compression in Apache server
Typically, when a web browser requests a URL, its response page is directly served by web server, as it is. These days, most web pages are content heavy with many images, files, etc. This can slow down your web page speed and also increase network bandwidth. It results in poor user experience and increase data costs and server processing.
This can be avoided by enabling data compression in web servers. When you enable it, then the server will compress the response data before sending it to client web browser. It also specifies the compression algorithm used for this purpose, as a response header. When the browser receives such a response, it immediately uncompresses the data before rendering the web page. This results in reduced data consumption and server resources, and also improves website speed, since the amount of data transferred is reduced. This whole thing completely occurs between the server and client browser and users do not need to do anything. All you need to do is enable data compression at the web server.
Why Enable GZIP Compression in Apache
Enabling GZIP compression offers several advantages such as :
1. Faster Page Load Times
When you enable data compression in Apache server, it reduces the size of data transferred between server and client browser. This automatically increases web site speed.
2. Reduced Bandwidth
Compressing data saves money for both website owners as well as users. It reduces the amount of outgoing data from web server, as well as incoming data to users. This also saves up server resources since it needs to spend less time sending response data.
3. Better User Experience
The end result is that your website appears a lot faster, even on mobile devices. This improves user experience along with better engagement and bounce rates.
How to Enable GZIP Compression in Apache Server
GZIP compression is managed by mod_deflate in Apache server. It is generally enabled by default. But sometimes that may not be the case. Here are the steps to enable GZIP compression in Apache server by enabling this module.
Redhat/CentOS
In Redhat/CentOS systems, open Apache configuration file in text editor. It is generally /etc/httpd/conf/httpd.conf. Add the following lines to this file to enable mod_deflate module.
LoadModule deflate_module modules/mod_deflate.so
LoadModule filter_module modules/mod_filter.so
Save and close the file.
Ubuntu/Debian
In Ubuntu/Debian systems, run the following command to enable mod_deflate.
sudo a2enmod deflate
In both cases, restart Apache server to apply changes.
sudo service apache2 restart
How to Apply GZIP Compression in Apache
The above methods apply gzip compression to all files whether it is CSS, JS, HTML, images, etc. Sometimes, you may want to apply gzip compression only for specific file types such as CSS, JS.
In this case, just customize and add the following code in httpd.conf file.
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript,
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE text/javascript
# Avoid compressing already compressed files like images, videos, etc.
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png|rar|zip|exe|flv|mov|wma|wmv|avi|mp4|mp3|ogg|webp|pdf)$ no-gzip dont-vary
</IfModule>
In the above code, we use AddOutputFilterByType directive to specifically compress file types. We have specified file types for JS, CSS and HTML. We have also avoided compressing file types that are already compressed, such as MP3 files. You can customize this code as per your requirement.
How to Disable GZIP Compression in Apache
Sometimes, if you find that the data compression on your web server is not working properly, then you may need to temporarily disable it to fix the problem. There are a several ways to disable GZIP compression in Apache
1. Disable GZIP Compression in Ubuntu
If you want to completely disable GZIP compression across your entire Apache installation, then just run the following terminal commands.
sudo a2dismod deflate
sudo systemctl restart apache2
2. Disable GZIP Compression in CentOS/RHEL
If you want to disable GZIP Compression in CentOS/RHEL then open Apache Configuration File and comment the following line by adding ‘#’ at its beginning. It will disable mod_deflate module.
#LoadModule deflate_module modules/mod_deflate.so
Restart Apache server to apply changes.
sudo service apache2 restart
3. Disable GZIP for Specific Files & Folders
You can also disable GZIP compression for specific files and folders, using .htaccess file or <Directory> tag of virtual host file. Here is a code snippet to disable GZIP compression for .js and .css files.
<IfModule mod_headers.c>
<FilesMatch "\.(js|css)$"> # Adjust file extensions as needed
SetEnv no-gzip 1
SetEnv dont-vary 1
</FilesMatch>
</IfModule>
In the above code, we first check if mod_headers is enabled or not. If so, then we further check the file extension. For JS and CSS files, we set environment variable no-gzip to 1. This will instruct GZIP module to skip compression of JS and CSS files. Place the above code in .htaccess file or within the Directory tag in virtual host file.
4. Disable for Specific Virtual Hosts
If you only want to disable gzip for a specific virtual host, then open the virtual host configuration file and add the no-zip environment variable as shown inside VirtualHost tag.
<VirtualHost *:80>
ServerName yourdomain.com
...
SetEnv no-gzip 1
</VirtualHost>
Conclusion
In this article, we have learnt how to enable GZIP compression for Apache web server. We have learnt how to enable gzip in RHEL/CentOS as well as Ubuntu/Debian systems. We have also learnt how to apply GZIP compression for specific file types. Lastly, we also learnt how to disable GZIP compression.
Enabling Gzip compression will improve your website speed and reduce data bandwidth. This will reduce both your server costs as well as data costs. Depending on your Apache installation, you can use any of the above steps. Please note, most web servers support several compression algorithms such as gzip, bzip2, brotli, etc. They are also supported by popular client browsers like Chrome, Firefox, Safari, etc. You can use any of them as per your requirement.
Also read:
How to Upgrade Apache Server in CentOS/Redhat
How to Block Image Hotlinking in Apache
How to Change Port Number in Apache

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.