NGINX + RTMP + HLS 自动部署总结文档

1. 安装依赖
sudo apt-get update
sudo apt-get install -y build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev openssl

⚠ 注意:如果出现依赖冲突,请先解决 libssl3、libdpkg-perl 等版本问题。
检查并清除 hold 的软件包
dpkg --get-selections | grep hold

如果看到:

libssl3 hold
libdpkg-perl hold

解除 hold:

sudo apt-mark unhold libssl3 libdpkg-perl

然后重新更新:

sudo apt update
sudo apt install -f
sudo apt upgrade

2. 编译 NGINX 并添加 RTMP 模块

下 载 nginx-1.20.2 源 码 ( 下 载 地 址 : http://nginx.org/download/nginx-1.20.2.tar.gz) 和nginx-rtmp-module(下载地址:hyttps://github.com/arut/nginx-rtmp-module),网盘“iTOP-3588开发板\02_【iTOP-RK3588 开发板】开发资料\09_Linux 系统开发配套资料\05_RTMP 开发配套

资料”目录下提供了下载好的。

3.将 nginx-1.20.2.tar.gz 和 nginx-rtmp-module-master.zip 拷贝到开发板,使用以下命令解压,如下图所示:

tar -vxf nginx-1.20.2.tar.gz

unzip nginx-rtmp-module-master.zip

cd nginx-1.20.2
./configure --prefix=/usr/local/nginx-1.20.2 --add-module=../nginx-rtmp-module-master --with-http_ssl_module
make
sudo make install
3. 创建 Web 服务目录
sudo mkdir -p /usr/share/nginx/html/live
sudo touch /usr/share/nginx/html/index.html

可写入简单 HTML 页面:

echo '<h1>Nginx RTMP + HLS Server</h1>' | sudo tee /usr/share/nginx/html/index.html
4. 设置目录权限(安全方式)
if id "www-data" &>/dev/null; then
    sudo chown -R www-data:www-data /usr/share/nginx
else
    sudo chown -R nginx:nginx /usr/share/nginx
fi
sudo chmod -R 755 /usr/share/nginx
5. 配置 nginx.conf(支持 RTMP + HLS)
worker_processes  1;

events {
    worker_connections 1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout 65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html;
        }

        location /live {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /usr/share/nginx/html;
        }
    }
}

rtmp {
    server {
        listen 1096;
        chunk_size 4096;

        application live {
            live on;
            record off;
            allow play all;

            # HLS 设置
            hls on;
            hls_path /usr/share/nginx/html/live;
            hls_fragment 3;
            hls_playlist_length 15;
        }
    }
}
6. 生成浏览器播放页面(player.html)
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>HLS Live Player</title>
    <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
</head>
<body>
    <h2>Live Stream</h2>
    <video id="video" controls autoplay width="800"></video>
    <script>
        var video = document.getElementById('video');
        var videoSrc = '/live/stream.m3u8';
        if (Hls.isSupported()) {
            var hls = new Hls();
            hls.loadSource(videoSrc);
            hls.attachMedia(video);
        } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
            video.src = videoSrc;
        }
    </script>
</body>
</html>
7. 启动 NGINX
sudo /usr/local/nginx-1.20.2/sbin/nginx -c /usr/local/nginx-1.20.2/conf/nginx.conf
ps -aux | grep nginx
8. 推流测试
ffmpeg -re -i test.mp4 -c copy -f flv rtmp://<IP>:1096/live/stream
9. 浏览器播放
http://<IP>/player.html
10. 注意事项
  • 确保端口 1096 没被占用
  • 如果需要 HLS 支持,直播源必须推送到 /live/stream
  • 目录权限尽量使用 nginx 或 www-data 用户,不建议 777
  • 如果系统中 libssl-dev 安装失败,请先解决依赖版本问题
完整脚本
#!/bin/bash

NGINX_DIR=/usr/local/nginx-1.20.2
NGINX_CONF=$NGINX_DIR/conf/nginx.conf
WEB_ROOT=/usr/share/nginx/html
LIVE_DIR=$WEB_ROOT/live

echo "==== 创建 Web 目录 ===="
sudo mkdir -p $LIVE_DIR
sudo touch $WEB_ROOT/index.html

echo "<h1>Nginx RTMP + HLS Server</h1>" | sudo tee $WEB_ROOT/index.html > /dev/null

echo "==== 生成 HLS 播放网页(player.html) ===="
sudo tee $WEB_ROOT/player.html > /dev/null <<EOF
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>HLS Live Player</title>
    <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
</head>
<body>
    <h2>Live Stream</h2>
    <video id="video" controls autoplay width="800"></video>
    <script>
        var video = document.getElementById('video');
        var videoSrc = '/live/stream.m3u8';

        if (Hls.isSupported()) {
            var hls = new Hls();
            hls.loadSource(videoSrc);
            hls.attachMedia(video);
        } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
            video.src = videoSrc;
        }
    </script>
</body>
</html>
EOF

# 设置权限
echo "==== 设置目录权限 ===="
if id "www-data" &>/dev/null; then
    sudo chown -R www-data:www-data /usr/share/nginx
else
    sudo chown -R nginx:nginx /usr/share/nginx
fi
sudo chmod -R 755 /usr/share/nginx

echo "==== 备份 nginx.conf ===="
sudo cp $NGINX_CONF $NGINX_CONF.bak_$(date +%s)

echo "==== 写入新的 nginx.conf(RTMP + HLS) ===="
sudo tee $NGINX_CONF > /dev/null <<EOF
worker_processes  1;

events {
    worker_connections 1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout 65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html;
        }

        location /live {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /usr/share/nginx/html;
        }
    }
}

rtmp {
    server {
        listen 1096;     # RTMP 端口
        chunk_size 4096;

        application live {
            live on;
            record off;

            # HLS 支持
            hls on;
            hls_path /usr/share/nginx/html/live;
            hls_fragment 3;
            hls_playlist_length 15;
        }
    }
}
EOF

echo "==== 检查 nginx 配置 ===="
$NGINX_DIR/sbin/nginx -t -c $NGINX_CONF

if [ $? -ne 0 ]; then
    echo "❌ nginx 配置检查失败,请检查"
    exit 1
fi

echo "==== 启动 nginx ===="
sudo $NGINX_DIR/sbin/nginx -c $NGINX_CONF

echo "==== 检查 nginx 进程 ===="
ps -aux | grep nginx

echo
echo "=========================================="
echo "   ✔ NGINX RTMP + HLS 已成功部署!"
echo "=========================================="
echo "RTMP 推流地址: rtmp://<你的IP>:1096/live/stream"
echo "HLS 播放地址:  http://<你的IP>/player.html"
echo "HLS 文件路径:  /usr/share/nginx/html/live/"
echo "=========================================="
echo

在这里插入图片描述

Logo

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。

更多推荐