以下是在 CentOS 7.6 上安装和配置 Nginx with RTMP module 的详细步骤:
1. 安装 Nginx with RTMP 模块
1.1 安装必要的依赖和工具
首先,更新系统并安装必要的依赖包:
sudo yum update -y
sudo yum install -y epel-release
sudo yum install -y gcc pcre-devel zlib-devel make unzip openssl-devel
1.2 下载并解压 Nginx 和 RTMP 模块
下载并解压 Nginx 和 RTMP 模块源码:
cd /usr/local/src
sudo wget http://nginx.org/download/nginx-1.18.0.tar.gz
sudo wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz
sudo tar -zxvf nginx-1.18.0.tar.gz
sudo tar -zxvf v1.2.1.tar.gz
1.3 编译并安装 Nginx with RTMP 模块
编译并安装 Nginx with RTMP 模块:
cd nginx-1.18.0
sudo ./configure --add-module=../nginx-rtmp-module-1.2.1 --with-http_ssl_module
sudo make
sudo make install
2. 配置 Nginx
编辑 Nginx 配置文件(通常位于 /usr/local/nginx/conf/nginx.conf
):
sudo nano /usr/local/nginx/conf/nginx.conf
添加以下内容:
worker_processes 1;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 8080;server_name localhost;location / {root html;index index.html index.htm;}location /hls {types {application/vnd.apple.mpegurl m3u8;video/mp2t ts;}root /tmp;add_header Cache-Control no-cache;}}
}rtmp {server {listen 1935;chunk_size 4096;application live {live on;record off;}application hls {live on;hls on;hls_path /tmp/hls;hls_fragment 3;}}
}
3. 启动 Nginx
启动 Nginx:
sudo /usr/local/nginx/sbin/nginx
4. 推流到服务器
使用 FFmpeg 推流到 Nginx RTMP 服务器:
ffmpeg -re -i input.mp4 -c:v libx264 -c:a aac -f flv rtmp://localhost/live/stream
5. 播放流媒体
使用 VLC 或其他支持 RTMP/HLS 协议的播放器播放流媒体。
使用 VLC 播放:
打开 VLC,选择 “媒体” -> “打开网络串流”,输入 RTMP 地址:
rtmp://localhost/live/stream
或者输入 HLS 地址:
http://localhost:8080/hls/stream.m3u8
总结
通过以上步骤,可以在 CentOS 7.6 上搭建一个简单的流媒体服务器,实现音视频内容的实时传输。根据实际需求,可以选择不同的流媒体服务器软件,并进行相应的配置和优化。以下是在 CentOS 7.6 上安装和配置 Nginx with RTMP module 的详细步骤:
1. 安装 Nginx with RTMP 模块
1.1 安装必要的依赖和工具
首先,更新系统并安装必要的依赖包:
sudo yum update -y
sudo yum install -y epel-release
sudo yum install -y gcc pcre-devel zlib-devel make unzip openssl-devel
1.2 下载并解压 Nginx 和 RTMP 模块
下载并解压 Nginx 和 RTMP 模块源码:
cd /usr/local/src
sudo wget http://nginx.org/download/nginx-1.18.0.tar.gz
sudo wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz
sudo tar -zxvf nginx-1.18.0.tar.gz
sudo tar -zxvf v1.2.1.tar.gz
1.3 编译并安装 Nginx with RTMP 模块
编译并安装 Nginx with RTMP 模块:
cd nginx-1.18.0
sudo ./configure --add-module=../nginx-rtmp-module-1.2.1 --with-http_ssl_module
sudo make
sudo make install
2. 配置 Nginx
编辑 Nginx 配置文件(通常位于 /usr/local/nginx/conf/nginx.conf
):
sudo nano /usr/local/nginx/conf/nginx.conf
添加以下内容:
worker_processes 1;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 8080;server_name localhost;location / {root html;index index.html index.htm;}location /hls {types {application/vnd.apple.mpegurl m3u8;video/mp2t ts;}root /tmp;add_header Cache-Control no-cache;}}
}rtmp {server {listen 1935;chunk_size 4096;application live {live on;record off;}application hls {live on;hls on;hls_path /tmp/hls;hls_fragment 3;}}
}
3. 启动 Nginx
启动 Nginx:
sudo /usr/local/nginx/sbin/nginx
4. 推流到服务器
使用 FFmpeg 推流到 Nginx RTMP 服务器:
ffmpeg -re -i input.mp4 -c:v libx264 -c:a aac -f flv rtmp://localhost/live/stream
5. 播放流媒体
使用 VLC 或其他支持 RTMP/HLS 协议的播放器播放流媒体。
使用 VLC 播放:
打开 VLC,选择 “媒体” -> “打开网络串流”,输入 RTMP 地址:
rtmp://localhost/live/stream
或者输入 HLS 地址:
http://localhost:8080/hls/stream.m3u8
总结
通过以上步骤,可以在 CentOS 7.6 上搭建一个简单的流媒体服务器,实现音视频内容的实时传输。根据实际需求,可以选择不同的流媒体服务器软件,并进行相应的配置和优化。