Enabling Gzip Compression in Apache Web Server on Linux Systems
In this article, we will show you the steps and benefits of enabling Gzip compression in Apache web server on Linux systems (RPM Family “Redhat/CentOS/Scientific Linux ”and Debian Family “Debian/Ubuntu”).
Performance of your website can be improved by enabling response compression using GZIP. Compression reduces the size of a response body, reduces the bandwidth required to transfer the response data, and ultimately makes sure the resources of your website are delivered to the client side sooner “reduce website page load time”.
In Apache web server, mod_deflate module provides the DEFLATE output filter that allows output from your server to be compressed before being sent to the client over the network. Currently mod_deflate is using with newer version of Apache. mod_deflate is the replacement of mod_gzip which was used with older version of Apache.
Step 1: Enable Gzip Module in Apache
By default mod_deflate modules are enabled in Apache. To make sure check following line in Apache configuration file.
LoadModule deflate_module modules/mod_deflate.so
Debian based users can enable Gzip module (mod_deflate) using following command.
$ sudo a2enmod deflate
Step 2: Configure Gzip Compression
We can define which file types we need to compressed with below identifier in configuration file. Here’s we’ll compress everything but images and pdf.
SetOutputFilter DEFLATE SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
Here’s some useful hints on compression your server response.
Hints: 1. Archives, images, and movies are not suitable for compression, as they are usually already compressed. 2. Executable files are less suitable for compression, but can benefit from it in some cases. 3. Response compression comes at a cost: it is CPU-intensive. You need to consider that in your capacity planning and system design.
Add following configuration in Apache Virtual Host to enable gzip compression for your website. You can also add this code in websites .htaccess file under main document root.
<Directory /var/www/html/> <IfModule mod_deflate.c> SetOutputFilter DEFLATE SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary </IfModule> </Directory>
Hints: 1. If you added the compression block in the Apache configuration file, YOU MUST RESTART YOUR APACHE WEB SERVER FOR CHANGES TO BE ACTIVATED. 2. If you added the compression block in .htaccess, it's effect will be immediately. 3. If you want to restrict the compression to particular MIME types in general, you may use the AddOutputFilterByType directive.
Here’s an example of using AddOutputFilterByType directive to enable compression for particular MIME types:
<Directory "/your-server-root/manual"> AddOutputFilterByType DEFLATE text/plain text/css text/html text/javascript text/xml application/json application/javascript application/x-javascript application/xml application/xml+rss </Directory>
The preceding configuration enables compression for MIME types that hypertext documents, cascading style sheets, and JavaScript files appear to be in. These are the types of documents that benefit most from the compression.
Now as your site has been enabled with gzip compression. You can use any online tools to verify that Gzip is working correctly on your website or use YSlow plugin on your browser.
If You Appreciate What We Do Here On Mimastech, You Should Consider:
- Stay Connected to: Facebook | Twitter | Google+
- Support us via PayPal Donation
- Subscribe to our email newsletters.
- Tell other sysadmins / friends about Us - Share and Like our posts and services
We are thankful for your never ending support.