Home » Centos/RHEL » Nginx RTMP Media server With Nginx-RTMP module On Centos

Nginx RTMP Media server With Nginx-RTMP module On Centos

Real Time Messaging Protocol (RTMP) is a trademarked protocol, formerly developed by Macromedia.
Macromedia now owned by Adobe. RTMP can do real-time streaming of video, audio, and data between a server and Flash player. However Adobe has declared that it will no longer support Flash, RTMP remains a usually used protocol for live streaming within production workflows.

RTMP: Real Time messaging protocol works on top of TCP and uses port number 1935 by default.
RTMPS, is a secure procedure of RTMP over a TLS/SSL connection.
RTMPE, The RTMPE is RTMP encrypted by Adobe’s own security mechanism. While the details of the operation are proprietary, the mechanism uses industry standard cryptographic primitives.
RTMP Media server With Nginx RTMP module.
https://www.nginx.com/products/nginx/modules/rtmp-media-streaming/
HLS and RTMP can easily integrates with the Nginx web server without any issue using the Nginx RTMP module.
Nginx RTMP is an Nginx module which permits you to enhance HLS and RTMP streaming to your media server. So in this tutorial we are going to implement RTMP Media server With Nginx RTMP module On Centos.
http://www.linuxpcfix.com/nginx-load-balancer-on-centosrhelfedora/
First of we need to install all dependencies which are required by nginx-rtmp module.

[root@linuxp~]#yum install git wget gcc gcc-c++ tar gd gd-devel perl-ExtUtils-Embed geoip geoip-devel pcre pcre-devel openssl openssl-devel zlib zlib-devel –y

Next, you have to download the pcre package execute the following to do so.

[root@linuxp~]#cd /usr/local/src
[root@linuxp~]#wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz

Extract the downloaded file.

[root@linuxp~]#tar -xvf pcre-8.42.tar.gz
[root@linuxp~]#cd pcre-8.42
[root@linuxp~]#./configure
[root@linuxp~]#make && make install
[root@linuxp~]#cd ../

Next, you have to download the zlib package and extract it using the following commands.

[root@linuxp~]#wget https://www.zlib.net/zlib-1.2.11.tar.gz

Extract the downloaded file.

[root@linuxp~]#tar -xvf zlib-1.2.11.tar.gz
[root@linuxp~]#cd zlib-1.2.11
[root@linuxp~]#./configure
[root@linuxp~]#make && make install

Next, download the OpenSSL package and extract it.

[root@linuxp~]#wget https://www.openssl.org/source/openssl-1.0.2q.tar.gz
[root@linuxp~]#tar -xvf openssl-1.0.2q.tar.gz
[root@linuxp~]#cd openssl-1.0.2q
[root@linuxp~]#./config && make && make install

Download Nginx and require other Additional Package

You have installed all required dependencies , so now download and install Nginx source with some other additional packages.
To do so you need to change your current directory to ‘/usr/local/src’ by the following command.

[root@linuxp~]#wget https://nginx.org/download/nginx-1.14.2.tar.gz

Next, you will need to extract the above-downloaded file using the following command.

[root@linuxp~]#tar -xvf nginx-1.14.2.tar.gz

After that, clone/pull the Nginx RTMP Module source code executing the git command.

[root@linuxp~]#git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git

Compile and Install Nginx

After downloading the Nginx with all the other required additional packages. So, let’s install and configure Nginx. To do so change your current directory to ‘nginx-1.14.2’.

