使用Ubuntu平台
1.先下载安装nginx和nginx-rtmp编译依赖工具
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
2.创造一个工作目录,并切换到工作目录
mkdir ~/working
cd ~/working
单独创建一个工作目录,避免干扰,
3.下载nginx和nginx-rtmp源码
wget http://nginx.org/download/nginx-1.20.1.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
tips:如果在Linux上面无法在github上面下载,可以科学上网在windows上下载好后,用WinSCP连接虚拟机和Windows,把GitHub上面压缩包拖到虚拟机里面
4.解压nginx和nginx-rtmp安装包
先安装unzip工具
sudo apt-get install unzip
解压压缩文件
tar -zxvf nginx-1.20.1.tar.gz
unzip master.zip
5.编译安装
cd nginx-1.20.0
./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master
make && make install
tips:
这里需要注意./configure --with-http_ssl_module --add-module=…/nginx-rtmp-module-master的时候可以能遇到解压导致的名字不对应的问题,题主这里因为遇到unzip master.zip导致名字错误,module=…/nginx-rtmp-module-master后面的nginx-rtmp-module-master名字已经要和你已存在的名字对应
配置成功后使用命令:/usr/local/nginx/sbin/nginx -V
会提示有这一段,来检验你rtmp是否安装成功
configure arguments: --prefix=/usr/local/nginx --add-module=…/nginx-rtmp-module (因为题主这里的文件名使用的是nginx-rtmp-module后面没有master,因为使用./configure --with-http_ssl_module --add-module=…/nginx-rtmp-module-master的时候不知道为什么把-master漏掉了。。。)
6.安装nginx init脚本
vim /etc/init.d/nginx
写入以下代码
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
#注意:这里的三个变量需要根据具体的环境而做修改。
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Check that networking is up.
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
$nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ]
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
$nginxd -s stop
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx $nginx_pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
kill -HUP `cat ${nginx_pid}`
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
为所有用户添加权限
sudo chmod a+x /etc/init.d/nginx
7.启动ngix
/etc/init.d/nginx start
在Linux平台下,可以使用:
ip addr show
来查看ip地址
如果启动成功在Windows上面输入自己的id地址
http://你自己的ip/
会出现界面
8.配置Nginx-rtmp服务器
vim /usr/local/nginx/conf/nginx.conf
在下面代码添加
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
record off;
hls on;
hls_path /usr/share/nginx/html/hls;
hls_fragment 2s;
exec 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;
}
application live360p {
live on;
record off;
}
application hls360p {
live on;
hls on;
hls_path /usr/share/nginx/html/hls2;
hls_fragment 2s;
}
}
tips:注意大括号匹配问题
创建hls的目录:
mkdir -p /usr/share/nginx/html/hls
之后重启nginx服务
/etc/init.d/nginx restart
其实就是把启动ngix里面的start换成了restart
OBS推流:
在直播里面设置服务器为rtmp://你的虚拟机里面的ip/live
然后在进入的主界面里面点击“开始直播”
VLC拉流
在媒体里面打开网络串流
在输入网络URL中,输入上面你在OBS里面的服务器地址,即rtmp://你的虚拟机里面的ip/live,然后点击播放即可完成