Friday 15 February 2019

Deploying NGINX with RTMP module on Ubuntu 18.04 LTS



In this post, I will discuss how to deploy NGINX with the RTMP module for media streaming on Ubuntu 18.04 LTS.

Firstly, what is NGINX (pronounced as Engine-X)?

NGINX is an open source web server known for its high performance, stability, rich feature set, simple configuration and low resource consumption. Created by Igor Sysoev, the first public release was in 2004 (En.wikipedia.org, n.d.).

NGINX operates in modules and has two different types: core (native) and third-party (external).
  • The core modules are built by the core NGINX developers and they are bundled as part of the software itself.
  • Third-party modules are built by the NGINX community, these third-party modules are used to extend the functionality of NGINX. Over a hundred third-party modules are currently available. Notable among the third-party modules is the RTMP module developed by Roman Arutyunyan which is used to enable the streaming capabilities of NGINX.
The RTMP module delivers streams over the RTMP protocol and dynamically converts RTMP into HLS and MPEG-DASH in real time. This module is available on GitHub (https://github.com/arut/nginx-rtmp-module)
There are two main types of NGINX.
  1. NGINX Open Source (nginx.org)
  2. NGINX Plus (nginx.com)
NGINX Plus is available commercially and offers additional features not found in NGINX Open source.

Compiling and Installing NGINX with RTMP Module for media streaming.


Now, let's walk through a step by step guide on how to compile and install NGINX with RTMP module.

Before NGINX can be installed, an operating system on the target machine is needed. This can either be a cloud platform or a local PC with any OS of choice.

For this exercise, a VM instance with Ubuntu 18.04 on Google Cloud Platform is used.

A GCP dashboard showing the streaming media VM instance

N: B - Various deployment of different types of media streaming servers were carried out as part of the practical exercise in this module, check my previous post (EXERCISE: Deploying Streaming Servers).

So, with the operating system in place, we are ready to get the NGINX complied and installed.

Compiling NGINX from source offers a lot more flexibility and allows third-party modules to be added during the installation.

Full documentation is available online on how to compile NGINX from source. https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/#sources

NGINX requires 3 main dependencies for the default build configuration. These 3 libraries need to be installed first, before NGINX. they are; 
  1. OpenSSL (or LibreSSL or BoringSSL) - Required by the NGINX SSL module and others.
  2. Zlib - Required by the NGINX Gzip module.
  3. PCRE -  Required by the NGINX Core and Rewrite modules.

Let’s get to work. (N: B – A short video the installation and testing process is attached at the end of this post)

Installation procedures 

  • Update the operating system packages
sudo apt update && sudo apt upgrade

  • Install a compiler tool for the operating system
sudo apt install build-essential git libpcre3 libpcre3-dev libssl-dev libperl-dev

  • Install NGINX Dependencies
# OpenSSL
wget http://www.openssl.org/source/openssl-1.1.1b.tar.gz
tar -zxf openssl-1.1.1b.tar.gz
cd openssl-1.1.1b
./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
make
sudo make install

# Zlib
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
sudo make install

# PCRE
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz
tar -zxf pcre-8.42.tar.gz
cd pcre-8.42
./configure
make
sudo make install
  • Clone nginx-rtmp-module
git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
I found this forked of the RTMP module better, It's being actively worked on and contains more fixes and improvements over the original which is available at https://github.com/arut/nginx-rtmp-module

  • Download NGINX source files
wget http://nginx.org/download/nginx-1.15.10.tar.gz
tar zxf nginx-1.15.10.tar.gz
cd nginx-1.15.10
NGINX maintains 2 different versions; stable and mainline. For this installation, the mainline version 1.15.10 was used. For the latest NGINX check http://nginx.org/en/download.html .
  • Configure the NGINX build.
./configure --with-http_ssl_module --add-module=../nginx-rtmp-module
During the configure stage, modules that NGINX will be compiled with are added. For this installation, the NGINX RTMP Module is added.
  • Compile and install NGINX
make
sudo make install
  • Install the NGINX init scripts to start on boot
sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults
  • After the installation is finished, start NGINX Open Source
sudo service nginx start
  • Create the folder structures necessary to hold the HLS & DASH manifests and video fragments:
sudo mkdir /tmp/hls sudo mkdir /tmp/dash
  • Open and edit the NGINX configuration file according to your requirements
sudo nano /usr/local/nginx/conf/nginx.conf
Setting up the NGINX config file for streaming is probably the most important part of the deployment process.To stream live, the live application must be defined in the NGINX config file.
“live on” turns on live mode.
HLS and DASH should also be ON for HTTP streaming.

The image below shows an example of a config file with the live mode, HLS and DASH turned ‘ON’
  • OPTIONAL: Install FFmpeg
sudo apt install ffmpeg

  • Clean up all .tar.gz files.
rm -rf *.tar.gz

With the installation done, its time to test the streaming server

Post a Comment

Whatsapp Button works on Mobile Device only

Start typing and press Enter to search