Nginx is a powerful web server, a reverse proxy server software that powered millions of websites. This lightweight software enables you to serve web pages with minimum resources. Nginx also supports external modules or custom modules to be added and linked to Nginx. Today most Linux distribution supported this web server in their official packages. Ubuntu, for instance, has Nginx in their official package but still lack of Brotli support.

What is Brotli?

Brotli is a new compression algorithm developed by Google that optimized for the web. It has a slightly better compression ratio than Gzip and supported by the vast majority of web browsers. The only drawback compared to Gzip is that Brotli took slower at the compression process.

Getting Started to Compile Nginx with Brotli

To compile the Nginx source, we need to install external libraries, such as the GCC compiler, OpenSSL, Zlib, PCRE library, and many more. Thankfully, Ubuntu offers a utility tool called build-dep to install the Nginx dependencies library. Below is the complete step of how to build Nginx from sources.

Install Nginx Requirements

$sudo apt build-dep nginx

Run command above to install libraries required by Nginx. If it return message like E: You must put some 'source' URIs in your sources.list then make sure to remove # on deb-src list in /etc/apt/sources.list then run apt update -y then re run $sudo apt build-dep nginx

Download Nginx + Ngx_brotli Source Codes

  1. Download and extract the Nginx source codes.
    $wget http://nginx.org/download/nginx-1.17.5.tar.gz && tar -xzf nginx-1.17.5.tar.gz
  2. Download ngx_brotli, a Nginx module for brotli support and pull it’s submodule
    $git clone https://github.com/google/ngx_brotli.git
    $cd ngx_brotli
    $git submodule update --init --recursive
    $cd ..

Configure & Build Nginx + Brotli

So we have the Nginx and Brotli source codes, and it’s time to configure. The configure command tells the compiler what enabled modules, where’s installation path, and many more. For further information about Nginx configure params, check this reference: http://nginx.org/en/docs/configure.html.

Make sure to append your ./configure params with this --add-module=/root/ngx_brotli to link Nginx with Ngx_brotli module.

$cd nginx-1.17.5
$./configure --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-FIJPpj/nginx-1.17.5=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time
e -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-pat
th=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/ngi
inx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_
_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=
=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --w
with-mail=dynamic --with-mail_ssl_module --add-module=/root/ngx_brotli
$make
$sudo make install

So we have Nginx installed in our system. According to our build params, the Nginx binary located in /usr/share/nginx and the temporary path for Nginx internal processing in /var/lib/nginx. Now lets create symbolic link for nginx binary and make sure /var/lib/nginx folder exists.

$sudo ln -s /usr/share/nginx/sbin/nginx /usr/sbin/nginx
$sudo mkdir -p /var/lib/nginx
$nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Yay! Nginx is configured properly and it’s time to create systemd entry for Nginx.

$sudo vim /lib/systemd/system/nginx.service

Paste the following config and save

[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

Enable and start the nginx process

$sudo systemctl enable nginx.service
$sudo systemctl start nginx.service

Useful links

Leave a Reply

Your email address will not be published. Required fields are marked *