DEV Community

Cover image for ⚙️ How to install Brotli module for Nginx on Ubuntu 20.04+
Vic Shóstak
Vic Shóstak

Posted on

⚙️ How to install Brotli module for Nginx on Ubuntu 20.04+

Introduction

Welcome back, DEV people! 💬 How about to find out once and for all how to install the Brotli module for Nginx on Ubuntu 20.04+?

Fasten your seatbelts, because we're starting! 🚀 I will show you a fast automated way (which I use myself) and more detailed (and manual).

📝 Table of contents

⚡️ Quick guide

Check, that you have Python (at least version 3.5) and Ansible (I recommend the 2.9 version) on your local machine.

Next, download ZIP archive (or git clone) Useful playbooks GitHub repository and go to the downloaded (or cloned) folder.

GitHub logo truewebartisans / useful-playbooks

🚚 Useful Ansible playbooks for easily deploy your website or webapp to absolutely fresh remote virtual server and automation many processes. Only 3 minutes from the playbook run to complete setup server and start it.

Select Ansible playbook called install_brotli-playbook.yml (see docs) and run it with --user and --extra-vars arguments, like this:

ansible-playbook \
                  install_brotli-playbook.yml \
                  --user <USER> \
                  --extra-vars "host=<HOST>"
Enter fullscreen mode Exit fullscreen mode
  • <USER> is the remote user's username (for example, root)
  • <HOST> is the host name in your inventory (from /etc/ansible/hosts file)

This playbook will determine the installed version of Nginx itself, download the necessary dependencies, build and configure Brotli module and add the load_module section to Nginx configuration (/etc/nginx/nginx.conf).

Enjoy! 😎

↑ Table of contents

📚 A long, but detailed guide

If you like longer and more detailed stories, as well as I do, let's start with a simple step: login to your remote virtual server running on Ubuntu 20.04+ (via SSH, for example).

And we're ready! 😉

✅ Install dependencies

sudo apt install \
                  git gcc cmake libpcre3 libpcre3-dev \
                  zlib1g zlib1g-dev openssl libssl-dev
Enter fullscreen mode Exit fullscreen mode

If you already have some packages (from the list above) installed, you do not need to install them again.

✅ Download the source code

First, download and unpack Nginx:

wget https://nginx.org/download/nginx-<VERSION>.tar.gz
tar zxvf nginx-<VERSION>.tar.gz
Enter fullscreen mode Exit fullscreen mode

Where <VERSION> is the Nginx version, like 1.17.10. You can check version on your remote server by running nginx -v command.

Second, git clone Brotli module from official Google repository:

git clone https://github.com/google/ngx_brotli.git
Enter fullscreen mode Exit fullscreen mode

The folder structure should look like this at the moment:

.
├── nginx-<VERSION>/
│   └── ...
└── ngx_brotli/
    └── ...
Enter fullscreen mode Exit fullscreen mode

✅ Build Brotli module with Nginx

Go to ./nginx-<VERSION> folder and run:

# Configure dynamic module
sudo ./configure --with-compat --add-dynamic-module=../ngx_brotli

# Make
sudo make modules
Enter fullscreen mode Exit fullscreen mode

☝️ Please wait, this may take some time!

Finally, copy ready module *.so files from ./nginx-<VERSION>/objs to the Nginx modules folder:

sudo cp ./objs/*.so /usr/share/nginx/modules
Enter fullscreen mode Exit fullscreen mode

✅ Add Brotli config

Add load_module section to the start of Nginx config:

# /etc/nginx/nginx.conf

# Load module section
load_module "modules/ngx_http_brotli_filter_module.so";
load_module "modules/ngx_http_brotli_static_module.so";

# ...

events {
  # ...
}

http {
  # ...

  # Include configs folders
  include /etc/nginx/conf.d/*.conf;
  include /usr/share/nginx/modules/*.conf;
}
Enter fullscreen mode Exit fullscreen mode

Now, add the Brotli config:

# /etc/nginx/conf.d/brotli.conf

# Enable Brotli
brotli            on;
brotli_static     on;
brotli_comp_level 6;

# File types to compress
brotli_types application/atom+xml application/javascript application/json application/rss+xml
             application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype
             application/x-font-ttf application/x-javascript application/xhtml+xml application/xml
             font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon
             image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;
Enter fullscreen mode Exit fullscreen mode

Restart Nginx service and we're done. It just works! 🎉

Congratulations! We managed to install Brotli module for Nginx web server on Ubuntu 20.04+.

↑ Table of contents

💬 Questions for better understanding

  1. What is Ansible used for? Please read the official docs.
  2. What does it take to work with Ansible playbook?
  3. What version of Nginx should your configure the Brotli module with?
  4. Why should you install the gcc and cmake packages to build the Brotli module?

↑ Table of contents

Photos/Images by

  • Jacob Miller (link: 1)

P.S.

If you want more — write a comment below & follow me. Thx! 😘

Discussion (5)

Collapse
odifyltsaeb profile image
Alan

Thanks a bunch! This worked fine, but not without few clithces (on ubuntu 20.04) in DO.
After cloning ngx_brotli, I had to go in the cloned folder and run git submodule update --init because without it I received an error when trying to build modules.

Then the path of modules was different for my install and was /usr/lib/nginx/modules (not /usr/share).

Collapse
koddr profile image
Vic Shóstak Author

Hi, Alan. Great addition to the article! 👍

Unfortunately, (in my practice) it was not necessary to do the actions you described.

Collapse
pepelsbey profile image
Vadim Makeev • Edited

I got the same error with exactly the same solution: to init submodules:

checking for getaddrinfo() ... found
configuring additional dynamic modules
adding module in ../ngx_brotli

./configure: error: Brotli library is missing from the /usr directory.

Please make sure that the git submodule has been checked out:

    cd ../ngx_brotli && git submodule update --init && cd /root/nginx-1.19.10
Enter fullscreen mode Exit fullscreen mode

Once I did that everything went well. It might be that in other tutorials they suggest cloning brotli with git clone --recursive command. Thank you for the article!

Collapse
mariusty profile image
mariusty

Thanks for such a detailed tutorial (as usual)

Collapse
koddr profile image
Vic Shóstak Author

You're welcome (as usual) 🥰