This is the easiest method to enable Brotli compression on VestaCP. This tutorial works on any VestaCP setup either Nginx PHP-fpm stack or Nginx-Apache stack.
Objective: To install and to enable Brotli compression on Nginx VestaCP.
Requirements:
- VestaCP installed;
- A website created;
- A domain resolved to VestaCP server;
- SSL configured properly.
Activate Brotli on VestaCP Server
Step 1. Login to your server through SSH command line.
Step 2. Install the build dependencies on to the system:
Ubuntu:
apt update -y
apt install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev unzip
CentOS:
yum update -y
yum install pcre-devel cmake git gcc zlib zlib-devel -y
Step 3. Create a new file called mkbrotli in the /usr/local/sbin/
:
cd /usr/sbin
nano mkbrotli
Step 4. Copy and paste this bash script inside nano editor:
#!/bin/bash
# https://www.majlovesreg.one/tag/code/
# https://www.majlovesreg.one/adding-brotli-to-a-built-nginx-instance
# Install needed development packages if not yet installed in the system
# sudo apt -y install git libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev
# For predefined NGINX version, use:
# ngver=1.17.1
# For passing the version via the command line (i.e.: [email protected]:~$ ./mkbrotli 1.17.1), use:
ngver=$1
# For automated detection of currently installed NGINX version (not to be used for auto-updating, see hooks in post), use:
# ngver=$(nginx -v 2>&1 | grep -o '[0-9\.]*')
# Get configure parameters of installed NGINX. Not needed if NGINX was configured '--with-compat'.
# Uncomment one of the lines below if the script sucessfully builds modules but NGINX throws a "not binary compatible" error.
# confparams=$(nginx -V 2>&1 | grep -o -- '--prefix='.*)
# confparams=$(nginx -V 2>&1 | grep -o -- '--[^with]'.*)
# confparams=$(nginx -V 2>&1 | grep -- '--' | sed "s/.*' //")
# To manually set NGINX modules directory:
# moddir=/path/to/modules/directory
# To automatically select NGINX modules directory:
[ -d /usr/share/nginx/modules ] && moddir=/usr/share/nginx/modules
[ -d $(nginx -V 2>&1 | grep -o 'prefix=[^ ]*' | sed 's/prefix=//')/modules ] && moddir=$(nginx -V 2>&1 | grep -o 'prefix=[^ ]*' | sed 's/prefix=//')/modules
[ -d $(nginx -V 2>&1 | grep -o 'modules-path=[^ ]*' | sed 's/modules-path=//') ] && moddir=$(nginx -V 2>&1 | grep -o 'modules-path=[^ ]*' | sed 's/modules-path=//')
[ ${moddir} ] || { echo '!! missing modules directory, exiting...'; exit 1; }
# Set temporary directory and build on it
builddir=$(mktemp -d)
cd ${builddir}
echo
echo '################################################################################'
echo
echo "Building Brotli for NGINX ${ngver}"
echo "Temporary build directory: ${builddir}"
echo "Modules directory: ${moddir}"
echo
# Download and unpack NGINX
wget https://nginx.org/download/nginx-${ngver}.tar.gz && { tar zxf nginx-${ngver}.tar.gz && rm nginx-${ngver}.tar.gz; } || { echo '!! download failed, exiting...'; exit 2; }
# Download, initialize, and make Brotli dynamic modules
git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli && git submodule update --init && cd ../nginx-${ngver}
[ ${confparams} ] && nice -n 19 ionice -c 3 ./configure --add-dynamic-module=../ngx_brotli "${confparams}" || nice -n 19 ionice -c 3 ./configure --with-compat --add-dynamic-module=../ngx_brotli
nice -n 19 ionice -c 3 make modules || { echo '!! configure or make failed, exiting...'; exit 4; }
# Replace Brotli in modules directory
[ -f ${moddir}/ngx_http_brotli_filter_module.so ] && sudo mv ${moddir}/ngx_http_brotli_filter_module.so ${moddir}/ngx_http_brotli_filter_module.so.old
[ -f ${moddir}/ngx_http_brotli_static_module.so ] && sudo mv ${moddir}/ngx_http_brotli_static_module.so ${moddir}/ngx_http_brotli_static_module.so.old
sudo cp objs/*.so ${moddir}/
sudo chmod 644 ${moddir}/ngx_http_brotli_filter_module.so || { echo '!! module permissions failed, exiting...'; exit 5; }
sudo chmod 644 ${moddir}/ngx_http_brotli_static_module.so || { echo '!! module permissions failed, exiting...'; exit 6; }
# Clean up build files
cd ${builddir}/..
sudo rm -r ${builddir}/ngx_brotli
rm -r ${builddir}
echo
echo "Sucessfully built and installed latest Brotli for NGINX ${ngver}"
echo "Modules can be found in ${moddir}"
echo "Next step: Configure dynamic modules and reload/restart NGINX."
echo
echo '################################################################################'
echo
# Start/restart NGINX.
# Not recommended if script is hooked since NGINX is automatically restarted by the package manager (e.g. apt) after an upgrade.
# Restarting NGINX before the upgrade could cause a module version mismatch.
# sudo nginx -t && { systemctl is-active nginx && sudo systemctl restart nginx || sudo systemctl start nginx; } || true
# echo
# systemctl --no-pager status nginx
# echo
Step 5. Hit Control+O to save then Control+X to exit the editor.
Step 6. Now find out the current version of Nginx installed by VestaCP. Issue this command:
nginx -v
Sample output:
nginx version: nginx/1.17.5
It means the currently running version of Nginx is v1.17.5.
Step 7. Install Brotli on Nginx using this command:
chmod +x mkbrotli
./mkbrotli 1.17.5
Replace 1.17.5 with your own version of nginx, for instance, v1.15.0
chmod +x mkbrotli
./mkbrotli 1.15.0
Sit tight and wait for the script to execute its tasks installing Brotli compression technology on Nginx.
Step 8. Find the Nginx global configuration file using this command:
nginx -V 2>&1 | grep -o 'conf-path=[^ ]*' | sed 's/conf-path=//'
In our case, the file is located at /etc/nginx/nginx.conf
(we use VestaCP Nginx-Apache stack on Ubuntu 18.04).
Step 9. Edit the Nginx configuration file using an editor. You can use Vi, Vim, or Nano:
nano /etc/nginx/nginx.conf
Step 10. Add the following two lines at the very top of the config file:
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
Step 11. Find the “http” block in the configuration file. You can use Control+W in the Nano editor. Type in http then hit Enter to find.
Step 12. Copy and paste following configuration to activate Brotli compression on nginx:
brotli on;
brotli_static on;
brotli_types
text/plain
text/css
text/xml
text/javascript
text/x-component
application/xml
application/xml+rss
application/javascript
application/json
application/atom+xml
application/vnd.ms-fontobject
application/x-font-ttf
application/x-font-opentype
application/x-font-truetype
application/x-web-app-manifest+json
application/xhtml+xml
application/octet-stream
font/opentype
font/truetype
font/eot
font/otf
image/svg+xml
image/x-icon
image/vnd.microsoft.icon
image/bmp;
Step 13. Hit Control+O to save then Control+X to exit the editor.
Step 14. Test nginx configuration to make sure all settings fine:
nginx -t
You’ll see output something like this:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Step 15. Finally, reload or restart nginx:
systemctl restart nginx
Check whether it works properly or not.
Visit the Brotli checker tool by KeyCDN here. Alternatively, you can use curl as well:
curl -H "Accept-Encoding: br" -I https://domain.tld
replace domain.tld with yours. The output will look similar to this:
HTTP/2 200
server: nginx
date: Wed, 30 Oct 2019 07:41:25 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
link: <https://miui.blog/wp-json/>; rel="https://api.w.org/"
content-encoding: br
That’s all.
Verdict
The method above is the easiest one that we tried. Credits belong to Majal Mirasol for the script.
The only drawback occurs when updating nginx. When Nginx automatically upgrades via a package manager (e.g. apt
, yum
, or pacman
) or you update it manually, the modules will not load. Why? Because Nginx is set not to load modules that were built for a different version.
The solutions? You can either reinstall the modules or use a script to do the upgrade as suggested by Mirasol.