Contents

Installing Nginx + HLS on Ubuntu 16.04

11 Dec 2017 2 270  0
Installing Nginx + HLS on Ubuntu 16.04
Installing Nginx + HLS on Ubuntu 16.04

Nginx is currently one of the most popular web servers in the entire world and a lot of the largest and highest traffic sites on the internet use it. It is resource-friendly in comparison to Apache in many cases and could be used as a web server.

In this tutorial we will teach you how to install Nginx-RTMP on your Ubuntu 16.04 server or any other Ubuntu versions, this tutorial will work on multiple.

Requirements

Before starting the tutorial, you will need to have a non-root user with sudo privileges configured on your server.

Once you have an account available, log in as your non-root user or root-user to start.

Installing Nginx

Nginx exists in Ubuntu’s repositories, therefore the installation is pretty straight forward.

Install the tools needed to compile Nginx and Nginx-RTMP from source.

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

Next, start downloading Nginx and Nginx RTMP source.

wget http://nginx.org/download/nginx-1.7.5.tar.gz

wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

Now extract Nginx and Nginx-RTMP source.

tar -zxvf nginx-1.7.5.tar.gz

unzip master.zip

Change to the Nginx directory.

cd nginx-1.7.5

Append modules which Nginx is going to be compiled with. Nginx-RTMP is a part of this.

./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master

For the final steps, compile and install Nginx with Nginx-RTMP.

make

sudo make install

Next, install the Nginx init scripts.

sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx

sudo update-rc.d nginx defaults

Then it is time to upgrade the package lists.

sudo apt-get update

Then it is time to upgrade the package lists.

sudo apt-get install ffmpeg

Setup Nginx on Ubuntu to Stream Live HLS Video., this will make it playable on mobile devices.

Begin with creating the directory structures needed in order to hold the live and mobile HLS manifests and video fragments.

sudo mkdir /HLS

sudo mkdir /HLS/live

sudo mkdir /HLS/mobile

sudo mkdir /video_recordings

sudo chmod -R 777 /video_recordings

It is recommended to have your firewall turned on if it is not already. You will need to allow traffic into the ports used by Nginx and HLS. If you want to run without a firewall for now, ignore the ‘UFW’ section in the following.

sudo ufw limit ssh

sudo ufw allow 80

sudo ufw allow 1935

sudo ufw enable

Next, open the Nginx configuration file.

sudo nano /usr/local/nginx/conf/nginx.conf

Delete the code and place this code below instead.

#user  nobody;

worker_processes  1;



error_log  logs/rtmp_error.log debug;

pid        logs/nginx.pid;



events {

    worker_connections  1024;

}



http {

    server {

        listen       80;

        server_name  localhost;



        location /hls {

            # Serve HLS fragments



            # 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 /tmp;

            add_header Cache-Control no-cache;

        }

    }

}



rtmp {

        server {

                listen 1935;

                chunk_size 8192;



                application hls {

                        live on;

                        meta copy;

                        hls on;

                        hls_path /tmp/hls;

        }

    }

}

Now save the file to disk with CTRL + X and then exit.

Before doing anything else, we suggest that you take care of what is called ‘cross-domain’ restrictions, this would cause your ability to be shut down if you wanted to begin streaming to a webpage/website.

Create a ‘crossdomain.xml’ file located in the ‘nginx/html’ directory and place instructions in it to grant data to flow between domains.

sudo nano /usr/local/nginx/html/crossdomain.xml

Start by copying from this page and then pasting the XML data shown below into the Nano editor area.

<?xml version="1.0"?>

<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">

<cross-domain-policy>

<allow-access-from domain="*"/>

</cross-domain-policy>

Now CTRL + O to write out, afterwards CTRL +X to save the file to disk and exit.

That should be everything.

A bit of information about your servers:

FMS URL:  rtmp://your-ip/hls/

– Stream Key: This is your choice and what you set it to is completely up to you.

http://your-ip/hls/stream.m3u8

If you set your stream key to movie then it should look like the below.

http://your-ip/hls/movie.m3u8

This is everything you will need to know.

0 Comments
Add a comment

Leave a Reply

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