【流媒體服務器】centos7 下使用nginx、nginx-rtmp-module搭建流媒體服務器

1、環境

  • VMware Workstation 14 Pro
  • 操作系統:centos7(1804)

2、搭建步驟

2.1 安裝CentOS7

2.2 更換阿里yum(個人喜好)

  • 下載wget
yum install -y wget
  • 備份默認的yum
mv /etc/yum.repos.d /etc/yum.repos.d.backup
  • 設置新的yum目錄
mkdir /etc/yum.repos.d
  • 下載阿里yum配置到該目錄中
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  • 重建緩存
yum clean all yum makecache
  • 升級所有包(改變軟件設置和系統設置,系統版本內核都升級,故需要幾分鐘耐心等待)
yum update -y

2.3 安裝步驟

  • 安裝構建環境
yum install git gcc make pcre-devel openssl-devel
  • 創建build目錄並切換至build目錄
mkdir ~/build 
cd ~/build
  • 下載nginx-rtmp-module
git clone git://github.com/arut/nginx-rtmp-module.git
  • 下載nginx並解壓
wget http://nginx.org/download/nginx-1.15.0.tar.gz
tar xzf nginx-1.15.0.tar.gz
  • 編譯
cd nginx-1.15.0
./configure --with-http_ssl_module --add-module=../nginx-rtmp-module
make
make install

2.4 配置和注意事項

  • 配置文件
    配置文件路徑:/usr/local/nginx/conf/nginx.conf,配置如下
worker_processes  1;
error_log  logs/error.log debug;
events {
    worker_connections  1024;
}
rtmp {
    server {
        listen 1935;
        application live {
            live on;
            recorder rec1{
                record all manual;
                record_unique on;
                record_path /record;
                record_suffix -%Y-%m-%d-%H_%M_%S.flv;
            }
        }
        application hls {
            live on;
            hls on;  
            hls_path /temp/hls;  
            hls_fragment 8s;  
        }
    }
}
http {
    server {
        listen      8080;
        location / {
            root html;
        }
        # rtmp stat
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
            # you can move stat.xsl to a different location
            root ~/build/nginx-rtmp-module;
        }
        location /hls {  
            #server hls fragments  
            types{  
                application/vnd.apple.mpegurl m3u8;  
                video/mp2t ts;  
            }  
            alias /temp/hls;  
            expires -1;  
        }  

        # rtmp control
        location /control {
            rtmp_control all;
        }

    }
}
  • 啓動nginx
/usr/local/nginx/sbin/nginx
  • 停止nginx
/usr/local/nginx/sbin/nginx -s stop
  • 注意事項

1、驗證,訪問http://IP:8080,無法訪問,請查防火牆配置,關閉防火牆

firewall-cmd --state #查看默認防火牆狀態(關閉後顯示notrunning,開啓後顯示running)
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啓動

2、配置了recorder,但是無法錄製

ps -aux|grep nginx #查看nginx work process的用戶
查看配置文件設置的錄製目錄(record_path)所屬用戶
配置nginx work process,在/usr/local/nginx/conf/nginx.conf中
user nobody改爲或首行添加user root(我的record_path配置的/record目錄所屬用戶是root)

PS:
參照:https://github.com/arut/nginx-rtmp-module/wiki/Getting-started-with-nginx-rtmp

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章