Nur Rony
Polyglot Programmer, DevOps and Forever Learner

Setup a private RTMP server with nginx-rtmp-module

Setup a private RTMP server with nginx-rtmp-module

Real Time Messaging Protocol (RTMP) is one of the most popular way for streaming video, audio or data between a Flash player and a server. You can easily have a private RTMP server using Nginx and a module of it called nginx-rtmp-module. This post will help you to setup a RTMP server following some simple steps. So no more chit-chat lets jump into it.

Prerequisites

You must have root privilege to complete this steps. I am on Ubuntu and using apt-get as my package manager but for other distribution of Linux I strongly hope that all command will be valid just replacing with the package manager and appropriate package name.

Installing dependencies

Run the following commands to compile Nginx with nginx-rtmp-module

sudo apt-get update;  
sudo apt-get install -y build-essential git libpcre3 \  
libpcre3-dev openssl libssl-dev zlibc zlib1g zlib1g-dev

Compiling Nginx with RTMP Module

Now we compile Nginx with RTMP module. I use following configure command to compile Nginx. You can add or remove options according to your need

cd $HOME && cd nginx-*  
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-file-aio \
--with-pcre \
--with-file-aio \
--with-http_spdy_module \
--with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -DTCP_FASTOPEN=23' \
--with-ld-opt='-Wl,-z,relro -Wl,--as-needed -L /usr/lib' \
--with-ipv6 \
--with-debug \
--without-http_scgi_module \
--without-http_uwsgi_module \
--add-module=$HOME/nginx-rtmp-module
make  
sudo make install  

Now you have Nginx installed in your dev machine or server which running Ubuntu compiled with nginx-rtmp-module.

Configure RTMP Application

Configuring simple live RTMP application is so simple. To configure a RTMP live streaming application you need to edit nginx configuration file

sudo nano /etc/nginx/nginx.conf 

and under HTTP block paste the following piece of RTMP block to make a RTMP live streaming application called live.

rtmp {  
    server {
        listen 1935;
        application live {
            live on;
        }
    }
}

Restart nginx server by running the following command

sudo /usr/sbin/nginx -s stop && sudo /usr/sbin/nginx  

That all!! Now you have a private live streaming RTMP server running with Nginx. You can push your live video or audio stream to rtmp://your-ip-or-domainName:1935/live/{your-stream-name} using FFMpeg or GStreamer.

There are lots of directive to fine tune the RTMP application. You can find it from the Directive page of nginx-rtmp-module.

If you face any problem please leave a comment... I will respond for sure.