Setup Nginx-RTMP on CentOS 7

RTMP is great for serving live content. When RTMP is paired with FFmpeg, streams can be converted into various qualities. Vultr is great for these applications as they provide fast and dedicated CPU performance. Vultr’s global network also ensures that you can deliver high-quality live content with minimal delay. Let's get started!

Installing Nginx and Nginx-RTMP

Install the tools required to compile Nginx and Nginx-RTMP from source.

sudo yum install pcre pcre-devel openssl openssl-devel zlib zlib-devel -y

Make a working directory and switch to it.

mkdir ~/working
cd ~/working

Download the Nginx and Nginx-RTMP sources.

wget http://nginx.org/download/nginx-1.9.7.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

Install the unzip package.

sudo yum install unzip

Extract the Nginx and Nginx-RTMP source.

tar -xvf nginx-1.9.7.tar.gz
unzip v1.2.1.zip

Switch to the Nginx directory.

cd nginx-1.9.7

Add modules to compile into Nginx. Nginx-RTMP is included.

./configure --add-module=../nginx-rtmp-module-1.2.1/

Compile and install Nginx with Nginx-RTMP.

make
sudo make install

Create a SystemD unit file for Nginx.

sudo nano /usr/lib/systemd/system/nginx.service

Populate the file with the following content.

[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

Start and enable the Nginx service.

sudo systemctl start nginx.service

Tell SystemD to start Nginx services automatically at boot.

systemctl enable nginx.service

Installing FFmpeg

Install epel-release and update the system.

sudo yum install epel-release -y
sudo yum update -y
sudo shutdown -r now

Install the Nux repository.

sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm

Install FFmpeg and its development packages.

sudo yum install ffmpeg ffmpeg-devel -y

Configuring Nginx-RTMP and FFmpeg

Open the Nginx configuration file.

sudo nano /usr/local/nginx/conf/nginx.conf

Append the following to the configuration.

    rtmp {
        server {
            listen 1935;
            chunk_size 4096;

            application live {
                    live on;
                    record off;
                    exec ffmpeg -i rtmp://localhost/live/$name -threads 1 -c:v libx264 -profile:v baseline -b:v 350K -s 640x360 -f flv -c:a aac -ac 1 -strict -2 -b:a 56k rtmp://localhost/live360p/$name;
            }
            application live360p {
                    live on;
                    record off;
        }
    }
}

After you've added that configuration text, you can customize settings such a video bitrate, audio bitrate, and resolution. These changes will only be applied to the lower quality stream. To add more qualities, copy and paste the exec FFmpeg line and change the settings. You'll also need to create a new application. You can do this by copying and pasting the live360 example that has been included. Don't forget to update the exec FFmpeg line with the address of the new application. You can do this by changing the final RTMP address in the exec FFmpeg line.

Note: Changing the value after -b:v will change the video bitrate. This is measured in kilobits per second. Changing the value after -b:a will change the audio bitrate. This is measured in kilobits per second. Changing the value after -s will change the resolution.

Save the file by pressing CTRL + X.

Restart Nginx.

sudo service nginx restart

Note: For best performance, each stream being converted should have its own CPU core. For example, if two qualities, 360P and 480P, are being created from a 720P stream, a Vultr instance with at least two CPU cores should be used.

Security Note

If you're using a firewall, you'll need to make sure TCP 1935 is allowed.

The current configuration allows anyone to stream to your server. We can fix this by only giving certain IP addresses the publish permission. Open the Nginx configuration.

sudo nano /usr/local/nginx/conf/nginx.conf

Look for the following lines.

live on;
record off;

Add the following to each set of the above lines. Change 0.0.0.0 to your actual IP address.

allow publish 127.0.0.1;
allow publish 0.0.0.0;
deny publish all;

The configuration will now look something like the following.

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application live {
                live on;
                record off;
                allow publish 127.0.0.1;
                allow publish 0.0.0.0;
                deny publish all;
                exec ffmpeg -i rtmp://localhost/live/$name -threads 1 -c:v libx264 -profile:v baseline -b:v 350K -s 640x360 -f flv -c:a aac -ac 1 -strict -2 -b:a 56k rtmp://localhost/live360p/$name;
        }
        application live360p {
                live on;
                record off;
                allow publish 127.0.0.1;
                allow publish 0.0.0.0;
                deny publish all;
        }
    }
}

