By Sajid Qureshi, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud's incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.
RTMP (Real Time Messaging Protocol) is a high-performance protocol for streaming of data, audio, and video through the internet. In this guide, we will be installing Nginx with RTMP module which is a media streamer based on Nginx. It provides RTMP, HLS, and MPEG-DASH live streaming capabilities. Nginx RTMP module has a lot of features some of them are listed below:
It is recommended that you should install any new packages on a freshly updated server.
First of all, upgrade all the available packages and update the system using the following command.
yum -y update
We will install Nginx with RTMP module, but before that, you will need to install other dependencies required such as development tools, EPEL repository, and other packages.
Execute the following command to install development tools.
yum -y groupinstall 'Development Tools'
Now, add the EPEL repository to the CentOS 7 system.
yum -y install epel-release
Next, install Nginx dependencies and other required packages using the following command.
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
The above command will take some time, so hold on a bit.
You have installed all the dependencies required, so let's download and install Nginx source with some additional packages. First of all, change your current directory to '/usr/local/src' using the following command.
cd /usr/local/src
Download Nginx using the following command.
wget https://nginx.org/download/nginx-1.14.0.tar.gz
Next, you will need to extract the above-downloaded file using the following command.
tar -xzvf nginx-1.14.0.tar.gz
Next, you will need to download the pcre package execute the following to do so.
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.zip
Extract the downloaded file.
unzip pcre-8.42.zip
Next, you will need to download the zlib package and extract it using the following commands.
wget https://www.zlib.net/zlib-1.2.11.tar.gz
tar -xzvf zlib-1.2.11.tar.gz
Next, 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
Finally, clone the Nginx RTMP Module source code using git command.
git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
You have downloaded the Nginx with all the other required additional packages. So, let's install and configure Nginx. To do so change your current directory to 'nginx-1.14.0'.
cd nginx-1.14.0/
Configure the nginx using these 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
Install Nginx with RTMP module. Execute the following commands and they will do the job for you.
sudo make
sudo make install
Once, the installation is complete create nginx symlink module to the '/etc/nginx' configuration directory.
ln -s /usr/lib64/nginx/modules /etc/nginx/modules
Next, you will need to create a new 'nginx' system user and group. Run the following command to do so.
useradd -r -d /var/cache/nginx/ -s /sbin/nologin -U nginx
Create a new directory using the following command.
mkdir -p /var/cache/nginx/
Next, you will need to change the ownership rules for the directory.
chown -R nginx:nginx /var/cache/nginx/
Finally, you can verify and test this nginx installation using the following command.
nginx -t
In this guide, you will be using Nginx as a service so you will have to create a new nginx service file.
Change your current directory to '/lib/systemd/system'.
cd /lib/systemd/system/
Now, create a new 'nginx.service' file using the following command.
nano nginx.service
Add the following content to the file.
[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 the file and exit from the text editor.
Next, reload the systemd system using the following command.
systemctl daemon-reload
Now, start the nginx service and enable it to launch at the boot time. Run following commands to do so.
systemctl start nginx
systemctl enable nginx
Your Nginx server is active and running as a service on your CentOS 7 system. Now, you will have to create a new nginx configuration for RTMP module and to do so, change your current directory to the nginx configuration directory back up the original configuration file. Execute the following commands and they'll do the job for you.
cd /etc/nginx/
mv nginx.conf nginx.conf.asli
Create a new nginx configuration file using any text editor using the following command.
nano nginx.conf
Add the following content to the file.
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 the file and exit from the text editor.
You will need to create a new directory for the HLS configuration using the following command.
mkdir -p /mnt/hls
Next, change the ownership rules for the same.
chown -R nginx:nginx /mnt/hls
Finally, restart your nginx server to apply changes that we just configured using the following command.
systemctl restart nginx
All the required dependencies are installed and configured correctly now. Now, let's create new RTMP stream video on demand using the mp4 videos on the server. You will need to edit the nginx configuration file to do so.
cd /etc/nginx/
nano nginx.conf
Add the following content into the 'rtmp { ... }' bracket in the file.
# RTMP video on demand for mp4 files
application vod {
play /mnt/mp4s;
}
# RTMP stream using OBS
application stream {
live on;
}
Save the file and exit from the text editor.
To store all the videos data you will have to create a new directory using the following command.
mkdir -p /mnt/mp4s
Next, change the ownership rules for the directory to the nginx user group.
chown -R nginx:nginx /mnt/mp4s
Finally, restart your nginx services using the following command.
systemctl restart nginx
You can test this installation and configuration by using RTMP live stream from the VLC media player.
Video On Demand Stream
In this tutorial, you have learned how to install Nginx with the RTMP module. You also learned to high-performance streaming of video using this RTMP module. We hope now you have enough knowledge to work with this module.
Making it Big in China: A Perspective by Just After Midnight
2,599 posts | 762 followers
FollowAlibaba Clouder - August 27, 2020
Sajid Qureshi - August 8, 2018
Alibaba Clouder - November 13, 2018
Alibaba Clouder - December 7, 2017
Alibaba Clouder - January 9, 2020
OpenAnolis - June 1, 2023
Dear Sirworker_processes auto;events { worker_connections 1024;}
2,599 posts | 762 followers
FollowAn array of powerful multimedia services providing massive cloud storage and efficient content delivery for a smooth and rich user experience.
Learn MoreTranscode multimedia data into media files in various resolutions, bitrates, and formats that are suitable for playback on PCs, TVs, and mobile devices.
Learn MoreAlibaba Cloud (in partnership with Whale Cloud) helps telcos build an all-in-one telecommunication and digital lifestyle platform based on DingTalk.
Learn MoreRealtime Compute for Apache Flink offers a highly integrated platform for real-time data processing, which optimizes the computing of Apache Flink.
Learn MoreMore Posts by Alibaba Clouder
5267066664839337 August 24, 2019 at 4:44 pm
Dear Sir,I got this error after adding this code