How to Install Nginx from Source with njs (JavaScript) Module
Along with certbot
To enable the JavaScript language subset njs
along with Nginx, we can install from source. In this guide, I’ll provide info on this setup for Ubuntu 20.04.1 LTS.
First check the downloads page here for the latest stable version: http://nginx.org/en/download.html
We’ll be downloading version 1.18.0, which can be done via:
$ cd ~
$ wget http://nginx.org/download/nginx-1.18.0.tar.gz
$ tar -zxvf nginx-1.18.0.tar.gz
System Prep
Make sure your host machine is up to date and install some necessary packages:
$ sudo apt-get update
$ sudo apt-get install build-essential
$ sudo apt-get install libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
If you have already installed Nginx via apt-get, make sure to uninstall before installing from source:
$ sudo apt-get remove nginx nginx-common
JavaScript Module
Next we’ll download mercurial
in order to get the source code for the njs
module (ngx_http_js_module).
$ sudo apt-get install mercurial
Now clone the njs
module code, configure, and run make
.
$ cd ~
$ hg clone http://hg.nginx.org/njs
$ cd njs
$ sudo ./configure
$ sudo make
Install Nginx
The host machine is now ready to install Nginx from source. Navigate to the download directory and configure the install.
$ cd ~/nginx-1.18.0
To see the options for the modules that can be included upon configuration use the help
option:
$ ./configure --help
I’m going to include the following modules/libraries with my install:
- PCRE library
- ngx_http_ssl_module
- ngx_http_v2_module
- ngx_http_auth_request_module
- ngx_http_js_module
The final configuration command ends up being:
$ ./configure --sbin-path=/usr/bin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-pcre --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_auth_request_module --modules-path=/usr/local/nginx/modules --with-http_v2_module --add-module=$HOME/njs/nginx
Note that I’m including the ngx_http_js_module
using the --add-module
option, which means that it will not be installed as a dynamic module. This also means that the load_module modules/ngx_http_js_module.so
directive does not need to be used within my nginx configuration file.
After configuration completes successfully, run make
and install
:
$ sudo make
$ sudo make install
Finally, check that that nginx has been install correctly and start it up:
$ which nginx
/usr/bin/nginx$ sudo nginx
You can run $ ps aux | grep nginx
to make sure the master and worker processes are running. That’s it!
Bonus: Install Certbot
The certbot installation instructions can be found from certbot.eff.org. More specifically, for ubuntu check here: https://certbot.eff.org/lets-encrypt/ubuntufocal-nginx
As a quick reference, here are a list of commands from their guide I typically use to install certbot:
$ sudo snap install core; sudo snap refresh core
$ apt-get remove certbot
$ sudo snap install --classic certbot
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot