0. 准备

  • 使用Raspberry Pi官方desktop full 32位系统镜像烧录TF卡,推荐rufus烧录软件。完整的桌面版系统镜像可以省下很多依赖安装与调试的功夫。

  • 准备好一个USB摄像头,随便一个能用的就行。

  • sudo raspi-config开启摄像头支持,这个很重要。

1. Nginx-RTMP服务器搭设

这一步我们通过自己编译,安装支持RTMP模块的Nginx。

  • 安装依赖
    sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

  • 下载nginx和nginx-rtmp-module源码,解压nginx

使用最新stable版本即可
wget http://nginx.org/download/nginx-1.24.0.tar.gz

git clone https://github.com/arut/nginx-rtmp-module.git

tar -zxvf nginx-1.24.0.tar.gz

  • 在nginx解压后的目录下新建一个extra文件夹,并将nginx-rtmp-module目录移到extra目录下,执行configure以生成Makefile配置:
    ./configure --with-http_ssl_module --add-module=./extra/nginx-rtmp-module

  • 编译nginx
    make -j4
    过程中出现死机请检查供电和散热。

安装自编译的nginx
sudo make install

2. nginx rtmp配置

默认编译安装的nginx配置文件在/usr/local/nginx/conf目录下,配置rtmp server功能需要修改该目录下nginx.conf文件,在文件的末尾,添加以下内容:
/home/zht/Videos替换成你的实际路径。

# rtmp server
rtmp {
      server {
            listen 1935;
             chunk_size 4096;

             # live用于直播
             application live {
                  live  on;
                 record off;
             }

             # vod用于点播
            application vod {
                    play /home/zht/Videos;
             }
       }
}
  • 测试rtmp服务器
    启动nginx,执行

sudo /usr/local/nginx/sbin/nginx

  • 点播测试
    /home/zht/Videos目录下添加一个测试文件,比如test.mp4,用自己的pc,打开播放器(比如vlc),输入rtmp点播URL:rtmp://192.168.18.193/vod/test.mp4

开机启动&进程守护

/usr/lib/systemd/system/目录下面新建一个nginx.service文件。并赋予可执行的权限。

编辑nginx.service内容:

[Unit]                                                                                      //对服务的说明
Description=nginx - high performance web server              //描述服务
After=network.target remote-fs.target nss-lookup.target   //描述服务类别

[Service]                                                                                 //服务的一些具体运行参数的设置
Type=forking                                                                         //后台运行的形式
PIDFile=/usr/local/nginx/logs/nginx.pid                               //PID文件的路径
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf   //启动准备
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf           //启动命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload                                                 //重启命令
ExecStop=/usr/local/nginx/sbin/nginx -s stop                                                       //停止命令
ExecQuit=/usr/local/nginx/sbin/nginx -s quit                                                        //快速停止
PrivateTmp=true                                                                  //给服务分配临时空间

[Install]
WantedBy=multi-user.target                                               //服务用户的模式

删掉中文注释,后

sudo systemctl daemon-reload

sudo systemctl start nginx.service

最后修改日期: 2023年7月20日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。