For those of you who didn’t know, Apache’s mod_deflate is an Apache module that will compress output from your server before it is sent to the client. If you have newer version of Apache the mod_deflate module is probably loaded by default, but it may not be turned on. This article assumes you have at least basic knowledge of linux, know how to use the shell, and most importantly, you host your site on your own VPS. In this post, I will talk about an easy way to enable gzip compression on apache web servers. It’s really not that difficult, Let’s start.
Step 1. First verify that the module is loaded in your httpd.conf file.
Your current version of Apache may already have enabled this module for you, but you may still want to be sure. To enable this module, add the following line (if it does not exist) in Apache configuration file.
1 | LoadModuledeflate_modulemodules/mod_deflate.so |
Step 2. Configure httpd.conf file.
Open httpd.conf file using a text editor and place the following in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #nano /etc/httpd/conf/httpd.conf # mod_deflate configuration <IfModulemod_deflate.c> # Restrict compression to these MIME types AddOutputFilterByTypeDEFLATEtext/plain AddOutputFilterByTypeDEFLATEtext/html AddOutputFilterByTypeDEFLATEapplication/xhtml+xml AddOutputFilterByTypeDEFLATEtext/xml AddOutputFilterByTypeDEFLATEapplication/xml AddOutputFilterByTypeDEFLATEapplication/x-javascript AddOutputFilterByTypeDEFLATEtext/javascript AddOutputFilterByTypeDEFLATEtext/css # Level of compression (Highest 9 - Lowest 1) DeflateCompressionLevel9 # Netscape 4.x has some problems. BrowserMatch^Mozilla/4gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch^Mozilla/4\.0[678]no-gzip # MSIE masquerades as Netscape, but it is fine BrowserMatch\bMSI[E]!no-gzip!gzip-only-text/html </IfModule> |
Step 3. Restart Apache and Verify.
1 2 3 4 5 | ## CentOS ## servicehttpdrestart ## Ubuntu ## /etc/init.d/apache2restart |
You can use this link to verify your server gzip using one of domain configured on server.