RTMP is a Real-Time Messaging Protocol developed by Macromedia that can be used to stream audio, video, and data over the Internet. Nginx RTMP is an Nginx-based media streamer that comes with a lot of features including H264/AAC support, online transcoding with FFmpeg, HTTP callback support, and an HTTP control module for recording the audio/video.

In this tutorial, we will show you how to compile Nginx with the RTMP module and create an RTMP live stream on CentOS 8.

Step 1 – Install Required Dependencies

Before starting, you will need to install the dependencies required to compile Nginx with RTMP support. First, install the Development Tool group package with the following command:

yum update -y
yum groupinstall 'Development Tools' -y

Next, install the EPEL repository with the following command:

yum install epel-release -y

Once installed, install other required dependencies using the following command:

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

Once all the packages are installed, you can proceed to compile Nginx.

Step 2 – Download Required Components

First, you will need to download the required components to compile Nginx with RTMP support.

First, download the latest stable version of Nginx with the following command:

wget http://nginx.org/download/nginx-1.18.0.tar.gz

Once downloaded, extract it using the following command:

tar -xvzf nginx-1.18.0.tar.gz

Next, download PCRE and Zlib with the following command:

wget https://ftp.pcre.org/pub/pcre/pcre-8.42.zip
wget https://www.zlib.net/zlib-1.2.11.tar.gz

Once downloaded, extract them with the following command:

unzip pcre-8.42.zip
tar -xvzf zlib-1.2.11.tar.gz

Once both are extracted, download OpenSSL with the following command:

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

Next, extract the downloaded package with the following command:

tar -xzvf openssl-1.1.0h.tar.gz

Next, download the latest version of the Nginx RTMP Module from the Git repository using the following command:

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

Once all the components are downloaded, you can proceed to the next step.

Step 3 – Install Nginx with RTMP Support

First, change the directory to the Nginx with the following command:

cd nginx-1.18.0/

Next, configure Nginx with RTMP support using the following command:

./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.18.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

Once the configuration process has been completed, you should get the following output:

Configuration summary
+ using threads
+ using PCRE library: ../pcre-8.42
+ using OpenSSL library: ../openssl-1.1.0h
+ using zlib library: ../zlib-1.2.11

nginx path prefix: "/etc/nginx"
nginx binary file: "/usr/sbin/nginx"
nginx modules path: "/usr/lib64/nginx/modules"
nginx configuration prefix: "/etc/nginx"
nginx configuration file: "/etc/nginx/nginx.conf"
nginx pid file: "/var/run/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/cache/nginx/client_temp"
nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"

Next, install Nginx with the following command:

make
make install

After installing Nginx, verify the installed version of Nginx with the following command:

nginx -V

You should get the following output:

nginx version: nginx/1.18.0 (CentOS)
built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)
built with OpenSSL 1.1.0h 27 Mar 2018
TLS SNI support enabled
configure arguments: --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

Step 4 – Create a Systemd Service File for Nginx

Next, you will need to create an Nginx service file to manage the Nginx service. You can create it with the following command:

nano /lib/systemd/system/nginx.service

Add the following lines:

[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 close the file when you are finished, then reload the systemd daemon with the following command:

systemctl daemon-reload

Next, start Nginx and enable it to start at system reboot with the following command:

systemctl start nginx
systemctl enable nginx

Step 5 – Configure Nginx to Use RTMP

First, create a backup copy of the Nginx main configuration file with the following command:

mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

Next, create a new Nginx configuration file using the following command:

nano /etc/nginx/nginx.conf

Add the following lines:

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 close the file, then create a new directory for the HLS configuration and give proper ownership:

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

Save and close the file when you are finished.

Step 6 – Create RTMP Live Stream

Next, you will need to create a RTMP stream video and create a new live RTMP stream. You can do this by editing Nginx main configuration file:

nano /etc/nginx/nginx.conf

Find the following lines:

hls_playlist_length 60;
# disable consuming the stream from nginx as rtmp
deny play all;
}

Add the following lines exactly below the above line:

application vod {
play /mnt/mp4s;
}

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

Save and close the file, then restart the Nginx service with the following command:

systemctl restart nginx

Next, create a directory to store MP4 video:

mkdir -p /mnt/mp4s

Next, download the sample video using the following command:

cd /mnt/mp4s
wget https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4 -O 
myfile.mp4

Next, give proper ownership to the /mnt/mp4s directory:

chown -R nginx:nginx /mnt/mp4s

Step 7 – Install FFmpeg and Start Streaming Server

First, you will need to install FFmpeg in your server. By default, it is not installed in the CentOS 8 default repository, so you will need to enable the Powertool repo in your system. You can enable it with the following command:

yum install epel-release dnf-utils
yum-config-manager --set-enabled PowerTools
yum-config-manager --add-repo=https://negativo17.org/repos/epel-multimedia.repo

Next, install the FFmpeg with the following command:

yum install ffmpeg -y

Once the FFmpeg is installed, start the streaming server with the following command:

ffmpeg -re -i /mnt/mp4s/myfile.mp4 -vcodec libx264 -vprofile baseline -g 30 -acodec aac -strict -2 -
f flv rtmp://your-server-ip:1935/show/vod

Step 8 – Test RTMP Streaming

Next, you will need to test the RTMP vod stream with VLC media player.

Once the client system, open the VLS media player, go to VLC Media => Open Network Stream. You should see the following screen:

Provide your RTMP streaming server URL and click on the Play button.

Once you are connected successfully, you should see your MP4 video in the following screen:

Conclusion

In the above tutorial, you learned how to install Nginx with RTMP support. You also learned how to configure Nginx to stream MP4 video and play it with VLC media player from the remote system. Try installing Nginx with RTMP support on VPS Hosting from Atlantic.Net today!

Learn more about our VPS hosting services and Virtual private servers.