Save the file by pressing CTRL + X.

Restart Nginx.

sudo service nginx restart

Configuring Software to Work with Nginx-RTMP

Streaming applications typically have two fields for connection information. The first field is usually for the server information and the second field is usually for the stream name or key. The information that you should place into each field is listed. The stream name or key can be set to anything.

Field 1: rtmp://your.vultr.ip/live/
Field 2: stream-key-you-set

To view the streams, open the following links in a player supporting RTMP.

rtmp://your.vultr.ip/live/stream-key-you-set
rtmp://your.vultr.ip/live360p/stream-key-you-set


Leave a Comment

ZPanel y Sentora en CentOS 6 x64

ZPanel y Sentora en CentOS 6 x64

ZPanel, un panel de control de alojamiento web popular, se bifurcó en 2014 a un nuevo proyecto llamado Sentora. Aprende a instalar Sentora en tu servidor con este tutorial.

Cómo instalar Vtiger CRM Open Source Edition en CentOS 7

Cómo instalar Vtiger CRM Open Source Edition en CentOS 7

Aprende cómo instalar Vtiger CRM, una aplicación de gestión de relaciones con el cliente, en CentOS 7 para aumentar tus ventas y mejorar el servicio al cliente.

Cómo instalar el servidor Counter-Strike 1.6 en Linux

Cómo instalar el servidor Counter-Strike 1.6 en Linux

Esta guía completa le mostrará cómo configurar un servidor Counter-Strike 1.6 en Linux, optimizando el rendimiento y la seguridad para el mejor juego. Aprende los pasos más recientes aquí.

¿Puede la IA luchar con un número cada vez mayor de ataques de ransomware?

¿Puede la IA luchar con un número cada vez mayor de ataques de ransomware?

Los ataques de ransomware van en aumento, pero ¿puede la IA ayudar a lidiar con el último virus informático? ¿Es la IA la respuesta? Lea aquí, sepa que la IA es una bendición o una perdición

ReactOS: ¿Es este el futuro de Windows?

ReactOS: ¿Es este el futuro de Windows?

ReactOS, un sistema operativo de código abierto y gratuito, está aquí con la última versión. ¿Puede satisfacer las necesidades de los usuarios de Windows de hoy en día y acabar con Microsoft? Averigüemos más sobre este estilo antiguo, pero una experiencia de sistema operativo más nueva.

Manténgase conectado a través de la aplicación de escritorio WhatsApp 24 * 7

Manténgase conectado a través de la aplicación de escritorio WhatsApp 24 * 7

Whatsapp finalmente lanzó la aplicación de escritorio para usuarios de Mac y Windows. Ahora puede acceder a Whatsapp desde Windows o Mac fácilmente. Disponible para Windows 8+ y Mac OS 10.9+

¿Cómo puede la IA llevar la automatización de procesos al siguiente nivel?

¿Cómo puede la IA llevar la automatización de procesos al siguiente nivel?

Lea esto para saber cómo la Inteligencia Artificial se está volviendo popular entre las empresas de pequeña escala y cómo está aumentando las probabilidades de hacerlas crecer y dar ventaja a sus competidores.

La actualización complementaria de macOS Catalina 10.15.4 está causando más problemas que resolver

La actualización complementaria de macOS Catalina 10.15.4 está causando más problemas que resolver

Recientemente, Apple lanzó macOS Catalina 10.15.4, una actualización complementaria para solucionar problemas, pero parece que la actualización está causando más problemas que conducen al bloqueo de las máquinas Mac. Lee este artículo para obtener más información

13 Herramientas comerciales de extracción de datos de Big Data

13 Herramientas comerciales de extracción de datos de Big Data

13 Herramientas comerciales de extracción de datos de Big Data

¿Qué es un sistema de archivos de diario y cómo funciona?

¿Qué es un sistema de archivos de diario y cómo funciona?

Nuestra computadora almacena todos los datos de una manera organizada conocida como sistema de archivos de diario. Es un método eficiente que permite a la computadora buscar y mostrar archivos tan pronto como presiona buscar.