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:
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.
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 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.
The next step is to compile and install Nginx with the RTMP module enabled. To stay organized, create a directory for the process.
cd ~/Make/
Next, clone the nginx-RTMP module:
Now we need to download nginx.
Head over to http://nginx.org/en/download.html and select your download option.
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 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.
Enter the RTMP support block as:
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.
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:
Play the stream using FFplay or VLC Media player on the resource.
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.