Home How to create a streaming server with nginx
Post
Cancel

How to create a streaming server with nginx

camcoder image

Video streaming can be difficult to set up especially if one doesn’t want to use established streaming services. YouTube, Facebook, Twitter, Wowza, and many other services provide streaming services that are suitable for different use cases. There is always that one time when creating a private streaming server that you have total control over is the best option.

Nginx

Nginx is described as “The only all‑in‑one load balancer, web server, content cache, and API gateway”. Most importantly Nginx is a web server more like Apache web server. It has its own advantages and one of these advantages is the fact that it can be used to easily set up a streaming server.

RTMP

RTMP stands for Real-Time Messaging Protocol. The protocol was developed by Macromedia as a means of streaming audio, video and other content over the web. Macromedia was acquired by adobe which later opened part of the RTMP protocol for public use.

Setting up the server

Video streaming requires a solid internet connection and a capable server. As with most server-side tasks, I personally prefer Linux based operating systems and, in this case, we will be using Ubuntu.

To install Nginx, update the server with the following command:

1
sudo apt update

Once the update is complete, install dependencies that will help to make sure Nginx runs on the server

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

Now for the nginx (pronounced “Engine-X”) installation part we will have to compile it from source so that we add the RTMP module to the server software. To achieve this, first navigate to your home directory by issuing the cd command. Once there, download the code using wget with the following command.

1
wget http://nginx.org/download/nginx-1.15.1.tar.gz

Once that is done, download the RTMP module from GitHub as follows:

1
wget https://github.com/sergey-dryabzhinsky/nginx-rtmp-module/archive/dev.zip

Issue the following commands to unpack both files and cd to the Nginx folder

1
2
3
tar -zxvf nginx-1.15.1.tar.gz 
unzip dev.zip 
cd nginx-1.15.1

Time to build the nginx server with the RTMP module. This can be achieved by issuing the following commands:

1
2
3
./configure — with-http_ssl_module — add-module=../nginx-rtmp-module-dev 
make 
sudo make install

Congrats!!! Nginx is installed.

Now, by default, the install folder for Nginx is “/usr/local/nginx”.

To start the server issue the following command:

1
sudo /usr/local/nginx/sbin/nginx

The server is almost ready now, the only part that is left is changing the config file so that we add RTMP capability. Open /usr/local/nginx/conf/nginx.conf using vim or any text editor of choice and add the following lines at the end of the file.

1
2
3
4
5
6
7
8
9
10
rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        application live {
            live on;
            record off;
        }
    }
}

Now restart the server with the following command to make sure the new configuration is loaded:

1
2
sudo /usr/local/nginx/sbin/nginx -s stop 
sudo /usr/local/nginx/sbin/nginx

The server is now ready for streams. To test the server, use any streaming software such as OBS Studio or Wirecast. Choose custom server on the streaming software configuration and enter *“rtmp:///live"* as the server you want to stream to. On the stream key or stream name part just enter anything.

Success, If the streaming software manages to stream then your server is working perfectly well. To view the stream there are a number of options to do so. The stream can be embedded in a webpage or viewed from and player that can play video streams such as VLC or IINA player.

This post is licensed under CC BY 4.0 by the author.