Saturday , July 20 2019
Home / Uncategorized / How to install Nginx with the RTMP module on CentOS 7

How to install Nginx with the RTMP module on CentOS 7



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

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

In this tutorial, I will show you how to install and configure the Nginx web server with the RTMP module. We will use the latest CentOS 7 server and this guide will include the configuration of the nginx and rtmp module and the creation of live streaming RTMP 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 the Nginx RTMP module
  6. Set the first RTMP live stream
  7. analysis

Step 1: Install the dependencies

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

Install CentOS development tools.

sudo yum -y groupinstall & # 39; Development Tools & # 39;

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 until all packages are installed.

Installs Nginx and RTMP dependencies

Step 2 – Download Nginx with additional package and RTMP module

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

Go to the & # 39; / usr / local / src & # 39 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 pc package and extract it.

wget https://ftp.pcre.org/pub/pcre/pcre-8.42.zip
decompress 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 source code of the Nginx RTMP module using the git command.

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

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

rm -f * .tar.gz * .zip

Below are the details of the directory we have.

I-Allah

Sources of Nginx downloaded

And we are ready for compilation and installation.

Step 3: Compile Nginx and install from source

Go to the & # 39; nginx-1.14.0 & # 39 directory.

cd nginx-1.14.0 /

Configure the nginx 1.14.0 using the following parameters.

./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-thread
–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

Complete nginx

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

sudo make
sudo make install

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

Create a symlink nginx module in the configuration directory & # 39; / etc / nginx & # 39 ;.

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

Create a new user and system group & # 39; nginx & # 39 ;.

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

Now create a new Nginx cache directory & # 39; / var / cache / nginx & # 39; and change the owner of the directory to "nginx" user and group.

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

Check the nginx configuration and the installed nginx version.

nginx -t
nginx -V

And the following is the result.

Nginx has been compiled correctly

The Nginx Web server was installed on CentOS 7 with the RTMP enabled.

Step 4: Configure Nginx as a service

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

Go to the & # 39; / lib / systemd / system & # 39; directory and create a new file & # 39; nginx.service & # 39; 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
Would you like = 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.

demon systemctl-reload

Start the nginx service and enable it to start it every time the system starts up.

systemctl start nginx
systemctl enables nginx

The Nginx Web server is active and running as a service on the CentOS 7 system.

Nginx systemd service

Step 5: Configure the Nginx RTMP module

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

Go to the configuration directory & # 39; / etc / nginx & # 39; and backup the file & # 39; nginx.conf & # 39; original.

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

Now create a custom configuration & # 39; nginx.conf & # 39 ;.

vim nginx.conf

Paste the NMPX RTMP configuration below.

auto worker_processes;
events {
worker_connections 1024;
}

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

# Define the application
show application {
alive;
# Activate HLS
hls on;
hls_path / mnt / hls /;
hls_fragment 3;
hls_playlist_length 60;
# disables the flow consumption from nginx as rtmp
deny everyone play;
}

}
}

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

server {
listen 8080;

Position / {
# Disable the cache
add_header & # 39; Cache-Control & # 39; & # 39; no-cache & # 39 ;;

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

# allows CORS preliminary verification requests
if ($ request_method = & # 39; OPTIONS & # 39;) {
add_header & # 39; Access-Control-Allow-Origin & # 39; & # 39; & # 39 ;; *
add_header & # 39; Access-Control-Max-Age & # 39; 1728000;
add_header & # 39; Content-Type & # 39; & # 39; text / plain charset = UTF-8 & # 39 ;;
add_header & # 39; Content-Length & # 39; 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 define the web root directory in the & # 39; / mnt & # 39; directory.

Create the & # 39; hls & # 39; directory in the & # 39; / mnt & # 39; directory and change the owner of the directory in the user and in the nginx 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: Install the first RTMP live stream

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

Go to the configuration directory & # 39; / etc / nginx & # 39; and edit the file & # 39; nginx.conf & # 39 ;.

cd / etc / nginx /
vim nginx.conf

Paste the configurations below into the parenthesis & # 39; rtmp {…} & # 39 ;.

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

# Stream RTMP using OBS
application flow {
alive;
}

Save and exit.

Now create a new directory & # 39; mp4s & # 39; to archive all vod videos and change the owner to the nginx user group.

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

Check the nginx configuration and make sure there are no errors, then restart the nginx service.

nginx -t
systemctl restart nginx

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

Set up RTMP live streaming

Step 7: test

Try live RTMP streaming and vod streaming using the VLC player.

Stream Video On Demand

Open the VLC app on your computer.

Click on the & # 39; File & # 39; menu select the & # 39; Open network & # 39; option.

Now type in the RTMP URL for our vod stream.

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

Click on the "Open" button.

Configuring the RTMP client

And the following are the results of the video stream.

RTMP Video Stream

Live Stream with OBS (Open Broadcaster Software)

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

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

Below is my configuration for the obs software.

Live Stream with OBS (Open Broadcaster Software)

Open the VLC app on your computer.

Click the & # 39; File & # 39; menu, select the & # 39; Open network & # 39; option.

Now type in the RTMP URL for our live stream.

rtmp: //192.168.1.10: 1935 / stream /

Click on the "Open" button.

Live stream configuration

And the following is the result of the live stream that uses the issuer open through the nginx rtmp module.

Live streaming is available in the media client

The installation of the Nginx Web server with the RTMP module on CentOS 7 has been completed successfully.

Reference

About Muhammad Arul

Muhammad Arul is a freelance system administrator and a technical writer. He has worked with Linux Environments for more than 5 years, passionate about Open Source and strongly motivated in the installation and troubleshooting of Linux problems. Mainly working with RedHat / CentOS Linux and Ubuntu / Debian, Nginx and Apache web servers, Proxmox, Zimbra Administration and Website Optimization. I am currently learning about OpenStack and Container Technology.


Source link

Leave a Reply

Your email address will not be published.