⚙️ How to install Brotli module for Nginx on Ubuntu 20.04+
Tags: devops, ubuntu, tutorial, beginners
Published: 2020-06-26 21:49:06

## 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_).

<a name="toc"></a>
### 📝 Table of contents

- [Quick guide](#ch-1)
- [A long, but detailed guide](#ch-2)
  - [Install dependencies](#ch-2-1)
  - [Download the source code](#ch-2-2)
  - [Build Brotli module with Nginx](#ch-2-3)
  - [Add Brotli config](#ch-2-4)
- [Questions for better understanding](#ch-3)

<a name="ch-1"></a>
## ⚡️ Quick guide

Check, that you have [Python](https://www.python.org/) (at least version `3.5`) and [Ansible](https://github.com/ansible/ansible) (I recommend the `2.9` version) on your local machine.

Next, download ZIP archive (or `git clone`) [Useful playbooks](https://github.com/truewebartisans/useful-playbooks) GitHub repository and go to the downloaded (or cloned) folder.

{% github truewebartisans/useful-playbooks no-readme %}

Select Ansible playbook called `install_brotli-playbook.yml` (see [docs](https://github.com/truewebartisans/useful-playbooks/blob/master/docs/install_brotli.md)) and run it with `--user` and `--extra-vars` arguments, like this:

```bash
ansible-playbook \
                  install_brotli-playbook.yml \
                  --user <USER> \
                  --extra-vars "host=<HOST>"
```

- `<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](#toc)

<a name="ch-2"></a>
## 📚 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! 😉

<a name="ch-2-1"></a>
### ✅ Install dependencies

```bash
sudo apt install \
                  git gcc cmake libpcre3 libpcre3-dev \
                  zlib1g zlib1g-dev openssl libssl-dev
```

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

<a name="ch-2-2"></a>
### ✅ Download the source code

First, download and unpack Nginx:

```bash
wget https://nginx.org/download/nginx-<VERSION>.tar.gz
tar zxvf nginx-<VERSION>.tar.gz
```

> 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](https://github.com/google/ngx_brotli):

```bash
git clone https://github.com/google/ngx_brotli.git
```

The folder structure should look like this at the moment:

```console
.
├── nginx-<VERSION>/
│   └── ...
└── ngx_brotli/
    └── ...
```

<a name="ch-2-3"></a>
### ✅ Build Brotli module with Nginx

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

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

# Make
sudo make modules
```

> ☝️ Please wait, this may take some time!

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

```bash
sudo cp ./objs/*.so /usr/share/nginx/modules
```

<a name="ch-2-4"></a>
### ✅ Add Brotli config

Add `load_module` section to the start of Nginx config:

```conf
# /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;
}
```

Now, add the Brotli config:

```conf
# /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;
```

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](#toc)

<a name="ch-3"></a>
## 💬 Questions for better understanding

1. What is Ansible used for? _Please read the official [docs](https://docs.ansible.com/ansible/latest/index.html)._
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](#toc)

<a name=""></a>
## Photos/Images by

- Jacob Miller (link: [1](https://unsplash.com/photos/ot5kWZkH97s))

<a name=""></a>
## P.S.

If you want more articles (like this) on this blog, then post a comment below and subscribe to me. Thanks! 😻

And of course, you can help me make developers' lives even better! Just connect to one of my projects as a contributor. It's easy!

My projects that need your help (and stars) 👇

- 🔥 [gowebly][gowebly_url]: A next-generation CLI tool for easily build amazing web applications with Go on the backend, using htmx & hyperscript and the most popular atomic/utility-first CSS frameworks on the frontend.
- ✨ [create-go-app][cgapp_url]: Create a new production-ready project with Go backend, frontend and deploy automation by running one CLI command.
- 🏃 [yatr][yatr_url]: Yet Another Task Runner allows you to organize and automate your routine operations that you normally do in Makefile (or else) for each project.
- 📚 [gosl][gosl_url]: The Go Snippet Library provides snippets collection for working with routine operations in your Go programs with a super user-friendly API and the most efficient performance.
- 🏄‍♂️ [csv2api][csv2api_url]: The parser reads the CSV file with the raw data, filters the records, identifies fields to be changed, and sends a request to update the data to the specified endpoint of your REST API.
- 🚴 [json2csv][json2csv_url]: The parser can read given folder with JSON files, filtering and qualifying input data with intent & stop words dictionaries and save results to CSV files by given chunk size.

<!-- Footer links -->

[gowebly_url]: https://github.com/gowebly
[cgapp_url]: https://github.com/create-go-app
[yatr_url]: https://github.com/koddr/yatr
[gosl_url]: https://github.com/koddr/gosl
[csv2api_url]: https://github.com/koddr/csv2api
[json2csv_url]: https://github.com/koddr/json2csv