Gzip compression is used to zip or compress webpages in the webserver before serving it to the users’ web browser. Thus it saves network bandwidth because the server sends the compressed form of your webpage over the network. Another advantage of Gzip compression is faster page loading time. When the users’ machines receive the compressed format of webpages, it only takes a few milliseconds to decompress it.
How to check if Gzip is enabled in my server?
Before enabling Gzip for WordPress, you need to make sure that the server is ready to serve compressed files in CentOS. The mod_deflate is a module that allows the server to compress the files in the server before delivering the page to the client machine.
By default mod_deflate modules are enabled in Apache. To make sure check following line in Apache configuration file /etc/httpd/conf/httpd.conf.
LoadModule deflate_module modules/mod_deflate.so
Debian based users can enable Gzip module (mod_deflate) using following command.
$ sudo a2enmod deflate
To confirm if mod_delfact in installed in CentOS you can use the following command
apachectl -M |grep deflate
To check if the mod_deflect is enabled run the following command:
curl -I -H ‘Accept-Encoding: gzip,deflate’ http://yoursite.com
Note: replace the http://yoursite.com by your website’s URL.
If you see “Content-Encoding: gzip” in the output of the above command, it means that module is active. If you don’t see “Content-Encoding: gzip” in the output, you need to activate it by creating a file named deflate.conf file in the /etc/httpd/conf.d/ directory. After creating the file add the following lines in /etc/httpd/conf.d/deflate.conf
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
DeflateCompressionLevel 8
</ifmodule>
Next, reload the apache server
service httpd reload
Finally run the command to check the status of the module.
# curl -I -H ‘Accept-Encoding: gzip,deflate’ http://yoursite.com
The last step is to create a .htacess file in the root directory of WordPress if it is not already created. If your WordPress installation already has a .htaccess file, you need to add the following codes at the end of the file. Do not remove anything that is already in the .htaccess file required by the WordPress.
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
List of useful web tools to verify if Gzip compression is enabled:
- https://varvy.com/tools/gzip/
- https://checkgzipcompression.com/
- http://www.whatsmyip.org/http-compression-test/
- https://gtmetrix.com/
If you want to test from WordPress dashboard to test Gzip compression status, you can install “Check and Enable GZIP compression” plugin. This plugin will give you information on whether Gzip is enabled in for your website.
Remember that enabling Gzip in the server does not mean that your pages will be Gzip compressed. Don’t forget to add the required codes in .htaccess file to tell WordPress to compress the pages.