How to Set Up a Video Streaming Server using Nginx-RTMP on Ubuntu 22.04
RTMP also called “real-time messaging protocol” is a data transmission technology that supports live online video streaming. It is used to transmit video files from an encoder to an online video hosting platform. RTMP uses TCP to travel and uses a three-way handshake when transporting data. RTMP is used with many different types of media including live television broadcasts, streaming video, and internet phone services.
Features
- Produce high-quality live videos without buffering
- Easy to install and configure.
- Compatible with most live streaming software and video services.
This tutorial will show you how to set up a video streaming server using the Nginx-RTMP on Ubuntu 22.04 server.
Requirements
- A server running Ubuntu 22.04.
- A root password is set up for your server.
Install Nginx Web Server
Before starting, the Nginx web server must be installed on your server. If not installed, you can install it with the following command.
apt install nginx -y
After installing the Nginx package, start the Nginx service and enable it to start at system reboot.
systemctl start nginx
systemctl enable nginx
You can verify the Nginx service status with the following command.
systemctl status nginx
You will get the following output.
? nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-01-09 14:54:55 UTC; 28s ago
Docs: man:nginx(8)
Process: 43294 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
Main PID: 43005 (nginx)
Tasks: 3 (limit: 4575)
Memory: 8.5M
CPU: 35ms
CGroup: /system.slice/nginx.service
??43005 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
??43295 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
??43296 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
Jan 09 14:54:55 vultr systemd[1]: Starting A high performance web server and a reverse proxy server...
Jan 09 14:54:55 vultr systemd[1]: Started A high performance web server and a reverse proxy server.
Jan 09 14:55:04 vultr systemd[1]: Reloading A high performance web server and a reverse proxy server...
Jan 09 14:55:04 vultr systemd[1]: Reloaded A high performance web server and a reverse proxy server.
Install and Configure Nginx-RTMP
By default, the Nginx-RTMP package is included in the Ubuntu default repository. You can install it with the following command.
apt install libnginx-mod-rtmp -y
Once Nginx-RTMP package is installed, edit the Nginx main configuration file and define your stream.
nano /etc/nginx/nginx.conf
Add the following lines at the end of the file:
rtmp
server
listen 1935;
chunk_size 4096;
allow publish all;
application live
live on;
record off;
Save and close the file then verify the Nginx for any configuration error using the following command.
nginx -t
You should see the following output.
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Finally, restart the Nginx service to apply the changes.
systemctl restart nginx
At this point, Nginx-RTMP is installed and listens on port 1935. You can check it with the following command.
ss -antpl | grep 1935
You should see the following output.
LISTEN 0 511 0.0.0.0:1935 0.0.0.0:* users:(("nginx",pid=43388,fd=8),("nginx",pid=43387,fd=8),("nginx",pid=43386,fd=8))
Send Video to RTMP Server
Next, you will need to download a sample video from YouTube and send it to your RTMP server using FFmpeg.
First, install youtube-dl ad FFmpeg package with the following command.
apt install python3-pip ffmpeg -y
pip install youtube-dl
Next, download any video from YouTube using the youtube-dl command line tool.
youtube-dl https://www.youtube.com/watch?v=oNEwEQ0uU1Y
Once the video is downloaded to your system, you can send the downloaded video to the RTMP server using the FFmpeg command.
ffmpeg -re -i "How to Install Ubuntu 22.04 LTS-oNEwEQ0uU1Y.mp4" -c:v copy -c:a aac -ar 44100 -ac 1 -f flv rtmp://your-server-ip/live/stream &
At this point, your downloaded video is sent to rtmp://your-server-ip/live/stream
Stream Video to VLC Player
In this section, we will show you how to stream your video from the RTMP server on VLC player software.
First, log in to your desktop system and open the VLC player. Then, click on Media => Open Network Stream. You should see the following screen.
Define your RTMP server URL and click on the Play button. If everything works, you should see your video on the following screen.
Configure RTMP Statistics for Monitoring
At this point, your RTMP server is installed and working. Now, you will need to configure the RTMP statistics page to monitor your streaming.
First, create a directory to save the RTMP stat file using the following command.
mkdir /var/www/html/rtmp
Next, copy the RTMP stat file to the created directory.
cp /usr/share/doc/libnginx-mod-rtmp/examples/stat.xsl /var/www/html/rtmp/stat.xsl
Next, edit the Nginx default virtual host configuration file.
nano /etc/nginx/sites-available/default
Remove all default lines and add the following lines:
server
listen 8080;
server_name _;
# rtmp stat
location /stat
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
location /stat.xsl
root /var/www/html/rtmp;
# rtmp control
location /control
rtmp_control all;
Save and close the file when you are done. Then, restart the Nginx service to apply the changes.
systemctl restart nginx
Access RTMP Stat Page
Now, open your web browser and access the RTMP stat page using the URL http://your-server-ip:8080/stat. You should see the RTMP Statistics on the following screen.
You will need to refresh the page every time when you stream a video and watch as the stream statistics change.
Conclusion
This post explained how to set up a video streaming server using the Nginx-RTMP on Ubuntu 22.04. We also test the RTMP server via VLC player. You can now stream any video to the RTMP server and access it from any supported media player. Feel free to ask me if you have any questions.
https://www.computingpost.com/how-to-set-up-a-video-streaming-server-using-nginx-rtmp-on-ubuntu-22-04/?feed_id=25830&_unique_id=63c28f3638d25