RTMP, or “Real-Time Messaging Protocol,” is a robust data transmission technology that enables seamless online video streaming. This protocol transmits video content from an encoder to an online video hosting platform, utilizing TCP and a three-way handshake for secure data transport. RTMP supports a wide range of media types, including live television broadcasts, streaming videos, and online telephony services.
Key Features of RTMP
- High-quality live video production with minimal buffering.
- Simple installation and configuration process.
- Broad compatibility with numerous live streaming software and video services.
In this tutorial, you will learn how to set up a video streaming server on an Ubuntu 22.04 system using Nginx-RTMP.
Prerequisites
- An Ubuntu 22.04 server.
- Root access to the server.
Step 1: Install Nginx Web Server
First, ensure that the Nginx web server is installed. If it’s not already installed, execute the following command:
apt install nginx -y
Afterward, start the Nginx service and enable it at boot:
systemctl start nginx systemctl enable nginx
Verify the status of the Nginx service using:
systemctl status nginx
Expected 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.
Step 2: Install and Configure Nginx-RTMP
The Nginx-RTMP module is available in Ubuntu’s default repository. Install it with:
apt install libnginx-mod-rtmp -y
Then, edit the main Nginx configuration file to define your stream:
nano /etc/nginx/nginx.conf
Add the following configuration at the end of the file:
rtmp { server { listen 1935; chunk_size 4096; allow publish all; application live { live on; record off; } } }
Save changes, close the file, and verify the configuration:
nginx -t
Expected output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart Nginx to apply the changes:
systemctl restart nginx
Verify that Nginx-RTMP listens on port 1935:
ss -antpl | grep 1935
Expected 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))
Step 3: Stream Video to the RTMP Server
Use FFmpeg to stream a sample video to your RTMP server. First, install the necessary tools:
apt install python3-pip ffmpeg -y pip install youtube-dl
Download a video from YouTube:
youtube-dl https://www.youtube.com/watch?v=oNEwEQ0uU1Y
Stream the downloaded video to your RTMP server:
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 &
Step 4: Stream Video to VLC Player
To play the RTMP stream on VLC, launch VLC, navigate to Media > Open Network Stream…, and enter your RTMP server URL.
If successful, you will see your video:
Step 5: Configure RTMP Statistics for Monitoring
Set up a statistics page to monitor your RTMP stream.
Create a directory for the stat file:
mkdir /var/www/html/rtmp
Copy the RTMP stat file to the directory:
cp /usr/share/doc/libnginx-mod-rtmp/examples/stat.xsl /var/www/html/rtmp/stat.xsl
Edit the Nginx default virtual host configuration file:
nano /etc/nginx/sites-available/default
Replace its content with:
server { listen 8080; server_name _; location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /var/www/html/rtmp; } location /control { rtmp_control all; } }
Save the file and restart Nginx:
systemctl restart nginx
Step 6: Access RTMP Stat Page
To view RTMP statistics, open your web browser and navigate to http://your-server-ip:8080/stat.
Refresh the page periodically to monitor stream statistics updates.
Conclusion
This tutorial guided you through the process of setting up a video streaming server using Nginx-RTMP on Ubuntu 22.04. You also learned how to test the server using VLC player. With this setup, you can stream any video to the RTMP server and view it with supported media players. Feel free to reach out if you have any questions.
Frequently Asked Questions (FAQ)
1. What is RTMP used for?
RTMP is primarily used for streaming audio and video content over the internet, especially for live broadcasts.
2. Is RTMP compatible with most streaming software?
Yes, RTMP is broadly compatible with a wide range of streaming software and services, making it a versatile choice for many applications.
3. How can I verify that my server is listening on the correct port?
You can check which ports are being listened to using the command ss -antpl | grep port-number
. For RTMP, you can replace port-number
with 1935.
4. Can VLC player open RTMP streams by default?
Yes, VLC player can play RTMP streams by selecting the “Open Network Stream” option and entering the appropriate RTMP URL.
5. Why should I monitor RTMP statistics?
Monitoring RTMP statistics helps in understanding the performance and health of your streaming server, enabling timely troubleshooting and optimization.