linux实现推拉流服务器-安装 Nginx-rtmp-module 模块
步骤 1: 安装依赖
首先,你需要安装一些必要的依赖库,这些库将帮助你编译 Nginx 和其模块。
sudo yum install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel wget
步骤 2: 下载 Nginx 和 Nginx-rtmp-module
你可以从 Nginx 官方网站下载最新的稳定版 Nginx。同时,Nginx-rtmp-module 也可以从其 GitHub 仓库获取。
cd /optsudo wget http://nginx.org/download/nginx-1.20.1.tar.gz # 替换为最新版本sudo tar -zxvf nginx-1.20.1.tar.gzcd nginx-1.20.1/ sudo git clone https://github.com/arut/nginx-rtmp-module.git
步骤 3: 编译 Nginx 并加入 RTMP 模块
在编译 Nginx 时,你需要指定 --add-module 选项来包含 RTMP 模块。
./configure --add-module=../nginx-rtmp-module --with-http_ssl_module
./configure --prefix=/opt/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules \ --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx \ --with-http_ssl_module --with-mail --with-mail_ssl_module --with-file-aio \ --with-http_v2_module --add-module=../nginx-rtmp-module makesudo make install
步骤 4: 配置 Nginx 以使用 RTMP 模块
创建一个新的 Nginx 配置文件,例如 /etc/nginx/conf.d/rtmp.conf,并添加以下配置:
rtmp {
server {
listen 1935; # RTMP standard port
chunk_size 4096;
application live {
live on;
record off; # 是否录制直播流到磁盘
}
}
}
步骤 5: 启动 Nginx 服务
sudo /usr/sbin/nginx -t # 检查配置文件是否有误sudo /usr/sbin/nginx # 启动 Nginx 服务
步骤 6: 验证 RTMP 服务
你可以使用 FFmpeg 来测试 RTMP 服务是否工作正常。例如,使用以下命令发送一个测试流:
ffmpeg -re -i your_input_file.mp4 -c:v libx264 -preset superfast -maxrate 3000k -bufsize 6000k -pix_fmt yuv420p -f flv rtmp://localhost/live/stream_key
替换 your_input_file.mp4 和 stream_key 为你的实际输入文件和流密钥。然后,你可以在浏览器中使用 RTMP 流 URL(如 rtmp://localhost/live/stream_key)来查看流媒体。