How to Install Nginx with RTMP Module on CentOS 7

RTMP (Real Time Messaging Protocol) is a high-performance protocol for transmission of data, audio, and video over the internet between flash-based platform technologies. Originally developed by Macromedia, and now owned by Adobe.

Nginx RTMP is a nginx-based media streamer with RTMP, HLS, and MPEG-DASH live streaming. Nginx RTMP module has lot of features, including H264/AAC support, HTTP callback support, HTTP control module for recording the audio/video etc.

In this tutorial, I will show you how to install and configure the Nginx web server with the RTMP module. We will be using the latest CentOS 7 server, and this guide will include the configuration of nginx and rtmp module and creating RTMP live stream and video on demand.

Prerequisites

  • CentOS 7 Server
  • Root privileges

What we will do?

  1. Install Dependencies
  2. Download Nginx with Additional Package and RTMP Module
  3. Compile Nginx and Install from Source
  4. Configure Nginx as a Service
  5. Configure Nginx RTMP Module
  6. Setup First RTMP Live Stream
  7. Testing

Step 1 - Install Dependencies

In this tutorial, we will build the Nginx web server from source. We need to install all packages dependencies needed, including development tools, EPEL repository, and other packages.

Install CentOS 'Development Tools'.

sudo yum -y groupinstall 'Development Tools'

Add the EPEL repository.

sudo yum -y install epel-release

Install Nginx dependencies.

sudo yum install -y  wget git unzip perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel pcre-devel GeoIP GeoIP-devel

Wait for all packages installed.

Install Nginx and RTMP dependencies

Step 2 - Download Nginx with Additional Package and RTMP Module

In this step, we will download nginx source code with the additional dependencies including pcre, zlib, and the OpenSSL.

Go to the '/usr/local/src' directory.

cd /usr/local/src

Download Nginx 1.14.0 and extract it.

wget https://nginx.org/download/nginx-1.14.0.tar.gz
tar -xzvf nginx-1.14.0.tar.gz

Download the pcre package and extract it.

wget https://ftp.pcre.org/pub/pcre/pcre-8.42.zip
unzip pcre-8.42.zip

Download the zlib package and extract it.

wget https://www.zlib.net/zlib-1.2.11.tar.gz
tar -xzvf zlib-1.2.11.tar.gz

Download the OpenSSL package and extract it.

wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz
tar -xzvf openssl-1.1.0h.tar.gz

Next, clone the Nginx RTMP Module source code using git command.

git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git

And remove all compressed tar.gz and .zip files.

rm -f *.tar.gz *.zip

Following are the directory details we have.

ls -lah

Nginx sources downloaded

And we're ready to compile and install.

Step 3 - Compile Nginx and Install from Source

Go to the 'nginx-1.14.0' directory.

cd nginx-1.14.0/

Configure the nginx 1.14.0 using those parameters below.

./configure --prefix=/etc/nginx \
            --sbin-path=/usr/sbin/nginx \
            --modules-path=/usr/lib64/nginx/modules \
            --conf-path=/etc/nginx/nginx.conf \
            --error-log-path=/var/log/nginx/error.log \
            --pid-path=/var/run/nginx.pid \
            --lock-path=/var/run/nginx.lock \
            --user=nginx \
            --group=nginx \
            --build=CentOS \
            --builddir=nginx-1.14.0 \
            --with-select_module \
            --with-poll_module \
            --with-threads \
            --with-file-aio \
            --with-http_ssl_module \
            --with-http_v2_module \
            --with-http_realip_module \
            --with-http_addition_module \
            --with-http_xslt_module=dynamic \
            --with-http_image_filter_module=dynamic \
            --with-http_geoip_module=dynamic \
            --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_auth_request_module \
            --with-http_random_index_module \
            --with-http_secure_link_module \
            --with-http_degradation_module \
            --with-http_slice_module \
            --with-http_stub_status_module \
            --http-log-path=/var/log/nginx/access.log \
            --http-client-body-temp-path=/var/cache/nginx/client_temp \
            --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
            --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
            --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
            --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
            --with-mail=dynamic \
            --with-mail_ssl_module \
            --with-stream=dynamic \
            --with-stream_ssl_module \
            --with-stream_realip_module \
            --with-stream_geoip_module=dynamic \
            --with-stream_ssl_preread_module \
            --with-compat \
            --with-pcre=../pcre-8.42 \
            --with-pcre-jit \
            --with-zlib=../zlib-1.2.11 \
            --with-openssl=../openssl-1.1.0h \
            --with-openssl-opt=no-nextprotoneg \
            --add-module=../nginx-rtmp-module \
            --with-debug

Compile nginx

Compile and install Nginx with RTMP module by running following commands.

sudo make
sudo make install

When the installation is complete, we need to create a new symlink module directory, create a new nginx user and group, and create a new nginx cache directory.

Create nginx symlink module to the '/etc/nginx' configuration directory.

sudo ln -s /usr/lib64/nginx/modules /etc/nginx/modules

Create a new 'nginx' system user and group.

sudo useradd -r -d /var/cache/nginx/ -s /sbin/nologin -U nginx

