In this tutorial, we will discuss the steps required to enable Gzip compression of static resources served by the Apache HTTP Server. When the browser or HTTP client sends a request to web server, the web server either generate the response(HTML) or locate the resource i.e. image, JavaScript, CSS and try to compress where possible before replying back to the browser or HTTP client. This is where Gzip compression happens where the web server further compresses the resources before sending it to the requestor.
The deflate output filter available in the mod_deflate module does the heavy lifting job to compress and serve the static resources including HTML, images, JavaScript, CSS, etc.
Check Module
Though the module is enabled by default in Apache, we can always check whether it's available as shown below.
a2enmod deflate
On WAMP installation, it's disabled by default. It looks like the one shown in Fig 1 using System Tray. Click on the deflate_module option to enable it.
Also, check the apache configuration file to make sure that the module is enabled.
# Locate the file httpd.conf and make sure that it is enabled.
# Ebabled - All is well LoadModule deflate_module modules/mod_deflate.so
# Disabled - Remove # and save the file to enable it # LoadModule deflate_module modules/mod_deflate.so
Gzip Compression
The simplest form to enable Gzip compression is to add
# Enable Gzip compression for html, text, xml, css, and javascript AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
We can also update the virtual host as shown below. In the case where it's not possible to update the virtual host, we can always go with the .htaccess option.
<Directory "/site"> ....
# Enable Gzip compression for html, text, xml, css, and javascript AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
.... </Directory>
The complete solution is as shown below. We can also configure a few more parameters to further enhance the compression by playing with the deflate_module options including DeflateCompressionLevel, DeflateMemLevel, and DeflateWindowSize.
<IfModule mod_deflate.c> # Compress HTML AddOutputFilterByType DEFLATE text/html
# Compress Text AddOutputFilterByType DEFLATE text/plain
# Compress CSS AddOutputFilterByType DEFLATE text/css
# Compress JavaScript AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE application/x-javascript
# Compress Images AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE image/x-icon
# Compress Fonts AddOutputFilterByType DEFLATE application/vnd.ms-fontobject AddOutputFilterByType DEFLATE application/x-font AddOutputFilterByType DEFLATE application/x-font-opentype AddOutputFilterByType DEFLATE application/x-font-otf AddOutputFilterByType DEFLATE application/x-font-truetype AddOutputFilterByType DEFLATE application/x-font-ttf AddOutputFilterByType DEFLATE font/opentype AddOutputFilterByType DEFLATE font/otf AddOutputFilterByType DEFLATE font/ttf
# Compress XML AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE text/xml
# Old browser fixes BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html Header append Vary User-Agent </IfModule>
This is the easiest way to enable Gzip compression of websites hosted on Apache HTTP Server.