搭建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-mpegURLvideo/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播放时首帧出现问题,可改为offgop_cache on; #可以减少首帧延迟}}
}
参数说明:
rtmp
协议名称server
说明内部中是服务器相关配置listen
监听的端口号,rtmp 协议的默认端口号是 1935application
访问的应用路径是,这里设置为了 livelive 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 nginxnginx的路径为:/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快速查看配置文件的方法
搭建Nginx+rtmp直播服务器相关推荐
- 宝塔 搭建 nginx rtmp 流媒体服务器
宝塔 搭建 nginx rtmp 流媒体服务器 安装环境说明 系统环境: Centos 7 机型: DELL R540 准备工作 nginx 添加模块,编译安装 nginx ,下载 nginx-rtm ...
- 阿里云服务器搭建Nginx+rtmp推流服务器
title: 阿里云服务器搭建Nginx+rtmp推流服务器 categories:[Centos] tags:[音视频编程] date: 2021/11/16 一.前期准备 服务器操作系统:Cent ...
- mac搭建nginx+rtmp直播流
1.nginx安装 (1)克隆github的项目 brew tap denji/nginx (2)安装nginx+rtmp模块 brew install nginx-full --with-rtmp- ...
- win7系统搭建流媒体服务器,windows7 下 搭建 nginx + rtmp 流媒体服务器
成果分享:https://github.com/ziq358/Nginx-Rtmp 材料准备: 1.Microsoft Visual Studio 2010 下载安装. 2.MinGW 安装. 3.下 ...
- armlinux 搭建nginx + rtmp服务器
1 综合篇 点我 2 uboot移植 点我 3 uboot lcd驱动 点我 4 kernel移植 点我 5 kernel lcd驱动 点我 6 摄像头驱动 点我 7 ubuntu base移植 点我 ...
- linux 搭建nginx + rtmp服务器
linux 搭建nginx + rtmp服务器 一 环境准备 虚拟机ubuntu 装备,安装一些nginx 必要的依赖和服务 sudo apt-get install libpcre3 libpcr ...
- FFmpeg入门详解之99:基于nginx的rtmp直播服务器(nginx-rtmp-module实现)
基于nginx的rtmp直播服务器(nginx-rtmp-module实现) 首先,在搭建服务之前先了解下目前主流的几个直播协议: 1.RTMP: 实时消息传输协议,Real Time Messagi ...
- 树莓派搭建nginx+rtmp服务器
树莓派搭建nginx+rtmp服务器 http://bbs.eeworld.com.cn/thread-506444-1-1.html 1.安装依赖包 sudo apt-get install bui ...
- Ubuntu18.04搭建nginx rtmp服务器
1.前言 系统:ubuntu18.04 安装所需要软件下载地址: https://download.csdn.net/download/u010798513/22847289 2. 安装prce 安装 ...
最新文章
- php设置backlog,高并发调优backlog多大合适?
- 如何強迫 .Net 應用程式輸出英文的例外訊息
- 给初学者们讲解人工神经网络(ANN)
- 【Leetcode】创建链表
- idea java web运行_使用IDEA创建JavaWeb项目 部署本地tomcat并运行
- ubuntu下各个目录的含义
- 王爽《汇编语言(第三版)》检测点11.1
- 你问我答,准备面试需要做哪些技术储备,面试官更加关心什么方面的技术点?...
- 北京航空航天大学计算机专业培养方案,北京航空航天大学计算机科学与技术专业...
- 星辰天合:为云存储而生 Ceph社区代码贡献领先的国产企业
- 3W+字的设计模式手册
- Matlab适配器模式
- (转载) Java线程池原理
- 【Kafka】Mac 环境 Kafka诡异问题之kafka eagle 界面无法访问
- Myeclipse 操作技巧
- C语言易混淆关键词详解-const, static, extern, typedef, 声明
- 双击事件 转载 http://blog.sina.com.cn/s/blog_739365a30100vk8p.html
- Python简单实现图书管理系统
- 唐诗三百首加密软件如何使用_绿盾加密软件如何设置邮件白名单
- 解决网页上不能直接复制文字的问题