本次使用Ubuntu 22.04 LTS系统的轻量云演示,所有软件均提供可直接复制的安装命令,其他Linux发行版可替换对应包管理器命令。
首先执行系统更新,避免依赖冲突,然后安装编译Nginx RTMP模块所需的工具库。
注意:所有命令需用root用户执行,或在开头加sudo
```bash 更新包索引 apt update -y 升级已安装的包 apt upgrade -y 安装编译依赖 apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev ```Nginx官方版不自带RTMP推流模块,需手动下载并编译添加nginx-http-flv-module(同时支持RTMP和HTTP-FLV拉流,兼容性比纯nginx-rtmp-module更好)。
配置路径固定为/usr/local/nginx,方便后续维护;添加ssl模块支持后续HTTPS访问。
```bash cd nginx-1.25.3 ./configure --prefix=/usr/local/nginx \ --with-http_ssl_module \ --add-module=../nginx-http-flv-module-1.2.11 ```使用vim或nano编辑,这里用nano(新手更友好),清空原内容后粘贴以下完整可直接复制的配置:
```bash nano /usr/local/nginx/conf/nginx.conf ``` ```nginx user www-data; worker_processes auto; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; HTTP-FLV拉流监听端口(可自定义) server { listen 80; server_name _; 拉流地址/路径(可自定义,但要和rtmp的路径对应) location /live { flv_live on; chunked_transfer_encoding on; add_header 'Access-Control-Allow-Origin' ''; add_header 'Access-Control-Allow-Credentials' 'true'; } 禁止访问根目录和配置文件 location / { deny all; } location ~ /\. { deny all; } } } RTMP推流核心配置 rtmp { server { RTMP默认推流端口(1935,可自定义,但要在云服务器安全组开放) listen 1935; chunk_size 4096; 推流应用名(可自定义,这里设为live) application live { live on; record off; 关闭录制,如需开启设为on并配置record_path 推流鉴权(可选但强烈建议开启,防止盗推) auth_key_push $arg_secret; auth_key_secret your_secret_key_123456; } } } ```编辑完成后按Ctrl+O保存,再按Enter确认文件名,最后按Ctrl+X退出。

创建systemd服务文件,设置开机自动启动:
```bash nano /etc/systemd/system/nginx.service ``` ```service [Unit] Description=nginx - high performance web server After=network.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target ```保存退出后执行以下命令:
```bash 重载systemd服务配置 systemctl daemon-reload 设置开机自启 systemctl enable nginx 启动Nginx systemctl start nginx 查看Nginx状态 systemctl status nginx ```如果状态显示active (running),说明Nginx配置成功。
这一步极易卡壳,务必操作!
登录轻量云/云服务器控制台,找到对应的实例,进入防火墙/安全组页面,添加以下入站规则:
编辑Nginx配置文件,取消注释鉴权相关的两行,替换your_secret_key_123456为自己的强密码:
```bash nano /usr/local/nginx/conf/nginx.conf ```修改后重载Nginx:
```bash systemctl reload nginx ```之后OBS推流时的服务器地址要改为:rtmp://你的服务器公网IP/live?secret=your_secret_key_123456,串流密钥不变。
上一篇: 从零到一,手把手教你搭建服务器推理服务
易频IT社区是综合性互联网IT技术门户网站,专注分享网络技术、服务器运维、网络安全、编程开发、系统架构、云计算、大数据等行业干货,实时更新IT行业资讯、零基础教程、实战案例,为IT从业者、技术爱好者提供专业的学习交流平台。
Copyright © 2021-2026 易频IT社区. All Rights Reserved. 备案号:闽ICP备2023013482号 网站地图