This guide for Centos/ RHEL/ Rocky Linux,… So, you can use for other Linux.

Install some required packages

sudo dnf -y install epel-release
sudo dnf -y groupinstall 'Development Tools'
sudo dnf 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
cd ~

Install NGINX from source

Download some required packages:

ZLIB:

Find lastest version here: https://zlib.net/

My version is https://zlib.net/zlib-1.2.12.tar.gz

sudo wget https://zlib.net/zlib-1.2.12.tar.gz
sudo tar -xf zlib-1.2.12.tar.gz 

PCRE

Find lastest version here: https://www.pcre.org/ or https://sourceforge.net/projects/pcre/files/pcre2/

My version is pcre2-10.37.tar.gz

sudo wget https://udomain.dl.sourceforge.net/project/pcre/pcre2/10.37/pcre2-10.37.tar.gz
tar -xf pcre2-10.37.tar.gz 

OPEN SSL

Find lastest version here: https://www.openssl.org/source/ or

My version is openssl-3.0.5.tar.gz 

sudo wget https://www.openssl.org/source/openssl-3.0.5.tar.gz
tar -xf openssl-3.0.5.tar.gz

NGINX RTMP MODULE

Have some fork version of this module. So, many people said this is a best choice

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

NGINX

Find lastest NGINX version here: https://nginx.org/en/download.html

My version is nginx-1.23.1

sudo wget https://nginx.org/download/nginx-1.23.1.tar.gz
tar -xf nginx-1.23.1.tar.gz
cd nginx-1.23.1

Configure then install NGINX with all packages above

cd nginx-1.23.1
./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.23.1             --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=../pcre2-10.37             --with-pcre-jit             --with-zlib=../zlib-1.2.12             --with-openssl=../openssl-3.0.5             --with-openssl-opt=no-nextprotoneg             --add-module=../nginx-rtmp-module             --with-debug
sudo make
sudo make install

sudo ln -s /usr/lib64/nginx/modules /etc/nginx/modules
sudo useradd -r -d /var/cache/nginx/ -s /sbin/nologin -U nginx
sudo mkdir -p /var/cache/nginx/
sudo chown -R nginx:nginx /var/cache/nginx/
sudo nginx -t
sudo nginx -V

sudo cat << EOF >> /lib/systemd/system/nginx.service
[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
EOF
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx

Create a folder for containing your offline video:

sudo mkdir -p /opt/video_storage
sudo chown -R nginx:nginx /opt/video_storage

Download a mp4 video to /opt/video_storage.

Edit /etc/nginx/nginx.conf. My example, public HTTP port is 9999, RTMP port is 1935.

worker_processes  auto;
events {
    worker_connections  1024;
}

# RTMP configuration
rtmp {
    server {
        listen 1935; # Listen on standard RTMP port
        chunk_size 4000;
		
	# RTMP video on demand MP4
        application vod {
            play /opt/video_storage;
        }

        # RTMP livestream (Streamer)
        application rtmp_livestream {
            live on;
        }
		
	# Client is watching livestream 
	    application hls_livestream {
            live on;
            hls on;
            hls_path /opt/hls/;
            # hls_fragment 3;
            # hls_playlist_length 60;
            # deny play all;
        }
    }
}

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

    server {
        listen 9999;

        location /hls {
            add_header 'Cache-Control' 'no-cache';

            # CORS configuration
            # add_header 'Access-Control-Allow-Origin' '*' always;
            # add_header 'Access-Control-Expose-Headers' 'Content-Length';
            # 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 /opt/;
        }
    }
}

Reload nginx:

sudo nginx -t
sudo systemctl restart nginx

Open firewall:

sudo firewall-cmd --zone=public --add-port=9999/tcp --permanent
sudo firewall-cmd --zone=public --add-port=1935/tcp --permanent
sudo firewall-cmd --reload

INSTALL FFMPEG

My example is Rocky Linux/ Centos

sudo dnf -y update
sudo dnf -y install https://download.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo yum-config-manager --enable powertools
sudo dnf install --nogpgcheck https://mirrors.rpmfusion.org/free/el/rpmfusion-free-release-8.noarch.rpm -y
sudo dnf install --nogpgcheck https://mirrors.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-8.noarch.rpm -y
sudo dnf install -y ffmpeg
sudo dnf -y install ffmpeg-devel
rpm -qi ffmpeg
ffmpeg -version

TEST

ffmpeg -fflags +igndts -hide_banner -i https://nhkworld.webcdn.stream.ne.jp/www11/radiojapan/all/263949/live_s.m3u8 -c copy -f flv rtmp://127.0.0.1/hls_livestream/stream

Now, I can open my Re-Stream on: http://my-ip:9999/hls/stream.m3u8 by VLC desktop

Note:

  • hls_livestream“: is defined in nginx config for stream
  • stream“: is file name in my temp folder (/opt/hls/), extension is .m3u8
  • hls“: is defined path on http protocol

You can test more ffmpeg function as stream from offline mp4,…

Example 2: Stream from mp4 file

I have a file /opt/video_storage/0005.mp4, then:

ffmpeg -re -i /opt/video_storage/0005.mp4 -vcodec libx264 -vprofile baseline -g 30 -acodec aac -strict -2 -f flv rtmp://127.0.0.1/hls_livestream/stream

Again, I try open stream on VLC: http://my-ip:9999/hls/stream.m3u8

Or, you want to add image as video for audio file:

ffmpeg -loop 1 -i /opt/video_storage/banner-04.jpg  -i /opt/video_storage/1.mp3 -c:v libx264 -ac 2  -ab 96k -f flv rtmp://127.0.0.1/hls_livestream/stream

Even on re-stream radio:

ffmpeg -loop 1 -i /opt/video_storage/banner-04.jpg  -i https://strm.voh.com.vn/radio/channel2/playlist.m3u8 -c:v libx264 -ac 2  -ab 96k -f flv rtmp://127.0.0.1/hls_livestream/stream

Test all now!

If NGINX can not start, check error by:

sudo systemctl status nginx

If your error is “13: Permission deny”, may be your port be restrict by system. Enable it by (my example is 8090/tcp):

dnf install -y policycoreutils-python-utils
semanage port -a -t http_port_t  -p tcp 8090

Donate info:

ETH (erc20): `0x17e333b82011d8507654af5fcec467860299d607`
BTC: `1FD3GgNxMXe1MeDic6Aa5MLW8FEggUAWhA`

,

DMCA.com Protection Status


Leave a Reply

Your email address will not be published. Required fields are marked *