Now create a new Nginx cache directory '/var/cache/nginx' and change the owner of the directory to 'nginx' user and group.

mkdir -p /var/cache/nginx/
chown -R nginx:nginx /var/cache/nginx/

Test nginx configuration and the installed nginx version.

nginx -t
nginx -V

And the following is the result.

Nginx compiled successfully

The Nginx web server has been installed on CentOS 7 with the RTMP Module enabled.

Step 4 - Configure Nginx as a Service

In this tutorial, we will be running nginx as a service and we need to create a new nginx service file to the systemd service directory.

Go to the '/lib/systemd/system' directory and create a new 'nginx.service' file using vim.

cd /lib/systemd/system/
vim nginx.service

paste the configuration below.

[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

Save and exit.

Now reload the systemd system.

systemctl daemon-reload

Start the nginx service and enable it to launch everytime at system boot.

systemctl start nginx
systemctl enable nginx

The Nginx web server is up and running as a service on CentOS 7 system.

Nginx systemd service

Step 5 - Configure Nginx RTMP Module

In this step, we will create a new custom Nginx configuration for RTMP module.

Go to the '/etc/nginx' configuration directory and backup the original 'nginx.conf' file.

cd /etc/nginx/
mv nginx.conf nginx.conf.asli

Now create a custom configuration 'nginx.conf'.

vim nginx.conf

Paste Nginx RTMP configuration below.

worker_processes  auto;
events {
    worker_connections  1024;
}

# RTMP configuration
rtmp {
    server {
        listen 1935; # Listen on standard RTMP port
        chunk_size 4000;

# Define the Application
        application show {
            live on;
            # Turn on HLS
            hls on;
            hls_path /mnt/hls/;
            hls_fragment 3;
            hls_playlist_length 60;
            # disable consuming the stream from nginx as rtmp
            deny play all;
        }

    }
}

http {
    sendfile off;
    tcp_nopush on;
    aio on;
    directio 512;
    default_type application/octet-stream;

    server {
        listen 8080;

        location / {
            # Disable cache
            add_header 'Cache-Control' 'no-cache';

            # CORS setup
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length';

            # allow CORS preflight requests
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
            }

            types {
                application/dash+xml mpd;
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }

            root /mnt/;
        }
    }
}

Save and exit.

Next, we need to create a new directory for the HLS configuration and we've defined the web root directory is on the '/mnt' directory.

Create the 'hls' directory under the '/mnt' directory and change the owner of the directory to the nginx user and group.

mkdir -p /mnt/hls
chown -R nginx:nginx /mnt/hls

Test the configuration and restart the nginx service.

nginx -t
systemctl restart nginx

Configure Nginx for RTMP

Step 6 - Setup First RTMP Live Stream

In this tutorial, we will create new RTMP stream video on demand using the mp4 videos on the server, and create a new live RTMP stream that will be broadcasted from the local computer using the OBS software.

Go to the '/etc/nginx' configuration directory and edit the 'nginx.conf' file.

cd /etc/nginx/
vim nginx.conf

Paste configurations below in to the 'rtmp { ... }' bracket.

        # RTMP video on demand for mp4 files
        application vod {
            play /mnt/mp4s;
        }

        # RTMP stream using OBS
        application stream {
            live on;
        }

Save and exit.

Now create a new directory 'mp4s' for storing all vod videos, and change the owner to the nginx user group.

mkdir -p /mnt/mp4s
chown -R nginx:nginx /mnt/mp4s

Test nginx configuration and make sure there is no error, then restart the nginx service.

nginx -t
systemctl restart nginx

Nginx configuration for the RTMP live stream and the vod stream has been completed.

Setup RTMP live stream

Step 7 - Testing

Test RTMP live stream and vod stream using the VLC player.

Video On Demand Stream

Open the VLC app on your computer.

Click the 'File' menu choose the 'Open Network' option.

Now type the RTMP URL for our vod stream.

rtmp://192.168.1.10:1935/vod/file.mp4

Click the 'Open' button.

RTMP client setup

And the following are the results of the video stream.

RTMP Video Stream

Live Stream using OBS (Open Broadcaster Software)

In this tutorial, we will test the RTMP live stream using the obs software, and the vlc player.

We will stream only the terminal window using the open broadcaster software, and try to watch the RTMP live stream using the VLC player.

Following is my configuration for the obs software.

Live Stream using OBS (Open Broadcaster Software)

Open the VLC app on your computer.

Click the 'File' menu, choose the 'Open Network' option.

Now type the RTMP URL for our live stream.

rtmp://192.168.1.10:1935/stream/

Click the 'Open' button.

Live stream configuration

And the following is the result of the live stream using open broadcaster through the nginx rtmp module.

Live stream sows up in Media client

Installation of the Nginx web server with RTMP module on CentOS 7 has been completed successfully.

Reference

Share this page:

Suggested articles

17 Comment(s)

Add comment

Comments

By: Doug Maupin

This tutorial worked out great, but I would like know how would one add streamer authentication, while this works it also allows anyone to stream on my setup i d like to be able to restrict access

By: MD Monowar

How to setup mobile live please help me

By: JulioS

