树莓派搭建nginx+rtmp服务器
http://bbs.eeworld.com.cn/thread-506444-1-1.html
1、安装依赖包
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
2、下载nginx和nginx-rtmp-module源码,解压nginx
wget http://nginx.org/download/nginx-1.10.3.tar.gz
git clone https://github.com/arut/nginx-rtmp-module.git
tar -C /usr/work/lib -zxvf nginx-1.10.3.tar.gz
3、在nginx解压后的目录下新建一个extra文件夹,并将nginx-rtmp-module目录移到extra目录下,执行configure以生成Makefile配置:
./configure --with-http_ssl_module --add-module=./extra/nginx-rtmp-module
报错:找不到zlib库
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
解决方案:官网下载zlib,解压安装
wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar -C /usr/work/lib -zxvf zlib-1.2.11.tar.gz
cd /usr/work/lib/zlib-1.2.11
./configure
make
make install
4、编译nginx
make -j4
报错:
解决方法:将src/os/unix/ngx_user.c文件中第26行注释,并将objs/Makefile文件中的-Werror删掉
5、安装
sudo make install
默认安装目录/usr/local/nginx
6、nginx rtmp配置
nginx配置文件在/usr/local/nginx/conf目录下,配置rtmp server功能需要修改该目录下nginx.conf文件,在文件的末尾(大括号之后,不要添加到大括号里面)添加以下内容
# rtmp server
rtmp {
server {
listen 1935;
chunk_size 4096;
//live用于直播
application live {
live on;
record off;
}
//vod用于点播
application vod {
play /home/robot/pi/Videos;
}
}
}
7、测试rtmp服务器
启动nginx,执行sudo /usr/local/nginx/sbin/nginx
7.1点播测试
在/home/robot/pi/Videos目录下添加一个测试文件,比如test.mp4,用自己的pc,打开播放器(比如vlc),输入rtmp点播URL:rtmp://192.168.3.9/vod/test.mp4
7.2直播测试
大疆无人机设置直播上传网址:rtmp://192.168.3.9/live
用自己的pc,打开播放器(比如vlc),输入rtmp直播URL:rtmp://192.168.3.9/live
8、设置开机自启动rtmp服务器
8.1、创建/etc/rc.local脚本文件,并写入想要运行的脚本程序
#!/bin/bashsudo -S /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf << EOF
root 密码
EOF
exit 0
8.2、设置/etc/rc.local脚本可执行权限
sudo chmod +x /etc/rc.local
8.3、创建/etc/init.d/ngnix文件为ngnix开机启动脚本
#!/bin/bash
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
# If the daemon file is not found, terminate the script.
test -x $DAEMON || exit 0
d_start() {
$DAEMON || echo -n " already running"
}
d_stop() {
$DAEMON –s quit || echo -n " not running"
}
d_reload() {
$DAEMON –s reload || echo -n " could not reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
# Sleep for two seconds before starting again, this should give the
# Nginx daemon some time to perform a graceful stop.
sleep 2
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0
8.4、设置/etc/init.d/ngnix脚本可执行权限
chmod +x /etc/init.d/nginx
8.5、设置开机自启动
sudo update-rc.d nginx defaults