How do I enable Brotli compression in WordPress? This tutorial will help you how to install Brotli on Ubuntu server. If you are using multiple PHP, this tutorial will help you install brotli manually. Why using Botli? You may need Brotli extension for W3 Total Cache WordPress plugin.
Let’s get started. Check your PHP version:
php --version
PHP 7.4.16 (cli) (built: Jul 5 2021 13:04:38) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies
I have version 7.4
Installing Brotli and php-dev
sudo apt-get install brotli php-dev -y
brotli --version
brotli 1.0.9
Clone the repository
cd /tmp
git clone --recursive --depth=1 https://github.com/kjdev/php-ext-brotli.git
cd php-ext-brotli
phpize
./configure
sudo make install
It created a module file brotli.so saved in /usr/lib/php/ [version] /brotli.so
In my system, brotli.so is here:
ls /usr/lib/php
20190902
ls /usr/lib/php/20190902
brotli.so
Add php mod
cd /etc/php
ls
7.4
cd 7.4
cd mods-available
echo "extension=brotli.so" > brotli.ini
sudo phpenmod brotli
Check if it work
phpquery -v 7.4 -s apache2 -m brotli
brotli (Enabled for apache2 by local administrator)
Or you can also check using php interactive mode
php -a
php > echo is_callable('brotli_compress');
1
php >
It should return 1.
Finally, don’t forget this command:
sudo service apache2 restart
That’s it, you are done.
Build Brotli Php Extension Manually
If you have multiple php installed, you may need to build brotli manually. For instance, I have Php 7.2, 7.4 and 8.0. But you want to use Php 7.4. You need to build brotli php extension manually. Here’s how:
cd /tmp
git clone --recursive --depth=1 https://github.com/kjdev/php-ext-brotli.git
cd php-ext-brotli
phpize
Find the php config
ls /usr/bin/php-config*
#/usr/bin/php-config /usr/bin/php-config7.4
#/usr/bin/php-config.default /usr/bin/php-config8.0
My php config file is /usr/bin/php-config7.4
Then run this:
./configure --with-php-config=/usr/bin/php-config7.4
sudo make
sudo make install
#Installing shared extensions: /usr/lib/php/20190902/
Add brotli ini file
cd /etc/php/7.4/mods-available
sudo nano brotli.ini
Paste this and save:
extension=brotli.so
Run this command:
sudo phpenmod brotli
Check:
phpquery -v 7.4 -s apache2 -m brotli
#brotli (Enabled for apache2 by local administrator)
Dont forget this final command:
sudo service apache2 restart