How To Install Nginx RTMP Server + HLS on Ubuntu

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

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

Download the Nginx and Nginx-RTMP source.


Install the Unzip package.

sudo apt-get install unzip

Extract the Nginx and Nginx-RTMP source.

tar -zxvf nginx-1.14.0.tar.gz

Switch to the Nginx directory.

cd nginx-1.14.0

Add modules that Nginx will be compiled with. Nginx-RTMP is included.

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

Compile and install Nginx with Nginx-RTMP.

Apache config:
sudo make install

Now Install the Nginx init scripts.

sudo wget -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults

Update the package lists.

sudo apt-get update

Install FFmpeg.

sudo apt-get install ffmpeg

Create the folder structures necessary 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’s probably a good idea to have your firewall turned on if you haven’t done so already. If so, you must allow traffic into the ports used by Nginx and HLS. If you’d like to run without the firewall for now, ignore the ufw section below.

sudo ufw limit ssh
sudo ufw allow 80
sudo ufw allow 1935
sudo ufw enable

Now open the nginx configuration file

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

Remove the code and open this file and add this code to it

#user nobody;
worker_processes  1;

error_log  logs/rtmp_error.log debug;
pid        logs/;

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

Ctrl + X to save the file to disk and exit.

Before you do anything else, it’s important to take care of what is called “cross-domain” restrictions, which would otherwise shut down your ability to stream to a webpage/website. Create a crossdomain.xml file in your nginx/html folder and put instructions in it to allow data to flow between domains:

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

First copy (from this page) and then paste (right-click) into the nano editor field the following XML data:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "">
<allow-access-from domain="*"/>

Press Ctrl + O to write out, then Ctrl + X to save the file to disk and exit.
And your done!

little info about your servers
FMS URL: rtmp://your-ip/hls/

Stream Key: ANY thing you like, I usually set it to stream.


again if you set your stream key to movie then it would look something like this

Tags: HLS, How To Install Nginx RTMP Server + HLS on Ubuntu, Nginx RTMP server, tutorial, Ubuntu

More Similar Posts

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Most Viewed Posts