Configuring VirtualBox As A nginx RTMP Server

Intro

This is how I went about building a version of nginx that supports RTMP media streaming on an Ubuntu 16.04 VirtualBox.   It is not a simple plug-and-play exercise but anyone with basic Linux system admin skills can get this going.

Initial Setup

Get the latest version of VirtualBox.

Install VirtualBox extensions.

Download Ubuntu 16.04 (AMD64).

Install VirtualBox by mounting the ISO.

Virtualbox Machine Settings – network adapter – advanced – port forwarding.
Forward port 2322 on 127.0.0.1 to port 22 on guest.

Start VirtualBox.

Login via the VirtualBox console and install stuff…

Start by adding the source libraries since we will need to build the RTMP protocol support into nginx from the source files. Edit the /etc/apt/sources.list file and uncomment any lines starting with deb-src where the deb line immediately above it is uncommented.

sudo vim /etc/apt/sources.list

Now update stuff and install stuff.

sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install openssh-server
sudo apt-get install git
sudo apt-get build-dep nginx

Now go get the source for nginx and the nginx-rtmp protocol. We will want a separate directory for all of this stuff.

cd ~
mkdir nginx
cd nginx
apt-get source nginx

# use the wildcard in this CD since the nginx version will vary
cd nginx*/debian/modules/
git clone https://github.com/arut/nginx-rtmp-module.git

Tell the nginx builder we want some special modules included. We’ll need to edit the debian/rules file in the nginx* source directory we just got a moment ago. Openthe

# add some rules
cd ~/nginx/nginx-*
sudo vim debian/rules

# add this line before the last entry in the extras_configure_flags := section
--add-module=$(MODULESDIR)/nginx-rtmp-module \

# that section should be similar to this:
extras_configure_flags := \
                        $(common_configure_flags) \
                        --with-http_addition_module \
                        --with-http_dav_module \
                        --with-http_flv_module \
                        --with-http_geoip_module \
                        --with-http_gunzip_module \
                        --with-http_gzip_static_module \
                        --with-http_image_filter_module \
                        --with-http_mp4_module \
                        --with-http_perl_module \
                        --with-http_random_index_module \
                        --with-http_secure_link_module \
                        --with-http_v2_module \
                        --with-http_sub_module \
                        --with-http_xslt_module \
                        --with-mail \
                        --with-mail_ssl_module \
                        --with-stream \
                        --with-stream_ssl_module \
                        --with-threads \
                        --add-module=$(MODULESDIR)/headers-more-nginx-module \
                        --add-module=$(MODULESDIR)/nginx-auth-pam \
                        --add-module=$(MODULESDIR)/nginx-cache-purge \
                        --add-module=$(MODULESDIR)/nginx-dav-ext-module \
                        --add-module=$(MODULESDIR)/nginx-development-kit \
                        --add-module=$(MODULESDIR)/nginx-echo \
                        --add-module=$(MODULESDIR)/ngx-fancyindex \
                        --add-module=$(MODULESDIR)/nginx-http-push \
                        --add-module=$(MODULESDIR)/nginx-lua \
                        --add-module=$(MODULESDIR)/nginx-upload-progress \
                        --add-module=$(MODULESDIR)/nginx-upstream-fair \
                        --add-module=$(MODULESDIR)/nginx-rtmp-module \
                        --add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module

Keep in mind that if you automatically or manually update nginx on this server it will overwrite your custom-baked RTMP friendly version.   You can edit the source debian/changelog file and add a new version at the top.   Copy the first block of code and add -rtmp- to your version.  Here is what my config section looks like in debian/changelog:

nginx (1.10.0-0rtmp-ubuntu0.16.04.4) xenial-security; urgency=medium

  * Add RTMP
    - https://github.com/arut/nginx-rtmp-module.git

 -- Lance Cleveland <lance@lance.bio>  Sat, 26 Nov 2016 10:42:14 -0400

Build it:

sudo dpkg-buildpackage -b

Move your RTMP-enabled nginx build to a good place:

mkdir /usr/local/nginx
mkdir /usr/local/nginx/rtmp
cp ~/nginx/nginx*deb /usr/local/nginx

Configure nginx for RTMP

From the command line of Ubunutu:

cd /etc/nginx
sudo vim nginx.conf

Add this section above the http{…} section:

rtmp {
    server {
        listen 1935;
        chunk_size 8192;

        application vod {
            play /usr/local/nginx/rtmp;
        }
    }
}

Start nginx from the command line:

sudo service nginx start

Start RTMP-enabled nginx

Install (and start) the new packages.

cd /usr/local/nginx
sudo dpkg -i nginx-common*
sudo dpkg -i nginx-extras_*

If your server is setup to auto-start nginx this new RTMP enabled version should start by default.  You can check this by running netstat:

netstat -a | grep LISTEN

You should see port 1935 on the list of ports being listened to.

netstat-a-2016-11-27_10-22-57

These commands can test that nginx is running and start it if necessary:

service nginx status

service nginx start

 

Connecting To The VirtualBox RTMP

In the VirtualBox machine settings you will want to makes sure your network is setup as attached to NAT with the advanced port forwarding option enabled.   Forward your host system’s localhost address (127.0.0.1) port 1935 to the guest port 1935.

virtualbox-rtmp-port-forwarding-2016-11-27_10-21-39

Meta

VirtualBox

The virtual machine service that lets us run a server from our laptop; in my case an Ubunutu 16.04 box on my MacBook Pro laptop.

https://www.virtualbox.org/

Ubuntu

A Linux operating system based on the Debian “flavor”.   Linux operating systems tend to play nice in virtual machines, unlike Windows or OS/X.

https://www.ubuntu.com/

openssh-server

The ssh protocol handler that allows us to connect via SSH to the VirtualBox.   I like to use the built-in OS/X terminal (or PuTTY on Windows) to connect to my virtual machines.   Far less input switching and graphics issues to deal with using a native terminal window on the host machine.

nginx

A media and web server.  Typically used to “hand out” web pages it can send media files like images, videos, and audio streams to users.   The default support is for HTTP requests which is primarily for serving static file requests, meaning you can not pause and restart the request easily.   RTMP protocol support needs to be added.

RTMP

A real time streaming media protocol.   It was originally a proprietary Flash protocol that was partially released as open source by Adobe.   Similar to what http is for web pages, rtmp is for video and audio files.  For tech geeks it is a TCP based protocol meaning it is stateful and can maintain persistent connections.

https://en.wikipedia.org/wiki/Real-Time_Messaging_Protocol

port 1935

The port we are using for nginx audio streaming as configured in the nginx.conf file.  It is the default listening port for RTMP.

One thought on “Configuring VirtualBox As A nginx RTMP Server

  1. hello, i follow your instruction. but still stuck on command “cd /etc/nginx” because i dont have folder nginx there. any instruction that i’m missing before? please give me some advice

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.