搭建 RTMP 服务器实现摄像头在线实时播放
在 Alibaba Cloud Linux (Aliyun Linux) 2.1903 LTS 上搭建 RTMP 服务器的方法
方法一:使用 Nginx + nginx-rtmp-module(推荐)
步骤 1:安装依赖
sudo yum update
sudo yum install -y gcc make pcre-devel zlib-devel openssl-devel git wget
步骤 2:下载源码
wget https://nginx.org/download/nginx-1.25.3.tar.gz
tar -zxvf nginx-1.25.3.tar.gz
git clone https://github.com/arut/nginx-rtmp-module.git
步骤 3:编译安装 Nginx 支持https
cd nginx-1.25.3
./configure --add-module=../nginx-rtmp-module --with-http_ssl_module
make -j4
sudo make install
步骤 4:配置 Nginx
编辑配置文件 /usr/local/nginx/conf/nginx.conf,在末尾添加以下内容(保留原有配置):
worker_processes 1;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
record off;
# 启用 HLS(可选)
hls on;
hls_path /tmp/hls;
hls_fragment 5s;
hls_playlist_length 15s;
}
}
}
# HTTP 服务器配置(用于 HLS)
http {
server {
listen 9091;
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
expires -1;
add_header Cache-Control no-cache; #跨域支持
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/nginx/; # 替换为实际模块路径
}
}
server {
listen 443 ssl;
server_name 8.136.2.112; # 替换为实际 IP
ssl_certificate /usr/local/nginx/ssl/server.crt;
ssl_certificate_key /usr/local/nginx/ssl/server.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
expires -1;
add_header Cache-Control no-cache; #跨域支持
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/nginx/; # 替换为实际模块路径
}
}
}
sudo /usr/local/nginx/sbin/nginx -t # 验证语法
sudo /usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx/sbin/nginx -V
步骤 5:创建 HLS 目录并设置权限
sudo mkdir -p /tmp/hls
sudo chmod 777 /tmp/hls
步骤 6:启动 Nginx
sudo /usr/local/nginx/sbin/nginx
步骤 7:开放防火墙端口
# Alibaba Cloud Linux 默认使用 firewalld
sudo firewall-cmd --permanent --add-port=1935/tcp # RTMP 端口
sudo firewall-cmd --permanent --add-port=80/tcp # HLS 端口(如果启用)
sudo firewall-cmd --reload
nginx配置连接监控: http://<服务器公网IP>/stat
以及相关地址
sudo /usr/local/nginx/sbin/nginx
sudo /usr/local/nginx/sbin/nginx -s reload
rtmp://<服务器公网IP>/live/设备序列号
http://<服务器公网IP>/hls/设备序列号.m3u8
http://<服务器公网IP>/stat

测试推流与播放
-
推流工具(如 OBS):
-
服务器地址:
rtmp://<服务器公网IP>/live -
流密钥:自定义(如
test)
-
-
拉流地址:
-
RTMP:
rtmp://<服务器公网IP>/live/test -
HLS(若启用):
http://<服务器公网IP>/hls/test.m3u8
-
-
使用 VLC 播放:
-
打开 VLC,选择 媒体 > 打开网络串流,输入上述地址。
-
关键注意事项
-
阿里云安全组规则:
-
确保安全组开放以下端口:
-
入方向:
1935/TCP(RTMP)、80/TCP(HLS)、8080/TCP(SRS的HTTP服务)。
-
-
配置路径:阿里云控制台 > 云服务器ECS > 安全组 > 配置规则。
-
-
SELinux 问题:
-
若遇到权限问题,临时禁用 SELinux:
sudo setenforce 0 # 临时关闭 sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config # 永久关闭(需重启)
-
-
日志排查:
-
Nginx 日志路径:
/usr/local/nginx/logs/error.log -
SRS 日志路径:
./objs/srs.log
-
常见问题
如果出现nginx配置的stat.xsl404或者403,请检查文件路径地址以及权限
-
Nginx 启动失败:
-
检查端口冲突:
sudo netstat -tunlp | grep 1935 -
验证配置语法:
sudo /usr/local/nginx/sbin/nginx -t
-
-
HLS 文件未生成:
-
确保推流工具(如 OBS)正确配置了流密钥和服务器地址。
-
检查
/tmp/hls目录权限:sudo chmod -R 777 /tmp/hls
-
-
公网无法访问:
-
检查阿里云安全组规则和本机防火墙是否放行端口。
-
测试本地访问:
curl http://localhost/hls/test.m3u8(若失败,优先解决本地配置)。
-

浙公网安备 33010602011771号