Wordpress

Install PHP “mod_brotli” on Apache to Reduce WordPress Page Load Time

The mod_brotli filter allows a compressed output of javascript, css and other static resources. It’s a php module which allows compression before the file is sent to the client browser. Due to its advanced compression algorithm, Brotli compression serves better when compared with Gzip. Furthermore, it also reduced the page load time. The faster the page loads, the better user experience if offers.

How to: Enable mod_brotli compression in WordPress

Brotli should be installed on your server. This tutorial would help you install Brotli in CentOS 7 using terminal. It’s important that you have ssh access of your server. In case you don’t have ssh access, make sure you contact your hosting provider and let him know that you need ssh for Brotli installation.

Make sure you have entered the root mode, or alternatively you can use sudo command for gaining super user permission, just as used below.

sudo yum install Brotli
sudo yum install php-brotli

Run the above two commands, one by one. First command would install Brotli, provided you’ve enabled the repository. The second command would install mod_brotli for PHP.

Now, check if Brotli is working by running the following command:

echo is_callable('brotli_compress');

The output of the above command should return as 1, this shows that Brotli is working on your server. Now, check which modules are enabled in php, run the following command:

php -m

In the output, you would see brotli along with other enabled and installed php modules. Normally, in CentOS, php modules configuration is kept in php.d directory under etc folder.

How to: Enable mod_Brotli in W3 Total Cache

We are going to use W3 Total Cache to enable Brotli compression in WordPress. Other way to do it, is to use apache configuration or .htaccess file. Since, this guide is using WordPress as target CMS; therefore, we’d use W3 Total Cache which would automatically add required .htaccess code to enable mod_brotli.

  1. Login to your WordPress dashboard and navigate to Performance > Browser Cache. Navigate down, and look for Enable HTTP (brotli) compresssion.
  2. Tick to enable Brotli compress, make sure you untick Enable HTTP (gzip) compression. This would enable Brotli compression on your WordPress website, Don’t forget to save the settings.

Conclusion:

Gzip is mostly used by servers to compress output. Normally, 300kb file is served with 200kb size which saves bandwidth and reduced page load time. Brotli is something new, which has advanced compression algorithm. Further, in the configuration of Brotli, Compression Output Level can be set. It can range from level 1 to level 10. Greater the compression level, greater is cpu used and slower is the process. You can read more about mod_brotli at Apache server documentation. Incase you don’t understand something, make sure you ask for help in comment section.