美文网首页
直播推流服务器端搭建

直播推流服务器端搭建

作者: Damon_He | 来源:发表于2019-11-06 13:28 被阅读0次

环境准备

  • 下载Nginx wget http://nginx.org/download/nginx-1.16.0.tar.gz

  • 解压Nginx tar -zxvf nginx-1.16.0.tar.gz

  • 下载Nginx RTMP模块 wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz

  • 解压Nginx RTMP模块 tar -zxvf v1.2.1.tar.gz

编译安装

  • ./configure --help > nginx_configure_help.txt 将help输出text方便查看

  • 进入Nginx解压目录,,执行configure:./configure --prefix=./bin --add-module=../nginx-rtmp-module-1.2.1

nginx 中 gzip 模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要 openssl 库。所以如果服务器未安装这三个依赖库的话会报错,需要先安装这三个依赖库

  • 执行完生成 Makefile 文件

  • 编译:make install

  • 编译完生成bin目录

$ ls
auto  bin  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
$ cd bin/
$ ls
conf  html  logs  sbin

conf:配置相关
html:欢迎页面、错误页面
logs:日志存放区
sbin:可执行文件存放区

修改配置

Nginx默认不支持rtmp,需要修改配置文件。

如何修改,可以参考:nginx-rtmp-module-1.2.1/test/nginx.conf

进入bin/conf目录,找到 nginx.conf 文件。

worker_processes  1;
error_log  logs/error.log debug;
events {
    worker_connections  1024;
}
#rtmp标签
rtmp {
    #服务标签,一个rtmp服务中可以有多个server标签,每个标签可监听不同端口号
    server {
        #注意端口占用,1935为默认端口
        listen 1935;
        #应用标签,一个服务标签中可以有多个应用标签
        application myapp {
            live on;
            #丢弃闲置5s的连接
            drop_idle_publisher 5s;
        }
    }
}
http {
    server {
        #注意端口占用
        listen      8080;
        #数据统计模块,将流媒体的状态记录到 stat.xsl 中
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        #将stat.xsl 访问目录指定到nginx-rtmp-module中
        location /stat.xsl {
        #注意目录
            root /root/he/nginx-rtmp-module-1.2.1/;
        }
        #控制器模块,可录制直播视频、踢出推流/拉流用户、重定向推流/拉流用户
        location /control {
            rtmp_control all;
        }
        location /rtmp-publisher {
            #注意目录
            root /root/he/nginx-rtmp-module-1.2.1/test;
        }
        location / {
            #注意目录
            root /root/he/nginx-rtmp-module-1.2.1/test/www;
        }
    }
}

启动服务

进入sbin目录尝试执行nginx:

$ cd sbin/
$ ./nginx -t
nginx: [alert] could not open error log file: open() "./bin/logs/error.log" failed (2: No such file or directory)

-t 表示测试。

仔细看错误说明,"./bin/logs/error.log" 文件找不到?也就是当前目录下找不到 bin/logs/error.log。我们执行的当前目录是sbin,里面只有可执行文件nginx,当然找不到了。所以需要到nginx根目录下执行。

$cd ../../
$ ./bin/sbin/nginx -t
nginx: the configuration file ./bin/conf/nginx.conf syntax is ok
nginx: configuration file ./bin/conf/nginx.conf test is successful

测试通过,可以正式执行,启动服务了。

$ ./bin/sbin/nginx 

查看nginx服务进程。

$ ps aux | grep nginx
root      7245  0.0  0.2  53368  5320 ?        Ss   15:47   0:00 nginx: master process ./bin/sbin/nginx
nobody    7246  0.0  0.3  53768  6076 ?        S    15:47   0:00 nginx: worker process
root      7263  0.0  0.0 112708   984 pts/4    R+   15:50   0:00 grep --color=auto nginx

服务启动成功。

测试服务

在windows浏览器中通过http来访问:http://xxx.xxx.xxx.xxx:8080/

报错:403 Forbidden
其实前面我们查看nginx进程的时候,可以发现master process和worker process的用户不一致,一个是root而另一个是nobody

重新修改nginx.conf文件,添加root用户。

#设置为root用户
user root;
worker_processes  1;
error_log  logs/error.log debug;
...

配置文件更改了,需要重新加载配置文件。

./bin/sbin/nginx -s reload

刷新浏览器就正常了。

停止服务

1,从容停止服务
这种方法较stop相比就比较温和一些了,需要进程完成当前工作后再停止。
./bin/sbin/nginx -s quit
2,立即停止服务
这种方法比较强硬,无论进程是否在工作,都直接停止进程。
./bin/sbin/nginx -s stop
3,杀死进程
pkill -9 nginx

直播推流测试

推流可以使用EV录频。

设置串流地址:rtmp://xxx.xxx.xxx.xxx/myapp/

播放可以使用EV播放器。

播放网络流地址:rtmp://xxx.xxx.xxx.xxx/myapp/

相关文章

网友评论

      本文标题:直播推流服务器端搭建

      本文链接:https://www.haomeiwen.com/subject/ymqqbctx.html