欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-08 22:20:42
...

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