[root@linuxp~]#cd nginx-1.14.2
[root@linuxp~]#./configure –add-module=../nginx-rtmp-module –sbin-path=/usr/sbin/nginx –lock-path=/var/run/nginx.lock –conf-path=/etc/nginx/nginx.conf –pid-path=/run/nginx.pid –with-pcre=../pcre-8.42 –with-zlib=../zlib-1.2.11 –with-openssl=../openssl-1.0.2q –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –user=nginx –group=nginx –with-http_auth_request_module –with-http_degradation_module –with-http_geoip_module –with-http_gunzip_module –with-http_gzip_static_module –with-http_image_filter_module –with-http_mp4_module –with-http_perl_module –with-http_realip_module –with-http_secure_link_module –with-http_slice_module –with-http_ssl_module –with-http_stub_status_module –with-http_v2_module –with-stream_ssl_module –with-stream –with-threads –prefix=/etc/nginx
[root@linuxp~]#make && make install
[root@linuxp~]#cp nginx-1.14.2/man/nginx.8 /usr/share/man/man8
[root@linuxp~]#cp man/nginx.8 /usr/share/man/man8
[root@linuxp~]#gzip /usr/share/man/man8/nginx.8
[root@linuxp~]#useradd –system –home /var/lib/nginx –shell /sbin/nologin –comment “nginx system user” nginx
[root@linuxp~]#mkdir /var/log/nginx && sudo chown nginx:nginx /var/log/nginx
[root@linuxp~]#vi /lib/systemd/system/nginx.service

Append the following line to generate new startup file.

#############################################################
[Unit]
Description=nginx – high performance web server
Documentation=https://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target

####################################################################

After creating new system daemon file run the following command to update the kernel about changes.

[root@linuxp~]#systemctl daemon-reload
[root@linuxp~]#systemctl start nginx
[root@linuxp~]#systemctl enable nginx
[root@linuxp~]#cd /etc/nginx/
[root@linuxp~]#cp nginx.conf nginx.conf_backup
[root@linuxp~]#rm -rf nginx.conf
[root@linuxp~]# vi nginx.conf

And append the following lines in your nginx.conf

