How to set up Nginx+RTMP live stream server

9 November 2018

How to set up Nginx+RTMP live stream server

IMPORTANT UPDATE! These guide may be outdated. Please see the new guide here:
http://www.ustoopia.nl/featured/nginx-rtmp-hls-ssl-videojs-on-ubuntu-18-04/
AN EVEN MORE IMPORTANT UPDATE! This is the most recent guide I've put together: https://livestream.puntenel.nl/knowledge-base/create-a-secure-adaptive-bitrate-hls-stream-with-nginx-rtmp-ffmpeg-videojs-on-ubuntu-20-04/

I made a couple of Youtube video's with some instructions on how to setup your own private live-stream server using Nginx running on Ubuntu 18.04. . Click on the CONTINUE READING link below to see a copy of the variables that I used in the video's. You can also find it on my Pastebin if you prefer.

I'm assuming you have already installed Ubuntu 18.04. If not, please take a look at this following video first.

FIRST INSTALL THESE USEFUL / REQUIRED PACKAGES

sudo apt-get install wget unzip software-properties-common dpkg-dev git make gcc automake build-essential zlib1g-dev libpcre3 libpcre3-dev libssl-dev libxslt1-dev libxml2-dev libgd-dev libgeoip-dev libgoogle-perftools-dev libperl-dev pkg-config autotools-dev gpac ffmpeg mediainfo mencoder lame libvorbisenc2 libvorbisfile3 libx264-dev libvo-aacenc-dev libmp3lame-dev libopus-dev unzip

ADD THE NGINX REPOSITORY AND UPDATE

sudo add-apt-repository ppa:nginx/stable
apt update

INSTALL NGINX

sudo apt install nginx

INSTALL THE RTMP MODULE

sudo apt install libnginx-mod-rtmp

CLONE THE RTMP GIT

cd /usr/src
git clone https://github.com/arut/nginx-rtmp-module

COPY THE STAT.XSL FILE TO YOUR WEB FOLDER

cp /usr/src/nginx-rtmp-module/stat.xsl /var/www/html/stat.xsl

CREATE A NEW FILE CALLED CROSSDOMAIN.XML IN YOUR WEB FOLDER

nano /var/www/html/crossdomain.xml

PASTE THIS IN THE NEW FILE AND SAVE IT

<?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>

EDIT THE NGINX CONFIG FILE

nano /etc/nginx/nginx.conf

AT THE END OF THE FILE, ADD THIS:

rtmp {
	server {
	listen 1935;
	chunk_size 4096;

	application live {
		live on;
		record off;
		interleave off;
		wait_key on;
		meta on;
		wait_video off;
		idle_streams off;
		sync 300ms;
		session_relay on;
		#allow publish 127.0.0.1;
		#allow publish 192.168.2.0/24;
		allow publish all;
		#deny publish all;
		allow play all; 

# EDIT THESE SO THE LIVESTREAM_KEY IS REPLACED BY YOUR PERSONAL KEY THAT YOU CAN LOOK UP ON THE SITE OF THE PLATFORM.
# push rtmp://live-ams.twitch.tv/app/LIVESTREAM_KEY;
# push rtmp://a.rtmp.youtube.com/live2/LIVESTREAM_KEY;
# push rtmp://ingest-ams.mixer.com:1935/beam/LIVESTREAM_KEY;
		}
	}
}

EDIT THE DEFAULT SITE CONFIG

nano /etc/nginx/sites-enabled/default

SEARCH FOR THE FOLLOWING LINES:

#location ~ /\.ht {
# deny all;

AND DIRECTLY UNDER IT PASTE THE FOLLOWING

location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /var/www/html/;
}
#location /control {
# you'll need a htpasswd auth file, that's outside the scope of this doc but any apache one will work
# auth_basic "stream";
# rtmp_control all;
#}
SO THAT IT WILL LOOK SOMETHING LIKE THIS AFTER PASTING:
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}

location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /var/www/html/;
}
#location /control {
# you'll need a htpasswd auth file, that's outside the scope of this doc but any apache one will work
# auth_basic "stream";
# rtmp_control all;
#}
}

# Virtual Host configuration for example.com

YOU ARE DONE SETTING UP THE SERVER. HAVE A LOOK AT THE WIKI OF THE RTMP MODULE WEBPAGE TO LEARN HOW TO USE DIFFERENT VARIABLES TO FURTHER SET UP YOUR SERVER. https://github.com/arut/nginx-rtmp-module

Pick a star to rate this
Table of Contents

10 comments on “How to set up Nginx+RTMP live stream server”

  1. How to Live Stream to Multiple Services with a RTMP Server ?
    Ex: 1. 192.168.2.23/live as key: stream
    2. 192.168.2.23/live1 as key: stream2

  2. Hey Dre,
    I just went through your setup instructions. Good work! Everything worked right away. As always, I had tried a lot before. Super structured and good job. A big thank you.

  3. Hi,
    I love your videos, i have built a streaming server thanks to you!! It is set up at the moment to record the stream... how do i record it as an mp4 , which i would then be able to play vod? maybe do a video on this??
    thanks again
    Andrew

  4. Thank you sooooo much !! it works!! final question... for now 🙂 my saved file name is this......
    stream-1604109650-31-Oct-20-02:00:50.mp4
    what the heck is the "stream-1604109650" part??
    cheers
    Andrew

  5. Most probably your stream name is “stream” and record unique file name is set to true, so “-unix timestamp”…

Leave a Reply

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

Unrelated Content

26 June 2025
Why Self-Hosting Your Livestream Is Safer, Cheaper, and More Flexible Than Commercial Platforms

With livestreaming more popular than ever, platforms like YouTube Live, Twitch, Vimeo, and Facebook Live have become household names. But are they really the best option for everyone? If you're a tech-savvy creator, a privacy-conscious broadcaster, or someone who just wants full control, self-hosting your own livestream server might be a better choice. In this […]

READ MORE
27 November 2020
Motivation behind this website

For every service that is offered for a lot of money on the internet, there's always a free or much cheaper alternative, especially if you embrace open-source software. For live-streaming this is also very much the case. We love that,

READ MORE
23 January 2021
Introduction to the new how-to article in the Knowledge Base.

I wrote a new guide on setting up your own live-stream server. Mostly due to your requests. I took the opportunity to simplify and shorten the guide a bit. If you follow this guide step by step, and use your brain a tiny bit, you'll be fine. This guide has so far been tested on […]

READ MORE
25 January 2021
Optimizing First-Frame Bitrate for HLS

When responding to an HLS request, the streaming server determines which video quality (i.e., ts file) the client will attempt to play before switching to a lower or higher quality video. This switch depends on available bandwidth and device type. Switching to the wrong quality first degrades the user experience considerably. For instance, sending a […]

READ MORE
25 January 2021
Adaptive Bit-rate Streaming

“What is adaptive streaming?” This question often leads to answers filled with technical terms like “HLS”, “MPEG-DASH”, “segments” and “codec“. This article is written for people who need to understand the principles, but don’t need to actually implement the technology. If you are looking for specific technical information on adaptive streaming then I recommend you read up on it on this website and elsewhere.

READ MORE
2 February 2021
About Bradmax Video Player WordPress plugin

Let's talk about our favorite video player plugin for WordPress. Here are some reasons why Bradmax Player is our favorite. And below that are detailed instructions on how to set it up. Setting it up may at first seem a bit awkward, but it really isn't. First you must obviously install the Bradmax Player plugin […]

READ MORE
Copyright © 2025. 
All rights reserved.
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram