zoukankan      html  css  js  c++  java
  • 搭建Nginx+rtmp直播服务器

    目录

    安装搭建 lnmp 环境

    安装 Nginx 的扩展模块

    关闭 nginx、php-fpm 服务

    查看 nginx 配置参数

    重新编译 nginx

    Centos7 端口开放

    查看防火墙状态

    开启防火墙

    关闭防火墙

    查看想开的端口是否已开

    开放端口号

    重新载入配置

    关闭端口

    配置 HLS 或 http-flv

    nginx快速查看配置文件的方法

    查看nginx路径

    查看nginx配置文件路径

    Nginx启动/重启/停止

    创建目录及文件

    开始推流

    hls 播放

    http-flv 播放

    推流验证


    安装搭建 lnmp 环境


    lnmp 环境一键安装脚本

    #下载并安装lnmp环境,将1.4替换为最新版本
    wget -c http://soft.vpser.net/lnmp/lnmp1.4.tar.gz
    tar -xzvf lnmp1.4.tar.gz
    cd lnmp1.4
    ./install.sh lnmp
    

    请按照脚本的提示选择你要安装的各个组件版本,若不确定,选择默认选项即可。

    此时访问服务器地址会出现Nginx界面。


    安装 Nginx 的扩展模块

    GitHub 项目地址:nginx-http-flv-module,克隆到本地即可:

    #为nginx创建扩展模块目录
    mkdir /usr/local/nginx/extend_module
    cd /usr/local/nginx/extend_module
    #下载扩展包到nginx扩展模块目录下
    git clone https://github.com/winshining/nginx-http-flv-module.git
    

    关闭 nginx、php-fpm 服务

    #关闭nginx
    killall -9 nginx
    systemctl stop nginx
    #关闭php-fpm
    systemctl stop php-fpm
    

    查看 nginx 配置参数

    注意:在部分系统下需要使用 service 代替 systemctl 命令。

    执行 nginx -V 会输出配置参数

    复制 configure arguments 后的所有参数,例如:

    --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module
    

    这会在后面用到。


    重新编译 nginx

    进入安装时生成的文件夹,并执行:

    cd lnmp1.4/src
    #解压nginx源码包,将1.14.0替换为你的版本,下同
    tar -xzvf nginx-1.14.0.tar.gz
    #进入nginx源码目录
    cd nginx-1.14.0
    #安装rtmp扩展模块,参数形式是:
    #./configure 加上 刚才复制的nginx configure参数 加上 --add-module=nginx-http-flv-module扩展包的目录
    #例如:
    ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --add-module=/usr/local/nginx/extend_module/nginx-http-flv-module
    #如果lnmp编译的nginx包含--add-module=openssl等,也需要一并解压,否则会报错找不到文件
    #编译
    make
    #安装
    make install
    #重启nginx
    systemctl start nginx
    systemctl start php-fpm
    

    重新启动 nginx 后,再次执行 nginx -V 查看安装状态,如果 configure arguments 后出现 nginx-http-flv-module 说明安装成功!


    Centos7 端口开放

    http://tool.chinaz.com/port/扫描服务器端口,检查开放状态。

    查看防火墙状态

    systemctl status firewalld

    running 状态即防火墙已经开启 
    dead 状态即防火墙未开启

    开启防火墙

    systemctl start firewalld

    关闭防火墙

    systemctl stop firewalld

    查看想开的端口是否已开

    firewall-cmd --query-port=8888/tcp

    提示yes表示已开通,提示no表示未开通。

    开放端口号

    firewall-cmd --add-port=8888/tcp --permanent

    提示success表示成功。

    重新载入配置

    要使配置生效一定要重新载入配置

    firewall-cmd --reload

    关闭端口

    firewall-cmd --permanent --remove-port=8888/tcp

    配置 HLS 或 http-flv

    对 nginx.conf 进行修改:

    #编辑配置
    cd /usr/local/nginx/conf/nginx.conf
    

    需要修改的是 http 下 server 的配置,其内容可参照:

    server
    {
        listen 80 default_server;
        #listen [::]:80 default_server ipv6only=on;
        server_name yourdomain.com;
        index index.html index.htm index.php;
        root  /home/www/; #服务器的根目录
        #error_page 404 /404.html;
        include enable-php.conf;
         
        #加入hls支持
        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8; 
                #或 application/x-mpegURL
                video/mp2t ts;
            }
            alias /home/hls/; #视频流文件目录(自己创建,与下方hls_path相同)
            expires -1;
            add_header Cache-Control no-cache;
        }
        #加入http-flv支持
        location /flv {
            flv_live on;
            chunked_transfer_encoding on;
        }
        #server下的其余配置无需改变,此处省略
    }
    #在文档末尾加入rtmp配置
    rtmp {
        server {
            listen 1935; #监听的端口
            chunk_size 4000;
            application live { #rtmp推流请求路径
                live on;
                record off;
                #publish_notify on; #推流验证,和下一行需一起使用
                #on_publish http://localhost/auth.php; #推流验证,具体可参照后文
    
                hls on;
                hls_path /home/hls/test; #视频流hls切片文件目录(自己创建)
                hls_fragment 3s; #hls单个切片时长,会影响延迟
                hls_playlist_length 32s; #hls总缓存时间,会影响延迟
    
                meta on; #如果http-flv播放时首帧出现问题,可改为off
                gop_cache on; #可以减少首帧延迟
            }
        }
    }
    

    参数说明:

    • rtmp 协议名称
    • server 说明内部中是服务器相关配置
    • listen 监听的端口号,rtmp 协议的默认端口号是 1935
    • application 访问的应用路径是,这里设置为了 live
    • live on 开启直播
    • record off 不记录数据

    修改完后执行 nginx -s reload 重载 nginx 配置

    nginx快速查看配置文件的方法

    查看nginx路径

    ps aux|grep nginx


    root              352   0.0  0.0  2468624    924   ??  S    10:43上午   0:00.08 nginx: worker process  
    root              232   0.0  0.0  2459408    532   ??  S    10:43上午   0:00.02 nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;  
    root             2345   0.0  0.0  2432772    640 s000  S+    1:01下午   0:00.00 grep nginx

    nginx的路径为:/usr/local/opt/nginx/bin/nginx 

    查看nginx配置文件路径

    使用nginx的 -t 参数进行配置检查,即可知道实际调用的配置文件路径及是否调用有效。

    nginx -t


    nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

    Nginx启动/重启/停止

    系统:CentOS 7下

    • 启动Nginx命令

    启动命令:systemctl start nginx

    • 重启Nginx命令

    重启命令:systemctl restart nginx

    • 停止Nginx命令

    停止命令:systemctl stop nginx


    创建目录及文件

    根据前面配置文件中的 hls_path 创建子目录 hls,例如 /home/hls,确保 nginx 服务器对其具有读写权限。
    在服务器的根目录,例如 /home/www 下创建 test.html 文件,进行 hls 播放:

    <!--test.html-->
    <html>
    <body>
        <video autoplay webkit-playsinline controls>
            <source src="http://yourdomain.com/hls/test.m3u8" type="application/vnd.apple.mpegurl" />
            <p class="warning">Your browser does not support HTML5 video.</p>
        </video>
        <video controls>
            <source src="http://yourdomain.com/hls/test.m3u8" type="application/x-mpegURL">
        </video>
    </body>
    </html>
    

    如果要使用 OBS Studio 等软件推流,地址设置为 rtmp://yourdomain.com:1935/live/test


    开始推流

    ffmpeg 也可以进行推流,用法是:

    ffmpeg -re -stream_loop -1 -i /path/to/your/video.mp4 -vcodec libx264 -acodec aac -f flv rtmp://yourdomain.com:1935/live/test
    

    版本较老的 ffmpeg 无法设置 -stream_loop -1,可以前往官方网站下载最新版本。


    hls 播放

    用 http 访问之前创建的 test.html,即可观看直播。
    如直播没有声音,请参考 ffmpeg 没有声音
     

    附录:浏览器对 hls 的支持

    如果浏览器并不原生支持 hls,可使用第三方插件,例如 hls.js;或者使用 video.js 配合 videojs-contrib-hls,这也是不错的解决方案。

    http-flv 播放

    对于 http-flv 播放,可以使用 VLC 等软件测试拉流,拉流地址为 http://yourdomain.com/flv?app=live&stream=test
    如果要实现 HTML5 播放,可以使用 B 站开源的 flv.js,更多内容可以参照其文档,此处不做更多说明。

    推流验证

    如果配置文件中开启了 publish_notify,那么 on_publish 项设置的页面将会被用作推流验证。如果验证失败,使这个页面返回 404 头即可。
    比如你设置了 on_publish http://localhost/auth.php,那么在 auth.php 中加入一下内容即可实现:

    <?php
    @$name = $_POST['name'];
    @$pass = $_POST['pass'];
    //在某些情况下,$_POST需要改为$_GET或$_REQUEST,请自行测试
    if (empty($name) || empty($pass)) {
        header("HTTP/1.0 404 Not Found");
        echo "串码流不正确!";
    }
    else {
        if ($pass == "password") {
            echo "串码流正确!";
        }
        else {
            header("HTTP/1.0 404 Not Found");
            echo "串码流不正确!";
        }
    }
    ?>
    

    这时的推流地址也要对应改为 rtmp://yourdomain.com:1935/live/test?pass=password


    参考文章:

    基于 Nginx 搭建视频直播服务器

    linux centos 7如何开放网络端口

    CentOS7启动/重启/停止Nginx命令大全

    nginx快速查看配置文件的方法

  • 相关阅读:
    Python-selenium 下拉框定位
    Python3+Selenium3自动化测试-(五)
    Python3+Selenium3自动化测试-(四)
    Python3+Selenium3自动化测试-(三)
    Python3+Selenium3自动化测试-(二)
    Python3+Selenium3自动化测试-(一)
    python selenium 下载安装(一)
    Elasticsearch查询——布尔查询Bool Query
    centos7 重启网卡失败
    kibana 创建饼图
  • 原文地址:https://www.cnblogs.com/Archger/p/12774656.html
Copyright © 2011-2022 走看看