How to set up your own private RTMP server using nginx

How to set up your own private RTMP server using nginx

Most people who stream enjoy using services such as Twitch.tv or Ustream to deliver video to viewers, and that works well enough. But sometimes you want some more control over your stream, or you want other people to be able to stream to you, or you want to stream to multiple places, or any number of things that requires you to have access to an actual RTMP stream from an RTMP server. This guide will cover the very basics of setting up a simple RTMP server on a Linux computer. Don't worry, it's not too complicated, but having familiarity with Linux will certainly help.

A couple things you can do with your own RTMP server that you might be interested in:

  • Stream to multiple external channels
  • Import other people's streams to incorporate for your own purposes (I use it in my casts for multiple camera angles, like in this video I did of a Tribes Ascend broadcast with multiple camera angles)
Alright, so how do you do these kinds of things?

Step 1: Get a Server Box

Believe it or not, RTMP is actually extremely light on system resources. Essentially it just grabs data from the input and forwards it on to the output, simple data transfer. Don't believe me? My RTMP server for a long time was a Raspberry Pi, a $35 mini-computer, sitting under my desk, and it was capable of hosting at least 3 simultaneous streams, and I never even stressed it to see how many more it could handle. So I assure you, even a cheap old box would suffice.

If you don't have your own box, a VPS can also work. I recommend Linode or Digital Ocean as providers. Just make sure you have enough bandwidth...remember that bandwidth usage will be (the size of a stream) * (the number of people uploading + the number of people downloading). So when I have 2 streamers stream to my server, and I download both of them, I can chew up 10GB of bandwidth in 2 hours.

I recommend using Ubuntu for the server software for the sake of ease, but you can obviously use whatever you want. As long as you get the dependencies for nginx somewhere besides apt, you can follow this guide just fine.

Note to Windows users: This guide focuses on using Linux. If you want to use Windows, you can find Windows binaries for nginx with the RTMP module already included here: http://nginx-win.ecsds.eu/download/

Note to Mac users: You can install nginx with the RTMP module via Homebrew: http://brew.sh/homebrew-nginx/

If you are hosting your server in your home, you will have to forward TCP port 1935 to the box...this varies by router, so look up how to set up port forwarding for your router. Also, I recommend using a dynamic DNS service to overcome dynamic IP issues that come up with residential hosting.

Step 2: Installing nginx with RTMP module

Log into your box, and make sure you have the necessary tools to build nginx using the following command:

$ sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
Now a bit of info about nginx (pronounced "engine-X"). nginx is an extremely lightweight web server, but someone wrote a RTMP module for it, so it can host RTMP streams too. However, to add the RTMP module, we have to compile nginx from source rather than use the apt package. Don't worry, it's really easy. Just follow these instructions. :)

From your home directory, download the nginx source code:

As of this writing, the latest stable version of nginx is 1.15.1. You can find the latest version on the nginx download page.

Next, get the RTMP module source code from git:

Unpack/unzip them both, and enter the nginx directory:

$ tar -zxvf nginx-1.15.1.tar.gz
$ unzip dev.zip
$ cd nginx-1.15.1
Now we build nginx:

$ ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-dev
$ make
$ sudo make install
And nginx is installed! By default it installs to /usr/local/nginx, so to start the server run the following command:

$ sudo /usr/local/nginx/sbin/nginx
And to test to make sure nginx is running, point your browser to http://<your server ip>/ and you should get the "Welcome to nginx!" page.

Step 3: Configuring nginx to use RTMP

Open your config file, located by default at /usr/local/nginx/conf/nginx.conf and add the following at the very end of the file:

Code:
rtmp {
        server {
                listen 1935;
                chunk_size 4096;

                application live {
                        live on;
                        record off;
                }
        }
}
This is an extremely basic configuration with a "live" application that simply forwards the RTMP stream on to whoever requests it. You can play with it some more later. Here's the whole configuration guide, which shows you how to forward streams to other places (such as Twitch), save recordings of uploads, output stats, etc.

Restart nginx with:

$ sudo /usr/local/nginx/sbin/nginx -s stop
$ sudo /usr/local/nginx/sbin/nginx
Step 4: Testing!

Your server should now be ready to accept RTMP streams! Let's try it out.

Create a new profile in OBS, and change your Broadcast Settings thusly:

Streaming Service: Custom
Server: rtmp://<your server ip>/live
Play Path/Stream Key: test
You may be wondering where that play path "test" came from. Well, we just made it up, just now. You can basically make up any play path and stream to it, and put that path into an RTMP player, and it will play back. For simple purposes, authentication isn't necessary in my experience.

You should now be able to start streaming to your server. If you hit "Start Streaming" and don't get an error from OBS, that's a good sign.

So how do you watch it? The easiest way to do so is with VLC (v2.1.0 or later). Just Open a Network Stream and enter in rtmp://<your server ip>/live/test as the URL. If it all worked right, then you should now be seeing your stream in VLC!

You now have a working RTMP server! Congrats!

What now?

You can add the stream to OBS itself using the Media source or VLC source, or use something like JWPlayer to play back the RTMP stream on a web site you set up.

You can also use your RTMP server to forward to other streaming services and channels! Underneath the "record off;" line in your nginx.conf, add the following:

