Set Up live streaming server with rtmp nginx
Table of contents
Open Table of contents
Prerequisites and usage
- Ubuntu server 20.04
- OBS (Free and open source software for video recording and live streaming)
- Nginx rtmp module
- Nano
Step 1: Install Nginx and the RTMP Module
-
Update the system:
sudo apt update sudo apt upgrade -y
-
Install the necessary dependencies:
sudo apt install build-essential libpcre3 libpcre3-dev libssl-dev zlib1g zlib1g-dev -y
-
Download and install Nginx:
cd /usr/local/src wget http://nginx.org/download/nginx-1.21.0.tar.gz tar -zxvf nginx-1.21.0.tar.gz cd nginx-1.21.0
-
Download and install the RTMP module:
git clone https://github.com/arut/nginx-rtmp-module.git
-
Compile Nginx with the RTMP module:
./configure --with-http_ssl_module --add-module=../nginx-rtmp-module make sudo make install
Step 2: Configure Nginx with the RTMP Module
-
Open the Nginx configuration file:
sudo apt install nano sudo nano /usr/local/nginx/conf/nginx.conf
-
Add the RTMP configuration to the
nginx.conf
file:worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # nodejs server received notify server { listen 80; server_name sv.example.com; client_max_body_size 64M; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_redirect off; proxy_buffering off; proxy_pass http://127.0.0.1:8000; } } server { listen 80; server_name live.example.com; location /hls { alias /var/www/hls; types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Headers' 'Range' always; add_header 'Cache-Control' 'no-cache' always; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always; } location /vod { alias /var/www/rtmp; autoindex on; } location /html { alias /var/www/html; } # rtmp stat location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } rtmp { server { listen 1935; ping 30s; application src { live on; # push to rtmp://localhost/live/$name; exec_push ffmpeg -i rtmp://localhost/src/$name -vcodec libx264 -vprofile baseline -g 10 -acodec aac -ar 44100 -ac 1 -f flv rtmp://localhost/live/$name 2>>/var/log/ffmpeg-$name.log; } application live { live on; sync 10ms; # notify publisher exec_push curl http://localhost:8000/api/livestream/onplay?status=exec_push&name=$name; # notify when publish done exec_publish_done curl http://localhost:8000/api/livestream/onplay?status=exec_publish_done&name=$name; # convert recorded file to mp4 format exec_record_done ffmpeg -y -i $path -acodec libmp3lame -ar 44100 -ac 1 -vcodec libx264 $dirname/$basename.mp4; # sample recorder recorder rec1 { record all; record_path /var/www/rtmp; } # sample HLS hls on; hls_cleanup off; hls_path /var/www/hls; hls_sync 100ms; hls_continuous on; } } }
-
Save and exit the editor.
Step 3: Start Nginx
-
Start Nginx:
sudo /usr/local/nginx/sbin/nginx
-
Check Nginx status:
sudo /usr/local/nginx/sbin/nginx -t
If everything is working correctly, you will see messages saying “syntax is ok” and “test is successful”.
Step 4: Live Stream
To start live streaming, you can use OBS (Open Broadcaster Software) or any streaming software that supports RTMP.
-
Install OBS:
- Download and install OBS from the OBS Project website.
-
Configure OBS:
- Open OBS, go to
Settings
->Stream
. - Select
Service
asCustom...
. - Enter
Server
asrtmp://<YOUR_SERVER_IP>/live
. - Enter
Stream Key
as any string you want, for example:stream
.
- Open OBS, go to
Step 5: Check the Live Stream
Open a browser or any software that supports RTMP video playback and enter the URL: rtmp://<YOUR_SERVER_IP>/live/stream
If you are streaming, you will see your video playing. With this, you have completed the setup of a live streaming server with RTMP and Nginx.