搭建 SRS 高性能直播服务器(CentOS 9 实操指南)



1. 环境准备

我们这次用的系统是 CentOS 9,保持系统更新:

dnf update -y

安装编译依赖:

dnf install -y gcc gcc-c++ make wget curl git cmake jemalloc jemalloc-devel \
openssl-devel pcre pcre-devel perl perl-core perl-FindBin perl-IPC-Cmd perl-Pod-Html

注意 Perl 模块一定要全,不然编译 OpenSSL 会报错 Can't locate FindBin.pm


2. 获取源码并编译

git clone https://github.com/ossrs/srs.git /opt/srs
cd /opt/srs/trunk

编译 SRS(开启 SSL + HLS + HTTP API):

./configure --with-ssl --with-hls --with-http-api
make -j$(nproc)

编译完成后,主程序就在:

/opt/srs/trunk/objs/srs

测试:

./objs/srs -v

3. 配置 SRS

我们写了一个 主配置 conf/srs.conf,核心功能包括:

vhost __defaultVhost__ {
    hls { enabled on; hls_path ./objs/nginx/html; hls_fragment 4; hls_window 20; }
    http_remux { enabled on; mount [vhost]/[app]/[stream].flv; }
    rtc { enabled on; rtmp_to_rtc off; rtc_to_rtmp off; }
    play { gop_cache_max_frames 2500; }

    # DVR 配置
    dvr {
        enabled on;
        dvr_apply all;
        dvr_plan segment;
        dvr_duration 1800;
        dvr_path /data/srs-record/[app]/[stream]/[2006]-[01]-[02]_[15]-[04]-[05].flv;
    }
}

DVR 录成 FLV 比 MP4 稳定,直播时间长文件不会损坏。后续再批量转 MP4。

创建录像目录:

mkdir -p /data/srs-record
chmod 755 /data/srs-record

4. 文件描述符 & 系统限制

SRS 默认 max_connections 1000,Linux 默认文件描述符 1024 不够。测试会报:

max_connections=1000, system limit to 1024, please run: ulimit -HSn 10000

临时解决:

ulimit -HSn 10000
./objs/srs -t -c conf/srs.conf

长期解决:

  1. 修改 systemd 服务 LimitNOFILE=10000

  2. 或修改 /etc/security/limits.conf


5. systemd 管理

写一个完整 systemd 服务文件:

cat > /etc/systemd/system/srs.service <<'EOF'
[Unit]
Description=SRS Streaming Server
After=network.target

[Service]
Type=simple
WorkingDirectory=/opt/srs/trunk
ExecStart=/opt/srs/trunk/objs/srs -c /opt/srs/trunk/conf/srs.conf
ExecStop=/bin/kill -TERM $MAINPID
Restart=always
RestartSec=3
LimitNOFILE=10000
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
EOF

启用服务:

systemctl daemon-reload
systemctl start srs
systemctl enable srs
systemctl status srs
journalctl -u srs -f

6. 推流测试

OBS 设置:

服务器:rtmp://服务器IP/live
串流密钥:test

播放:


7. 注意点和经验

  1. DVR 文件切片:segment 比 session 稳,长直播不会一个文件太大。

  2. 文件描述符ulimit + systemd LimitNOFILE 必须比 max_connections 大。

  3. Perl 模块:CentOS 默认最小 Perl 不够用,编译 OpenSSL 必须装全。

  4. 端口:1935/8080/1985/8000 都要放行。

  5. 后续扩展:先跑单机推流,确认 OBS → SRS → HLS/FLV 再考虑多节点分发或直接推到斗鱼/B站/虎牙。


总结

整个部署就是:

跑起来后可以同时支持 多端直播、录像、HLS/FLV 播放、WebRTC。稳定、可控,而且开机自动启动。


已有10位网友发表了看法:

发表评论

必填

选填

选填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。