Nginx

How to Setup Nginx RTMP

Over the last decade, live streaming has become more and more popular as users communicate with family and friends, watch content online, and perform business operations. Although there are popular services and platforms to perform live streams, it can be challenging—and costly—to set up a private session.

This tutorial seeks to solve that by showing you how to set up a personal live streaming server that takes advantage of Nginx, a powerful and lightweight web server and reverse proxy and the RTMP protocol.

Before we proceed, ensure:

  • You are running Ubuntu or Debian server
  • Have access to your server via SSH
  • Root user or sudo privileges

Let us get started.

What Is Nginx

As mentioned above, Nginx is a fast and lightweight open-source web-server and reverse proxy used to serve simple static websites or massive applications. Nginx is a high-performance web-server with incredible speed and security. Thanks to its light-weight nature, it a very efficient choice for streaming services and traffic with massive traffic.

You can learn more about Nginx from the resource provided below:

https://nginx.org/en/docs/

What is Nginx RTMP

Nginx RTMP is an open-source and powerful module for the Nginx web server. It enables RTMP protocol, HTTP Live Streaming (HLS), and Dynamic Adaptive Streaming over HTTP (DASH).

You can learn more about HLS and DASH from the resource page:

https://www.cloudflare.com/learning/video/what-is-mpeg-dash/

Now that you know what Nginx and Nginx RTMP are, let us set up the server:

Configure to Server

The first step is to gain access to the server. Ensure SSH to your server is enabled and you have the keys added for easier login.

ssh debian@34.73.160.42
debian@streaming:~$

Once we have access to the server, we can start installing the required packages and libraries. First, ensure that your server is up to date:

sudo apt-get update
sudo apt-get upgrade -y

Installing Required Software

To set up the streaming server, we will need to build Nginx from the source and enable the RTMP module. For this, we shall use build-essentials and other packages. Use the apt command below to install all dependencies.

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

The next step is to compile and install Nginx with the RTMP module enabled. To stay organized, create a directory for the process.

mkdir ~/Make
cd ~/Make/

Next, clone the nginx-RTMP module:

git clone git://github.com/arut/nginx-rtmp-module.git

Now we need to download nginx.

Head over to http://nginx.org/en/download.html and select your download option.

wget http://nginx.org/download/nginx-1.19.8.tar.gz
tar xzf nginx-1.19.8.tar.gz
cd nginx-1.19.8

The final step in this section is to compile Nginx with the RMTP module enabled. Use the commands below:

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

Setting up Live Streaming

To enable the RTMP support, we need to edit the nginx configuration file and specify the RTMP block.

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

Enter the RTMP support block as:

rtmp {
    server {
        listen 1935;
        ping 30s;
        application live {
            live on;
            interleave on;
 
            hls on;
            hls_path /tmp/hls;
            hls_sync 100ms;
            hls_fragment 15s;
        }
    }
}
 
http {
    default_type application/octet-stream;
 
    server {
        listen 80;
        location /tv {
            root /tmp/hls;
        }
    }
 
    types {
        application/vnd.apple.mpegurl m3u8;
        video/mp2t ts;
        text/html html;
    }
}

In the above configuration, we enable the RTMP module and HLS. Consider the documentation to learn how to enable DASH.

Once you have the configuration, save the file and start the Nginx service.

sudo /usr/local/nginx/sbin/nginx

Start Streaming

Once you have everything configured correctly, you can start streaming using FFmpeg—it’s the easiest. You can also configure other tools like OBS studio, but I will not cover that in this tutorial.

Use the FFmpeg command as:

sudo ffmpeg -re -i sample-mp4-file.mp4 -c copy -f hls rtmp://localhost/live/stream

Play the stream using FFplay or VLC Media player on the resource.

ffplay rtmp://IP/live/stream

Once you have the stream running, you can expose the service to an outside network.

NOTE: Ensure you have secured your server before allowing public access.

Conclusion

This tutorial has covered how to set up a streaming server using Nginx and the Nginx RTMP module. Keep in mind that there are many gears involved in setting up a streaming service, and you can customize it according to your needs.

Consider the following resources to explore more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list