Great post! How play the videos stored on /mnt/mp4s or /mnt/hls embeded in html?

By: neil

I have done all right as what this article says, but still can't play video through vlc player. 

By the way. The vlc player was installed on a Windows10 computer. 

I try convert the rtmp stream to rtsp by using ffmpeg and it could successfuly parse the rtmp stream. 

 

I don't know why my vlc player on windows os can't work. Could somebody help me.

By: Acacio Farias Brasil

 

Hello, I wanted to know which VPS server to hire, I need to know what configuration I need to use with 2000 thousand people watching my video simultaneously, thanks for the tutorial.

By: richard

Works like a charm, except how can I view the stream from an embedded player from the net. Works in VLC just fine, but I want the live stream to be viewed by others on my website.

By: Hubert K

I seem to have a problem streaming Live from OSB.  Stream starts fine OSB connect to the server, stream starts.

When I try using VLC to stream I just see the bar going back and forth, nothing happens, no errors on the VLC side.

Any ides would be helpful.

Cheers,

 

Hubert

By: Litana

plz help, im stuck in here systemctl enable nginx

Failed to execute operation: Bad message

what to do please ?

Thanks

By: Rob

hello, I am looking to have someone build for me an HLS Streaming Server with NGINX so that I can do live broadcasing on Roku IPTV. 

I want to use my existing website to link the m3u8 link and run it through hoping that this may allow unlimited viewers since i won't be saving any data. can anybody help? email me at [email protected]

By: Barani

Hi,

I would like to use this to try out and create tutorial with in contacts. Can I do so?

By: petit

This is a great article. According to the tutorial, I successfully configured the nginx server. I would like to translate your article to other forums. I wonder if it is OK? Thanks again?

By: Howtoforge

I'm sorry, translation and republishing of HowtoForge articles is not permitted.

By: David123

Hello ,

I have donw excectlly as it say

didn't get any error 

but it doesn't work , 

when I enter the

serverIP:8080 , I get nothing 

ServerIP:1935 nothing 

 the service is up 

 sudo systemctl status nginx

? nginx.service - The nginx HTTP and reverse proxy server

   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)

   Active: active (running) since Thu 2020-07-02 13:25:17 GMT; 32min ago

 

what could it be ?

 

Thanks, 

 

By: till

Do you use a firewall? Are the ports opened in the firewall?

By: Twitchy FPV

Using CentOS 8 (Core) - All these steps work fanatstic except you now need to use nginx 1.15.0 instead of 1.14.0

wget https://nginx.org/download/nginx-1.15.0.tar.gztar -xzvf nginx-1.15.0.tar.gz

By: Eskimo

hls fragmented files deleted after a while i upload them to the server! in fact, after 1 min the converted files deletes on the server!

RTMP TEST WORKS FINE ON VLC. 

here is my ffmpeg command:

ffmpeg -i /mnt/videos/myfile.mp4 /mnt/hls/myfile.m3u8

.......here is my hls config:# Define the Applicationapplication show {live on;# Turn on HLShls on;hls_path /mnt/hls/;hls_cleanup off;hls_playlist_length 10y;hls_fragment 3;......Any Idea!TNX.

By: Jose Diaz

Very nice tutorial, but i have been burning my head trying to compile nginx and always get the same error:  

 

nginx-1.15.0/src/stream/ngx_stream_limit_conn_module.o \

nginx-1.15.0/src/stream/ngx_stream_access_module.o \

nginx-1.15.0/src/stream/ngx_stream_geo_module.o \

nginx-1.15.0/src/stream/ngx_stream_map_module.o \

nginx-1.15.0/src/stream/ngx_stream_split_clients_module.o \

nginx-1.15.0/src/stream/ngx_stream_return_module.o \

nginx-1.15.0/src/stream/ngx_stream_upstream_hash_module.o \

nginx-1.15.0/src/stream/ngx_stream_upstream_least_conn_module.o \

nginx-1.15.0/src/stream/ngx_stream_upstream_zone_module.o \

nginx-1.15.0/src/stream/ngx_stream_ssl_preread_module.o \

nginx-1.15.0/ngx_stream_module_modules.o \

-shared

sed -e "s|%%PREFIX%%|/etc/nginx|" \

        -e "s|%%PID_PATH%%|/var/run/nginx.pid|" \

        -e "s|%%CONF_PATH%%|/etc/nginx/nginx.conf|" \

        -e "s|%%ERROR_LOG_PATH%%|/var/log/nginx/error.log|" \

        < man/nginx.8 > nginx-1.15.0/nginx.8

make[1]: se sale del directorio '/usr/local/src/nginx-1.15.0'

 

with any version of Centos, i have tryied 7 Minimal and then Centos 8 Core or Minimal Installation, i tried LVM and Standard partitioning and i have done everything the most clean and clear way possible, so whats missing, whats wrong? at least i have tried to continue every step and everything seems to be working, i have made the test for ondemand video and it works, so i just have to ask?

 

does any body know if this is normal? or abnormal?

 

make[1]: se sale del directorio '/usr/local/src/nginx-1.15.0'

 

just after that the make process ends, so it would make any body think that something went wrong and cannot continue.