push rtmp://<other streaming service rtmp url>/<stream key>
And any stream streamed to that application will be forwarded on to the other service, as well as being served up from the server! You can add multiple "pushes" to forward the stream to multiple locations.

FAQ

Q: Why nginx? Why not crtpmserver/Red5/Wowza?
A: I've tried crtmpserver (aka rtmpd) before, and it works, but is harder to use. If you need to use RTSP, though, instead of RTMP, then I recommend it, since the nginx RTMP module doesn't do RTSP. Red5 seemed overly complicated and heavy to me, and written in Java...you can use it if you want though. I haven't investigated it deeply. Wowza is not free. You can use what you like, but so far nginx is lightweight, easy to use, and free, so you're going to have to pry it from my cold, dead hands.

Q: How do I X?
A: This FAQ is still in progress...ask questions so I can add them here!
Author
dodgepong
First release
Last update
Rating
4.59 star(s) 39 ratings

Latest updates

  1. Added a link to Windows binaries for nginx with RTMP module included

    For you people who want to use Windows for some reason.

Latest reviews

I am incredibly thankful for this guide!! There are a few things missing for newer users though. I had never used Ubuntu before so I had to learn a lot to get this to work.

1.) When I first installed NGINX, I didn't have a user created (didn't know I needed one) which caused problems because the installs default location is inside a user folder.

2.) When he says to "edit the config file" I had absolutely no clue how to even begin doing that from a command line. But I learned that I needed to use the "vi" editor, and was able to find some other tutorials online about it.

Again, incredibly thankful for this guide, but it seems aimed at users who are already familiar with Ubuntu. But there are many novice users who could use this functionality who would benefit from a more comprehensive tutorial.
i already create my own server in a raspberry pi 3 b+ but how can i access to the rtmp server from another network?

with the internal IP address works great but i want to stream from another network with the public IP address..

thanks a lot.
Thanks for the guide!

Here's my problem: I need to stream sports events to youtube, the camera we're using outputs to hdmi, we use an elgato usb capture device to encode the video feed and outputs it via usb to a computer. Than we grab that encoded feed with OBS and stream it to youtube. Except that the camera needs to be close to the computer (due to hdmi cable length limitations) and we'd like to put the camera farther away from the computer...

Using a raspberry and a usb capture device, would it be possible to stream to the computer over wifi or ethernet?

Something like this:
camera > usb capture card > raspberry > network > computer > youtube
Great guide but for some reason I'm running into the same issue as another. When I stream my CPU usage goes just as high as when I'm streaming normally to twitch.
I couldn't get this to work because I was using a network bridge, but my friend who wasn't got his to work fine. I guess just keep that inmind if you're doing this.
Thanks for writing this, it got me fairly far. However it does assume a bit of prior knowledge. I've used Linux before, but not extensively.
I found out that I needed to install unzip. I had to Google that. For newbies, it would be good to include this line before unzipping:
sudo apt-get install zip unzip

Also for the initial line:
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
I got loads of 404 errors. That's apparently because some of the paths are old. I Googled and found that the following worked:

apt-get update && install build-essential libpcre3 libpcre3-dev libssl-dev

Not sure why!

However, when I got to the end and added to the config file and tried to stream to it from OBS I just got server not found.

I used something similar to this:
rtmp {
server{
listen 1935;
chunk_size 4096;

application live {
live on;
record off;
push rtmp://live.twitch.tv/app/[streamkeyfromtwitch];
push rtmp://a.rtmp.youtube.com/live2/[streamkeyfromyoutube];
}
}
}

and streamed to rtmp://(My-IP)/live

Any thoughts on how I can debug this. I really don't like it when things don't work!
Good stuff to deal with ISP oversea speed throttle and lower down resource use.

There is a way to add stream key for OBS (to rtmp server)?
I'm using a VPS and this VPS also running my small website.
I'm afraid of someone will know my VPS public IP (since it can be discover by reverse website IP).
Since OBS didn't support proxy/socks so I can't tunnel to VPS to make a local proxy/socks and using block all IP from using RTMP protocol except localhost.
can i use this method to lower my cpu usage ? when i stream on my pc my cpu blows up and stream stops randomly from obs , so i was thinking maybe i can let my rasperi board handle streaming part
You have done something wrong when it comes to installing the RTMP modules, if anyone is getting an error when configuring the RTMP config file do this >> ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-dev << As you can see, I changed the 'module' at the end to 'dev' as you are no longer downloading module.zip but dev.zip. Hope this helps some people!
dodgepong
dodgepong
Looks like that was one more spot I missed when I did the update a couple weeks ago, thanks for the catch. Sorry about that.
I think this guide is outdated.
It mentions version 1.15.1 at the top, then asks us do download a dev.zip file, and then extracting a master.zip file, that we haven't been intructed to download. And after that it wants us to go to a folder named nginx-1.13.1, which doesn't exist, because we downloaded the 1.15.1.

If I extract the 1.15.1 file and the dev.zip, the installation in the next steps fails.
same if I download the 1.13.1 file and master.zip, although I get a different error.

I'm using ubuntu in windows.
dodgepong
dodgepong
Hi Popupkiller,

I missed a couple spots when I recently updated the guide for a newer version, thanks. The correct files are nginx-1.15.1.tar.gz for nginx and dev.zip for the rtmp module.
Top