×
Community Blog How to Install Nginx with RTMP Module on CentOS 7

How to Install Nginx with RTMP Module on CentOS 7

In this article, we will be installing Nginx with RTMP for media streaming on an Alibaba Cloud ECS CentOS 7.

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:

  1. H264/AAC support
  2. Online transcoding with FFmpeg
  3. Running external programs on certain events (exec)
  4. HTTP control module for recording audio/video and dropping clients
  5. Advanced buffering techniques to keep memory allocations at a minimum level for faster streaming
  6. Stream relay support for distributed streaming: push & pull models

Prerequisites

  1. You must have Alibaba Cloud Elastic Compute Service (ECS) activated and verified your valid payment method. If you are a new user, you can get a free account in your Alibaba Cloud account. If you don't know about how to set up your ECS instance, you can refer to this tutorial or quick-start guide. Your ECS instance must have at least 1GB RAM and 1 Core processor.
  2. A domain name registered from Alibaba Cloud. If you have already registered a domain from Alibaba Cloud or any other host, you can update its domain nameserver records.
  3. Root user

Update the System

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

Install Dependencies

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.

Download Nginx with Additional Package

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

Compile and Install Nginx

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

Configure Nginx as a Service

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

Configure Nginx RTMP Module

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

Setup First RTMP Live Stream

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

Testing

You can test this installation and configuration by using RTMP live stream from the VLC media player.

Video On Demand Stream

  1. Open the VLC media player app on your computer.
  2. Click on the 'File' menu from the media library and select the Open Network option.
  3. Now enter the RTMP URL for our vod stream like this:
    rtmp://YourServerIP:1935/vod/file.mp4
    Replace YourServerIP with the actual IP address of your server.
  4. Finally, click on the Open button. You will get the Video Stream on your screen.

Conclusion

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.

2 0 0
Share on

Alibaba Clouder

1,904 posts | 408 followers

You may also like

Comments

5267066664839337 August 24, 2019 at 4:44 pm

Dear Sir,I got this error after adding this code

5267066664839337 August 24, 2019 at 4:48 pm

Dear Sirworker_processes auto;events { worker_connections 1024;}

Alibaba Clouder

1,904 posts | 408 followers

Related Products

  • Media Solution

    An array of powerful multimedia services providing massive cloud storage and efficient content delivery for a smooth and rich user experience.

    Learn More
  • Realtime Compute

    Realtime Compute offers a highly integrated platform for real-time data processing, which optimizes the computing of Apache Flink.

    Learn More
  • Web App Service

    Web App Service allows you to deploy, scale, adjust, and monitor applications in an easy, efficient, secure, and flexible manner.

    Learn More
  • Intelligent Robot

    A dialogue platform that enables smart dialog (based on natural language processing) through a range of dialogue-enabling clients

    Learn More