user nginx;
worker_processes auto;
#server_tokens off;
events {
worker_connections 1024;
}
# We need to setup an rmtp server to stream video from client devices
rtmp {
server {
listen 1935;
chunk_size 4096;
ping 30s;
notify_method get;
allow play all;
# rmtp handler our clients connect to for live streaming, it runs on port 1935. It converts the stream to HLS and stores it on our server
application live {
allow play all;
allow publish 172.31.39.38; #nginx
# allow publish 192.100.1.101; #encoder
live on;
exec ffmpeg -i rtmp://172.31.39.38/live/$name -threads 1
-c:v libx264 -profile:v baseline -b:v 768K -s 640×360
-vf “drawtext= fontcolor=red: fontsize=20: fontfile=/usr/share/fonts/dejavu/DejaVuSans.ttf: text=’360′: x=10: y=10:”
-f flv -c:a aac -ac 1 -strict -2 -b:a 96k rtmp://172.31.39.38:1935/show/$name_360
-c:v libx264 -profile:v baseline -b:v 1024K -s 960×540
-vf “drawtext= fontcolor=red: fontsize=20: fontfile=/usr/share/fonts/dejavu/DejaVuSans.ttf: text=’480′: x=10: y=10:”
-f flv -c:a aac -ac 1 -strict -2 -b:a 128k rtmp://172.31.39.38:1935/show/$name_480
-c:v libx264 -profile:v baseline -b:v 1920K -s 1280×720
-vf “drawtext= fontcolor=red: fontsize=20: fontfile=/usr/share/fonts/dejavu/DejaVuSans.ttf: text=’720′: x=10: y=10:”
-f flv -c:a aac -ac 1 -strict -2 -b:a 128k rtmp://172.31.39.38:1935/show/$name_72;
}
application show {
live on;
hls on;
hls_path /var/www/hls/live;
hls_fragment 10s;
allow play all;
allow publish 127.0.0.1;
allow publish 172.31.39.38;
# allow publish 192.100.1.101;
dash on;
dash_path /var/www/dash;
dash_fragment 10;
# Instruct clients to adjust resolution according to bandwidth
hls_variant _240 BANDWIDTH=288000;
hls_variant _360 BANDWIDTH=448000;
hls_variant _480 BANDWIDTH=1152000;
hls_variant _720 BANDWIDTH=2048000;
#hls_variant _1080 BANDWIDTH=4096000;
# hls_variant _low BANDWIDTH=288000; # Low bitrate, sub-SD resolution
# hls_variant _mid BANDWIDTH=448000; # Medium bitrate, SD resolution
# hls_variant _high BANDWIDTH=1152000; # High bitrate, higher-than-SD resolution
# hls_variant _hd720 BANDWIDTH=2048000; # High bitrate, HD 720p resolution
# hls_variant _src BANDWIDTH=4096000; # Source bitrate, source resolution
hls_nested on; # create a new folder for each stream
record_notify on;
record_path /var/www/videos;
record all;
record_unique on;
}

application vod {
play /var/www/videos;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
server {
server_name yourdomainc.com;
# add_header ‘Cache-Control’ ‘no-cache’;
# add_header ‘Access-Control-Allow-Origin’ ‘*’;
# add_header ‘Access-Control-Allow-Origin’ *;
# add_header ‘Access-Control-Expose-Headers’ ‘Content-Length’;
# add_header ‘Access-Control-Max-Age’ 1728000;
# add_header ‘Access-Control-Allow-Headers’ ‘Origin,Range,Accept-Encoding,Referer,Cache-Control’;
# add_header ‘Access-Control-Expose-Headers’ ‘Server,Content-Length,Content-Range,Date’;
# add_header ‘Access-Control-Allow-Methods’ ‘GET, HEAD, POST, OPTIONS, PUT, DELETE’;
location / {
root html;
add_header Cache-Control no-cache;
add_header ‘Access-Control-Allow-Origin’ ‘*’;
index index.html index.htm;
#rewrite ^(.*)$ /crossdomain.xml;
}
# To access the http end point our web based users to connect and see the live stream
location /live {
types {
application/vnd.apple.mpegurl m3u8;
video/MP2T ts;
application/dash+xml mpd;
}
alias /var/www/hls/live;
add_header Cache-Control no-cache;
add_header ‘Access-Control-Allow-Origin’ ‘*’;
}

listen 80;
server_name 172.31.39.38 yourdomainc.com;
root /var/www/html;
}
}

Afer putting the following config file. Run the following command on your server terminal.

[root@linuxp~]#mkdir -p /var/www/hls/live
[root@linuxp~]#cd /var/www/
[root@linuxp~]#ln -s /etc/nginx/html/ html
[root@linuxp~]# systemctl restart nginx

In this tutorial we are going to implement nginx-rtmp streaming server with multi bitrate so that user can access streaming according his internet speed.
To enable multi bitrate we require to install FFMPEG tool, So here are the steps to install FFMPEG.

[root@linuxp~]#rpm -v –import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
[root@linuxp~]#rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
[root@linuxp~]#yum install ffmpeg ffmpeg-devel
[root@linuxp~]#ffmpeg -version
Nginx rtmp media server installation done. You can install OBS broadcaster on your local system to broadcast video/audio to your Nginx rtmp media server. The following url will use for streaming.
OBS URL :: rtmp://server-ip-address:1935/live
VOD URL :: rtmp://server-ip-address/videos/filename.mp4
HLS URL :: http://server-ip-address/live/_360/index.m3u8
http://server-ip-address/live/_480/index.m3u8
http://server-ip-address/live/_720/index.m3u8

About

I am founder and webmaster of www.linuxpcfix.com and working as a Sr. Linux Administrator (Expertise on Linux/Unix & Cloud Server) and have been in the industry since more than 14 years.

2 thoughts on “Nginx RTMP Media server With Nginx-RTMP module On Centos

  1. Eskimo says:

    hls fragmented files deleted after a while i upload them to the server!
    here is my hls config:
    # Define the Application
    application show {
    live on;
    # Turn on HLS
    hls on;
    hls_path /mnt/hls/;
    hls_cleanup off;
    hls_playlist_length 10y;
    hls_fragment 3;
    ……
    Any Idea!TNX.

    1. Admin says:

      The hls fragment files automatically deleted once broadcasting stopped

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Time limit is exhausted. Please reload the CAPTCHA.

Categorized Tag Cloud