nginx-rtmp-module: a free, open-source alternative to Wowza Media Server, FMIS, et al.
A short introduction:
nginx-rtmp-module is an open-source (BSD license) module for nginx, the One True Web Server, Saviour of the Internet, Infinitely Better than Apache and Other Lesser Web Servers that provides RTMP, Apple HLS, and MPEG-DASH streaming capabilities integrated into the webserver.
It's an incredibly powerful, light-weight (especially compared to Red5 and Wowza, the Java Beasts) and free module/server that's used in production at Vimeo and Smotri, and hopefully by the end of this tutorial you'll be able to:
- appreciate funny strikethrough text
- configure nginx-rtmp-module at a basic level
- come away with basic skills to help you mold it to fit your needs
Apologies for inciting violence
This tutorial will assume you are running CentOS 6. At the risk of starting a holy war, I will be upfront and tell you that it is the best distribution ever. Just my opinion, but also a fact.
Anyone who can provide painless compilation instructions for the other distros, please do so in the thread - I didn't include instructions for other distributions because I don't have the time to test them, and don't want to provide anyone with unreliable information.
Double requests for anyone who can help me wrap my head around packaging on Debian, which seems completely foreign and weird to me.
You may be able to just reuse the CentOS compilation instructions or use alien to install the provided RPM on Debian/Ubuntu. I don't know, and I wouldn't recommend it unless you're adventurous or using a fresh box.
0. Prerequisite skills
- If you can handle yourself on the command-line, that's really good.
- An AP Stylebook to turn to in times of need, doubt, and potentially libelous situations.
- Patience.
1. Finding a host to fit your needs
Before even thinking about starting, please be sure you're with a host that allows streaming media. Check their TOS and AUP — if you see no mentions of audio or video streaming, you should be good. Check even if you're on a dedicated server.
To be sure, if you plan for a large amount of viewers or streaming video at a large bitrate, you should consult with your host anyway. Unless you're on a dedicated server, your network port is shared with other users on the node.
In addition, if you plan to use Apple HLS or MPEG-DASH, make sure you check with your host about CPU usage limits. If you follow this tutorial properly, an ffmpeg instance will run on each published stream: this can quickly become a CPU hog if you plan to run multiple video streams.
2. System requirements
I can't state exact requirements because I don't know what they are, so the following is based on my own experience:
- If you want to use Apple HLS or MPEG-DASH, you probably want at least 512MB RAM. You'll probably also want a decent CPU core or two. See the CPU warning above.
- If you want to just stream RTMP (Flash-only), you can get away with 128MB and one core. Probably even less.
The module's developer says it should work fine on Linux/FreeBSD/MacOS and Windows. I'm only aiming for Linux here, and if the instructions work well on any other platforms, let us know!
The race to the bottom in the low end market has made the above requirements easy to meet on the cheap.
Quality is a more nebulous concept: my strong recommendations go to BuyVM, CatalystHost, Hostigation, RamNode, Prometeus/Iperweb, Terafire, BlueVM. They all provide great price/performance service and, to the best my knowledge, allow streaming.
3. Setting the foundation
nginx-rtmp-module is, as the name suggests, a module to nginx. It's not included by default, so you need to compile nginx yourself.
If you're on CentOS, I have compiled my own nginx+nginx-rtmp-module RPMs and SRPM, available here. They are based on nginx-rtmp-module commit 51c1459e04 and nginx 1.5.8 mainline.
You will also want to install ffmpeg - install the ATRPMs repository, then install ffmpeg:
32-bit:
rpm -Uvh http://dl.atrpms.net/all/atrpms-repo-6-7.el6.i686.rpm
yum -y install ffmpeg
64-bit:
rpm -Uvh http://dl.atrpms.net/all/atrpms-repo-6-7.el6.x86_64.rpm
yum -y install ffmpeg
If you'd prefer to compile this by hand, I adapted a random tutorial and tested it. Works well: Compiling on CentOS
You will need to punch out ports 80 and 1935 in your firewall.
4. Basic configuration
(If you have installed from the RPM, you can skip this step, but you should read it anyway.)
Installing the RPM or following the linked instructions above should bring you to a point where you have the basic skeleton nginx configuration up. If not, please try again, post in the thread for help, or PM me.
In the /etc/nginx/ directory, open up your text editor of choice (nano uber alles, vi, emacs..) and insert the following:
rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /var/sock;
rtmp {
server {
listen 1935;
chunk_size 4000;
application live {
live on;
wait_video on;
wait_key on;
drop_idle_publisher 10s;
exec ffmpeg -i rtmp://localhost/live/$name -threads 2 -c:a aac -ac 1 -strict -2 -b:a 64k -c:v libx264 -profile:v baseline -g 10 -b:v 500K -s 720x480 -f flv rtmp://localhost/hls/$name_mid;
}
application hls {
live on;
hls on;
hls_continuous off;
hls_fragment_slicing aligned;
hls_fragment 15s;
hls_path /tmp/hls;
allow publish 127.0.0.1;
deny publish all;
}
}
}
Save this as /etc/nginx/rtmp.conf and close the document.
Open up /etc/nginx/nginx.conf and and add this to the bottom:
include /etc/nginx/rtmp.conf;
include /etc/nginx/vhls.conf;
Save this and close the document.
5. Serving HLS files from a vhost
(If you have installed from the RPM, this configuration is already installed in /etc/nginx/conf.d/default.conf - instead, edit the server_name directive in this file to your liking.)
Open up your text editor of choice - add this:
server {
listen 80;
server_name PUT.A.DOMAIN.HERE.EVEN.IF.ITS.A.SUBDOMAIN.tld;
root /usr/share/nginx/html/;
access_log off;
index index.html;
location ~ /\.ht {
deny all;
}
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
}
Replace the server_name directive with a domain or subdomain you want to use for this experiment, or permanently. Your call.
The key in this configuration is the "location /hls" block - if you want to add this to your own premade vhost, go ahead.
Save this as /etc/nginx/vhls.conf.
6. Fasten your seatbelts and put all seats in the upright position
Start up nginx now:
service nginx start
(continued in next post - "Body is 539 characters too long" is nonsense)