虚拟机设置为桥接模式,与本机地址同网段
安装git
yum -y install git
下载nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git
安装openssl
yum -y install openssl openssl-devel
下载nginx-1.10.3.tar.gz
wget http://nginx.org/download/nginx-1.10.3.tar.gz
解压nginx-1.10.3.tar.gz
tar -zxvf nginx-1.10.3.tar.gz
添加rtmp和openssl支持
cd nginx-1.10.3
./configure --add-module=/root/nginx-rtmp-module --with-http_ssl_module
编译安装
make && make install
启动nginx
/usr/local/nginx/sbin/nginx
如出现错误
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] still could not bind()
表示80端口被占用,需要查看端口PID,然后根据PID将相关进程关闭后重启
lsof -i tcp:80
kill PID
/usr/local/nginx/sbin/nginx
修改nginx配置文件nginx.conf,rtmp配置
vi /usr/local/nginx/conf/nginx.conf
按i进入编辑模式,在最后的空白处,添加以下:
rtmp {
server {
listen 1935;
chunk_size 4000;
application hls {
live on;
hls on;
hls_path /home/hls/test;
hls_fragment 3s;
}
}
}
rtmp是协议名称
server 说明内部中是服务器相关配置
listen 监听的端口号, rtmp协议的默认端口号是1935
application 访问的应用路径是 hls
live on; 开启实时
record off; 不记录数据
hls配置,http的server下添加以下:
server {
listen 80;
location /hls {
#server hls fragments
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /home/hls/test;
expires -1;
}
按esc 输入:wq 回车保存,重启nginx
[root~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
进入/home目录并创建 hls及其子目录test
cd /home
mkdir hls
cd hls
mkdir test
服务器开放1935端口或关闭防火墙
//CentOS7 默认使用firewalld防火墙
//关闭firewall
systemctl stop firewalld.service
//禁止firewall开机启动
systemctl disable firewalld.service
//查看防火墙状态(关闭后显示not running,开启后显示 running)
firewall-cmd --state
//iptables开放端口
/sbin/iptables -I INPUT -p tcp --dport 1935 -j ACCEPT
//保存配置
/etc/rc.d/init.d/iptables save
//重启服务
/etc/rc.d/init.d/iptables restart
//查看端口开放状态
/etc/init.d/iptables status
打开浏览器,输入ip地址,显示如下表明搭建成功:
使用OBS推流rtmp
使用vlc拉流
ipc推流
使用OBS推流hls
使用safari拉流hls(http://172.18.30.231/hls/test.m3u8)
此时服务器上的/home/hls/test/目录下会生成一个m3u8文件以及多个ts文件
至此,模拟结束。