Install nginx from source on ubuntu with nchan

Saurabh Mhatre
CodeClassifiers
Published in
3 min readDec 17, 2016

Nginx is an open source and high-performance HTTP server.Nchan is a scalable, flexible pub/sub server for the modern web, built as a module for the Nginx. It can buffer messages in memory, on-disk, or via in memory database Redis.

While you can easily install nginx directly using apt-get command, you might need extra modules like pagespeed and nchan to add extra functionalities to nginx. There are two ways to add modules to nginx i.e. either to download and add modules while building nginx from source or adding modules dynamically.

In this tutorial we are going to cover the first method in detail.

You need ubuntu installed with super user privileges to install them.

The steps are as follows:

Install dependencies:

sudo apt-get install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev unzip

Drop into root shell:

sudo bash

Add nginx build source:

gedit /etc/apt/sources.list.d/nginx.list

Add following lines:

If ubuntu version is 14:

deb http://nginx.org/packages/ubuntu/ trusty nginx
deb-src http://nginx.org/packages/ubuntu/ trusty nginx

If ubuntu verion is 16:

deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx

Download nginx latest verion from repo:

cd /opt/
mkdir -p nginx_source
cd nginx_source/
apt-get source nginx

Download nchan:

mkdir -p ~/opt/nchan/
cd ~/opt/nchan/

Get latest nchan release zip from github

Save it in /opt/nchan/ and unzip the file:

unzip nchan-1.0.8.zip

Go to nginx source directory and add rules as follows:

cd ~/opt/nginx_source/nginx-1.10.1/debian/
gedit rules

Add the code — add-module=../../../../nchan/nchan-1.0.8 at the end of config.status.nginx as follows:

config.status.nginx: config.env.nginx
cd $(BUILDDIR_nginx) && \
CFLAGS="" ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-file-aio --with-threads --with-ipv6 --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-cc-opt="$(CFLAGS)" --with-ld-opt="$(LDFLAGS)"
--add-module=../../../../nchan/nchan-1.0.8
touch $@

Build nginx from source:

cd ~/opt/nginx_source/nginx-1.10.1/
dpkg-buildpackage -b

Install generated nginx.deb file as follows:

cd ~/opt/nginx_source/
dpkg -i nginx_nginxversion_amd64.deb

If there are any pipe issues then run force overwrite as follows:

dpkg -i --force-overwrite nginx.deb

Check nginx version and test configuration:

nginx -V
nginx -t

Edit nginx config file:

cd /etc/nginx/conf.d/
gedit default.conf

Add the following lines below location block in server:

server {
listen 80;
server_name localhost;
...
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#Add following lines
location = /sub {
nchan_subscriber;
nchan_channel_id $arg_id;
}
location = /pub {
nchan_publisher;
nchan_channel_id $arg_id;
}
...

Test your nginx config and restart nginx:

nginx -t
service nginx restart

Test nchan publisher:

curl --request POST --data "some message" http://127.0.0.1:80/pub

If your response is:

queued messages: 1
last requested: 0 sec. ago
active subscribers: 0

Then nchan is setup successfully. You can use npm modules like wscat to test messaged passed to subscribers.

If you would like to know about adding modules dynamically to nginx during installation head over this article.

Please mention your recommendations/doubts/appreciations in the comments section and share the article of you like it and click the heart icon if you like it.

Check out my other articles here:

--

--

Saurabh Mhatre
CodeClassifiers

Senior Frontend Developer with 9+ years industry experience. Content creator on Youtube and Medium. LinkedIn/Twitter/Instagram: @SaurabhNative