Setup VOD streaming server with nginx using RTMP on Ubuntu 18.04



Install required packages for source code nginx build

# apt-get install -y build-essential ca-certificates wget curl libpcre3 libpcre3-dev autoconf unzip automake libtool tar git libssl-dev zlib1g-dev uuid-dev lsb-release vim htop sysstat ufw fail2ban makepasswd libgd-dev libgeoip-dev -y

create a directory

# mkdir /usr/local/src/nginx/
# cd /usr/local/src/nginx/

download and extract nginx tar

# wget -qO- http://nginx.org/download/nginx-1.14.2.tar.gz | tar zxf -

clone nginx rtmp module

# git clone https://github.com/arut/nginx-rtmp-module

Download other modules

- PCRE version 8.42
# wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz && tar xzvf pcre-8.42.tar.gz

- zlib version 1.2.11
# wget https://www.zlib.net/zlib-1.2.11.tar.gz && tar xzvf zlib-1.2.11.tar.gz

- OpenSSL version 1.1.0h
# wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz && tar xzvf openssl-1.1.0h.tar.gz

# cd nginx-1.14.2/

configure  nginx with modules and options

# ./configure --add-module=/usr/local/src/nginx/nginx-rtmp-module/ --with-http_ssl_module --prefix=/usr/local/nginx-streaming/ --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --user=www-data --group=www-data --with-http_ssl_module --with-http_v2_module --with-http_mp4_module   --with-http_auth_request_module --with-http_stub_status_module --with-http_gunzip_module --with-http_realip_module  --with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module=dynamic --with-stream_ssl_preread_module --with-pcre=../pcre-8.42 --with-pcre-jit --with-zlib=../zlib-1.2.11 --with-http_flv_module --with-select_module --with-poll_module --with-http_image_filter_module --with-http_flv_module

# make

# make install

# mkdir -p /etc/nginx-sites-enabled

create nginx service  systemd file

# vim /lib/systemd/system/nginx.service

paste bellow content

[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecStartPost=/bin/sleep 0.1
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

save and exit
create required directories

# mkdir -p /var/cache/nginx/client_temp
# mkdir -p /var/www/mp4s
# mkdir -p /var/www/flvs

copy rtmp nginx stat xml

# cp /usr/local/src/nginx/nginx-rtmp-module/stat.xsl /var/www/
create original nginx.conf backup

mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf_orig

create new nginx.conf

vim /etc/nginx/nginx.conf

paste bellow content

user www-data;
worker_processes auto;
worker_rlimit_nofile 80000;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 200000;
    multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 30;
    types_hash_max_size 2048;
    server_tokens off;
    reset_timedout_connection on;
    client_max_body_size 500M;
    log_not_found off;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

rtmp {
    server {
        listen 1935;

        chunk_size 4000;

        # video on demand for flv files
        application vod {
            play /var/www/flvs;
        }

        # video on demand for mp4 files
        application vod2 {
            play /var/www/mp4s;
                live on;
                meta copy;
                hls on;
                hls_path /tmp/hls;
 
       }
    }
}

save and exit

create one nginx virtual host file as stream.conf

vim /etc/nginx/sites-enabled/stream.conf

psate bellow content

server {
        # in case we have another web server on port 80
        listen      8080;
        root /var/www;
        # This URL provides RTMP statistics in XML
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            # XML stylesheet to view RTMP stats.
            # Copy stat.xsl wherever you want
            # and put the full directory path here
            root /var/www/;
        }

        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias /tmp/app;
            expires -1;
        }
    }

save and exit

check for possible syntax error in nginx

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

above output shows we are good to go.

restart service

# systemctl restart nginx

done !

If you wish to check a demo

cd /var/www/mp4s

wget http://vod.leasewebcdn.com/bbb.mp4

open vlc and click in media  ->  open network stream -> paste bellow url

rtmp://<IP_OF_THE_SERVER>:1935/vod2/bbb.mp4

hit play  and done !

Comments

Popular posts from this blog

using libguestfs virt-customize tool to modify qcow2 image and reset root password.

Running cockpit behind nginx reverse proxy with nginx ssl and cockpit non ssl

setting up openshift alert manager mail alerting based on critical and warning