How to Setup a Streaming Server with NGINX RTMP Module
Tags: server, nginx, rtmp, streaming
Published: 2020-10-13 15:07:11

In this article, we will discuss how to setup a streaming server with NGINX RTMP module.

If you are looking to buy a streaming server, we have 10gbps dedicated servers to boost your streaming. We provision servers quickly, affordably, and reliably.

An HLS Streaming Server is one of the most popular protocols for streaming videos. HLS is an adaptive streaming platform that allows you to stream media content to the best viewing experience, customized to the user's device and network conditions.

HLS and RTMP can be easily integrated with the Nginx web server using the Nginx RTMP module. In this tutorial, we'll show you how to setup an HLS live streaming server on Ubuntu.

Here we will show you how to configure HLS live streaming server on Ubuntu. It is easy to integrate with the Nginx web server.

Let's get to know about Nginx RTMP.

Nginx RTMP is an Nginx module that allows you to add streaming RTMP and HLS to your media server. The RTMP and HLS modules were previously separate Nginx modules, but now they can all be integrated as a single module to Nginx.

<strong>Prerequisites:</strong>

HostnExtra <a href="https://www.hostnextra.com/10gbps-dedicated-servers.html" target="_blank">10gbps dedicated servers</a> with Ubuntu OS installed.
Let's get started with the installation

<strong>Step 1: Login via SSH to the server:</strong>

Login as root or a user with sudo access on the server.

<strong>Step2: Keep server up-to-date:</strong>

Always keep the server updated and if you the server is new, you should consider updating the system software:

- apt-get update -y

- apt-get upgrade -y

<strong>Step 3: Download required software</strong>

Start by updating the apt repository:

- apt-get install -y git build-essential ffmpeg libpcre3 libpcre3-dev libssl-dev zlib1g-dev

<strong>Step 3: Clone Module</strong>

- git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git

<strong>Step 4: Download Nginx</strong>

Copy the latest download link from the Nginx website and decompress the files:

- wget http://nginx.org/download/nginx-1.17.6.tar.gz
- tar -xf nginx-1.17.6.tar.gz
- cd nginx-1.17.6

<strong>Step 5: Configure Nginx</strong>

- ./configure --prefix=/usr/local/nginx --with-http_ssl_module --add-module=../nginx-rtmp-module
- make -j 1
- make install

<strong>Step 6: Configure Nginx</strong>

- rm /usr/local/nginx/conf/nginx.conf

- vim /usr/local/nginx/conf/nginx.conf

Copy the following contents into the file and save it:

worker_processes auto;
events {
worker_connections 1024;
}

rtmp {
server {
listen 1935; # Listen on standard RTMP port
chunk_size 4000;

application show {
live on;

hls on;
hls_path /mnt/hls/;
hls_fragment 3;
hls_playlist_length 60;

deny play all;
}
}
}

http {
sendfile off;
tcp_nopush on;
directio 512;
default_type application/octet-stream;

server {
listen 8080;

location / {

add_header 'Cache-Control' 'no-cache';

add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length';

if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}

types {
application/dash+xml mpd;
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}

root /mnt/;
}
}
}

<strong>Step 7: Start Nginx</strong>

Start Nginx

- /usr/local/nginx/sbin/nginx

<strong>Step 8: Start Streaming</strong>

This server can stream from a variety of sources including a static file, webcam, etc.

We previously installed ffmpeg. We'll start streaming example-vid.mp4 to our http://localhost/show/stream

- ffmpeg -re -i example-vid.mp4 -vcodec libx264 -vprofile baseline -g 30 -acodec aac -strict -2 -f flv rtmp://localhost/show/stream

<strong>Step 9: Integrate into Player</strong>

The stream can now be integrated into a javascript HLS player as per your requirements.

We have successfully setup a streaming server with NGINX RTMP module.