How to Set Up a Video Streaming Server using Nginx-RTMP with HLS and DASH on a Linux Server

Lakshitha Perera
4 min readSep 9, 2024

--

Introduction

Video streaming has become a cornerstone of modern digital experiences, from live broadcasts and online classes to entertainment platforms and social media. For developers and content creators looking to build their own streaming infrastructure, having a robust and scalable streaming server is essential.

This guide will walk you through setting up a video streaming server using Nginx with the RTMP (Real-Time Messaging Protocol) module on a Linux server. This setup allows you to ingest video streams using RTMP and deliver them to viewers using HLS (HTTP Live Streaming) and DASH (Dynamic Adaptive Streaming over HTTP), ensuring compatibility with a wide range of devices, including desktops, mobile phones, and smart TVs.

Why Use Nginx with RTMP, HLS, and DASH?

  • Nginx with RTMP Module: Nginx is a powerful, lightweight web server known for its high performance and ability to handle numerous simultaneous connections. By adding the RTMP module, Nginx becomes a capable streaming server for live video content using the RTMP protocol. RTMP is a low-latency protocol that works well for live streaming.
  • HLS and DASH: While RTMP is excellent for ingesting live streams, it is not natively supported by most modern browsers and mobile devices. HLS (developed by Apple) and DASH (an open standard) are adaptive bitrate streaming protocols that provide high compatibility and quality for video delivery across all major platforms and devices. Converting RTMP to HLS and DASH enables seamless playback on nearly all web and mobile clients.

Prerequisites

To follow this guide, you will need:

  • A Linux server (with sudo access).
  • Basic knowledge of using the terminal and command-line tools.
  • A domain name or IP address is needed to access the server.

Step 1: Install Required Dependencies

First, ensure your system is up to date and install the necessary build tools and libraries required for compiling Nginx with the RTMP module.

sudo apt update
sudo apt upgrade -y
sudo apt install -y build-essential libpcre3 libpcre3-dev libssl-dev zlib1g zlib1g-dev unzip

Step 2: Download and Install Nginx with the RTMP Module

Nginx does not include the RTMP module by default, so you need to download Nginx and the RTMP module source code and compile them together.

1. Download Nginx and the RTMP Module Source Code:

cd /usr/local/src
wget http://nginx.org/download/nginx-1.21.6.tar.gz # Use the latest version available
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

2. Extract the Downloaded Files:

tar -zxvf nginx-1.21.6.tar.gz
unzip master.zip

3. Compile Nginx with the RTMP Module:

Navigate to the extracted Nginx folder and configure it to include the RTMP module:

cd nginx-1.21.6
./configure --add-module=../nginx-rtmp-module-master --with-http_ssl_module
make
sudo make install

** This will install Nginx to /usr/local/nginx.

Step 3: Configure Nginx with RTMP, HLS, and DASH Support

1. Create Required Directories for HLS and DASH:

Create directories where the HLS and DASH fragments will be stored:

sudo mkdir -p /usr/local/nginx/html/stream/hls
sudo mkdir -p /usr/local/nginx/html/stream/dash

2. Edit the Nginx Configuration File:

Open the Nginx configuration file in a text editor:

sudo nano /usr/local/nginx/conf/nginx.conf

Replace the contents or add the following configuration:

3. Save and Exit the editor (Ctrl + X, then Y and Enter).

Step 4: Start Nginx

Start Nginx using the installed path:

sudo /usr/local/nginx/sbin/nginx

To restart or reload Nginx after making changes:

sudo /usr/local/nginx/sbin/nginx -s reload

Step 5: Test the Configuration with OBS and Access the DASH Stream

1. Set Up OBS for Streaming:

Open OBS Studio (or any other RTMP encoder) and configure it to stream to your Nginx server:

  • Server URL: rtmp://your_server_ip/stream
  • Stream Key: test

Replace your_server_ip with the IP address or domain of your server.

2. Start Streaming in OBS.

  • Access the DASH Stream in a Video Player:
  • Once OBS is streaming, access the DASH stream via:
http://your_server_ip/dash/test.mpd

Use a DASH-compatible player like VLC Media Player or set up a simple HTML page with Video.js to play the stream in a browser.

Step 6: Ensure Ports are Open

Ensure that ports 80 (HTTP) and 1935 (RTMP) are open in your firewall:

sudo ufw allow 80/tcp
sudo ufw allow 1935/tcp

Conclusion

By following these steps, you have successfully set up a video streaming server using Nginx with the RTMP module on a Linux server. With this configuration, you can stream live video using RTMP and serve it to a wide audience using the HLS and DASH protocols, ensuring compatibility across various devices and platforms.

Now, you have the foundation to build your own custom video streaming platform or integrate live streaming into your existing services.

Happy streaming!

--

--

Lakshitha Perera
Lakshitha Perera

Written by Lakshitha Perera

I’m a passionate individual and a technophile obsessed with the latest technologies. I prefer to find new solutions using the latest approaches and systems. ✌👦

Responses (1)