Ubuntu 18.04 compile Nginx + RTMP + VOD + HLS

This tutorial will explain how to setup your own streaming server on ubuntu 18.04 with RTMP, HLS and VOD add on module.
Firtsly, we prepare nginx source, download from http://nginx.org/en/download.html

#wget http://nginx.org/download/nginx-1.14.1.tar.gz
#tar -zxvf nginx-1.14.1.tar.gz

Clone source nginx-rtmp-module and nginx-vod-module

#git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
#git clone https://github.com/kaltura/nginx-vod-module.git

Install nginx dependencies

#sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

Download zlib from http://zlib.net/

#wget http://zlib.net/zlib-1.2.11.tar.gz
#tar -zxxvf zlib-1.2.11.tar.gz

compile nginx

#cd nginx
#./configure --add-module=../nginx-rtmp-module/ --add-module=../nginx-vod-module/ --with-file-aio --with-threads --with-zlib=../zlib-1.2.11
#make
#make install

move to nginx installation folder and edit nginx.conf

#cd /usr/local/nginx/
#nano conf/nginx.conf

add rtmp server on nginx configuration

rtmp {
    server {
        listen 1935;
        chunk_size 4000;
        allow play all;
        application live {
            allow play all;
            live on;
            record all;
            record_path /film;
        }
        #allows you to play your recordings of your live streams using a URL like "rtmp://my-ip:1935/vod/filename.flv"
        application vod {
            play /film;
        }
        application hls {
            live on;
            # Turn on HLS
            hls on;
            hls_path /mnt/hls/;
            hls_fragment 3;
            hls_playlist_length 60;
            # record stream to folder /film/
            record all;
            record_path /film;
            # disable consuming the stream from nginx as rtmp
			deny play all;
        }
    }
}

Create folder /mnt/hls and /film
Give nginx permission to access that folder

add hls streaming config on htto section at nginx configuration

server {
    listen 8080;

    location /hls {
        # Disable cache
        add_header Cache-Control no-cache;

        # CORS setup
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length';

        # allow CORS preflight requests
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }

        types {
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }

        root /mnt/;
    }
}
}

add vod server on http section at nginx configuration

    server {
        listen 8384;

        # vod caches
        vod_metadata_cache metadata_cache 256m;
        vod_response_cache response_cache 128m;

        # vod settings
        vod_mode local;
        vod_segment_duration 2000; # 2s
        vod_align_segments_to_key_frames on;

        #file handle caching / aio
        open_file_cache max=1000 inactive=5m;
        open_file_cache_valid 2m;
        open_file_cache_min_uses 1;
        open_file_cache_errors on;
        aio on;

        location /filmku/ {
            alias /film/;
            vod hls;
            add_header Access-Control-Allow-Headers '*';
            add_header Access-Control-Expose-Headers 'Server,r$
            add_header Access-Control-Allow-Methods 'GET, HEAD$
            add_header Access-Control-Allow-Origin '*';
            expires 100d;
        }
    }

check configuration and run nginx

# /usr/local/nginx/sbin/nginx -
# /usr/local/nginx/sbin/nginx

If you stream video using OBS to rtmp://your_ip/hls/streamname
so you can access your streaming via hls streaming http://your_ip:8080/hls/streamname.m3u8
or with rtmp vod rtmp://yourip/vod/streamname.flv
of with hls vod http://your_ip:8384/filmku/streamname.flv/index.m3u8

Leave a Reply

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