Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manh-dan/26cd90d836d243757baac0f252229ad6 to your computer and use it in GitHub Desktop.
Save manh-dan/26cd90d836d243757baac0f252229ad6 to your computer and use it in GitHub Desktop.
Compile NGINX with RTMP and setup Multi-Streaming

Compile NGINX with RTMP and setup Multi-Streaming

These Scripts will install NGINX with the RTMP Module in the usual directories similar to installation with apt-get.

The RTMP-Server you get with this can then be used to do one ore more of the following:

  • deliver streams in a local network
  • deliver streams to websites similar to youtube
  • transcode rtmp streams to hls video
  • publish to multiple streaming providers
  • record livestreams to a harddrive
  • many, many more: Check the Docs on Nginx RTMP-Module

And it also runs flawless on the Raspberry Pi 4!

Installation

If git is not present, install it: Download Git

Get the Scripts:

git clone https://gist.github.com/489c5c0cf5a561a32f757d7513465344.git install_nginx

Change into the directory:

cd install_nginx

Then Run the main Script:

sudo install_nginx.sh

Pulish to your new RTMP-Server:

Publish from OBS or another Streaming-Device to the following URL (where hostname is the Name or IP of your server).

rtmp://hostname:1935/live

If you have an api-key for example to publish to youtube, either set it in your OBS or alter the URL like so:

rtmp://hostname:1935/live/your-api-key

Explanation and Options

This script installs a rtmp-config which accepts Livestreams and accepts client-connections to play them.

By default the following options are disabled in the configuration file nginx.conf but you can enable and alter them according to your scenario:

Publishing to Streaming-Service (i.e. Youtube):

If you enable the push option, every published stream will also be pushed to the provided server, by default youtube.

local VLC Playback (i.e. on Raspberry Pi):

The second option is very handy if you want to Livestream into the internet but have to also Stream into another Room/Location at your site where an audience is watching.

The command after exec_publish will be executed when starting a livestream and will be killed on stop.

remote VLC Playback

The Command can be altered to start the VLC-Window on another device if you use it with ssh.

To get this running you will have to do some things first, replace username and remote-client with the values fitting your setup:

sudo -u www-data ssh-keygen -t rsa -b 4096
sudo -u www-data ssh-copy-id username@remote-client

After that you can enable an option similar to this:

ssh -o "StrictHostKeyChecking no" user@remote-client "export DISPLAY=:0.0 && vlc -f --video-on-top --no-video-title-show --mouse-hide-timeout 1 rtmp://localhost/live/$name";

To see what the Scripts do exactly or what the config means, look into the Files below.

# update repository index and update
sudo apt update && sudo apt upgrade -y
# install build tools and NGINX requirements
sudo apt install -y git build-essential ffmpeg libpcre3 libpcre3-dev libssl-dev zlib1g-dev
# retrieve the latest RTMP-Module
git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
# retrieve NGINX (if you want the latest release update this path according to the NGINX Website: http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.18.0.tar.gz
# extract the .tar.gz archive
tar -xf nginx-1.18.0.tar.gz
# change into the nginx directorx update this too if you are using another version!
cd nginx-1.18.0/
# configure nginx to desired settings before making, more infos about these options: https://www.nginx.com/resources/wiki/start/topics/tutorials/installoptions/
./configure --prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=www-data \
--group=www-data \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_secure_link_module \
--add-module=../nginx-rtmp-module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
# make with above options ( -j 1 limits it to one job)
make -j 1
# install nginx
sudo make install
# leave the nginx directory
cd ..
# remove the old nginx conf and replace it with the one from this gist.
sudo rm /etc/nginx/nginx.conf
sudo cp nginx.conf /etc/nginx/
# remove no longer needed folders and tarballs
rm -r nginx-1.18.0 nginx-rtmp-module
rm *.tar.gz
# place the service description into correct directory
sudo cp nginx.service /etc/systemd/system/
# initially start the nginx service
sudo systemctl start nginx.service
# enable the service to start at desired runtimes
sudo systemctl enable nginx.service
# automatically generate workers not limited to a fixed amount: http://nginx.org/en/docs/ngx_core_module.html#worker_processes
worker_processes auto;
events {
# maximum connections per worker process: http://nginx.org/en/docs/ngx_core_module.html#worker_connections
worker_connections 1024;
}
# RTMP configuration: https://github.com/arut/nginx-rtmp-module/wiki/Directives
rtmp {
# a server can contain several applications
server {
listen 1935; # Listen on standard RTMP port
chunk_size 4000; # the bigger the lower cpu-overhead: https://github.com/arut/nginx-rtmp-module/wiki/Directives#chunk_size
# application "live" results in a url like rtmp://localhost:1935/live
application live {
live on; # enable live-streaming
record off; # disable recording to local drive
# remove the first # to enable the following options
# Push incoming streams to youtube
# push rtmp://a.rtmp.youtube.com/live2;
# start a local vlc displaying the incoming stream as soon as one is published to this server:
# exec_publish "export DISPLAY=:0.0 && vlc -f --video-on-top --no-video-title-show --mouse-hide-timeout 1 rtmp://localhost/live/$name";
}
}
}
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment