Nginx nginx-rtmp-module 搭建直播流服务器教程(全)笔记
centos 7.2 安装Nginx和nginx-rtmp-module搭建直播流媒体服务器
本机安装目录
/usr/share/
1、在当前目录下载同版本的Nginx
cd /usr/share/
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar -zxvf nginx-1.12.2.tar.gz
2、更新下依赖相关包
yum -y install pcre*
3、下载nginx-rtmp-module
cd /usr/share/
git clone https://github.com/arut/nginx-rtmp-module.git
mv nginx-rtmp-module nginx-rtmp-module-1.1.10
4、进入nginx-1.12.2目录
cd /usr/share/nginx-1.12.2
5、编译模块
./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module-1.1.10 --with-
http_ssl_module
make #验证
make install
6、启动Nginx
cd /usr/local/nginx
./sbin/nginx
7、配置conf
参考资料
github:《nginx-rtmp-module》https://github.com/arut/nginx-rtmp-module
复制一份副本
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
编辑配置
vi /usr/local/nginx/conf/nginx.conf
rm -rf /usr/local/nginx/conf/.nginx.conf.swp
在events {}下面添加以下代码
参考《Nginx.conf》
nginx.rar
rtmp_auto_push on; # Multi-Worker
rtmp {
server {
listen 1935;
chunk_size 4096; #//数据传输块的大小
allow play all;
application live {
live on; # 启用rtmp
#publish_notify on; # 启用notify
# on_publish http://127.0.0.1/notify/publish;
# on_play http://127.0.0.1:8080/rtmp/notify/play;
# on_play_done http://127.0.0.1:8080/rtmp/notify/play_done;
# on_publish_done http://127.0.0.1/notify/publish_done;
# on_record_done http://127.0.0.1:8080/rtmp/notify/record_done;
# on_update http://127.0.0.1:8080/rtmp/notify/update;
hls on; # 启用hls
wait_key on; #对视频切片进行保护,这样就不会产生马赛克了
hls_path /www/web/default/live; #切片视频文件存放位置
hls_fragment 10s; #每个视频切片的时长
hls_continuous on; #连续模式
hls_cleanup on; #对多余的切片进行删
# hls_plahls_nested on; #嵌套模式
record all;
record_path /www/web/default/vod; # 录制保存地址
#record_append on;
record_unique on;
#record keyframes;
#record_path /www/web/default/vod;
#record_max_frames 100;
#record_interval 20m;
#exec /usr/local/ffmpeg-3.4-64bit-static/ffmpeg -i rtmp://localhost/live/$name -threads 1 -c:v libx264 -profile:v baseline -b:v 350K -s 640x360 -f flv -c:a aac -ac 1 -strict -2 -b:a 56k rtmp://localhost/live360p/$name;
# 跟踪客户信息
exec_play bash -c "echo $addr $pageurl >> /tmp/clients";
exec_publish bash -c "echo $addr $flashver >> /tmp/publishers";
# 转换录制的文件格式的MP4
# exec_record_done /usr/local/ffmpeg-3wp:4-64bit-static/ffmpeg -y -i $path -acodec libmp3lame -ar 44100 -ac 1 -vcodec libx264 $dirname/$basename.mp4;
}
# application live360p {
# live on;
# hls on; # 启用hls
# hls_path /home/live360p;
# record off;
# allow publish 127.0.0.1;
# deny publish all;
# }
}
}
在http{}前
8、nginx相关命令
/usr/local/nginx/sbin/nginx -s reload #重载配置
/usr/local/nginx/sbin/nginx -s stop #停止
/usr/local/nginx/sbin/nginx #开启
9、添加端口开放
查看端口是否开放
firewall-cmd --query-port=1935/tcp
永久开放
firewall-cmd --permanent --zone=public --add-port=1935/tcp
重启防火墙
systemctl restart firewalld.service
10、设置开机启动
vi /lib/systemd/system/nginx.service
服务代码描述:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
保存后重载下服务
systemctl daemon-reload
命令详解:
systemctl enable nginx.service #启动服务
systemctl status nginx.service #查看服务状态
systemctl start nginx.service #开启服务
systemctl stop nginx.service #停止服务
systemctl restart nginx.service #重启服务
11、检查推流端口是否正常
cmd
telnet 140.143.157.56 1935
12、直播目录
live /www/web/default/live
vod /www/web/default/vod
13、推流地址
直播地址:rtmp://140.143.157.56/live/
串码:1111