1. 安装 Nginx 依赖软件
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
2. 安装nginx与rtmp模块
a)获取nginx安装包
cd /usr/local/
wget http://nginx.org/download/nginx-1.19.5.tar.gz
tar zxvf nginx-1.19.5.tar.gz
b)创建nginx安装目录/usr/local/nginx与模块目录/usr/local/nginx-1.19.5/module、视频数据存放位置/tmp/hls
mkdir -p /usr/local/nginx
mkdir -p /tmp/hls
mkdir -p /usr/local/nginx-1.19.5/module
c)下载nginx-rtmp-module,下载后的文件夹名叫nginx-rtmp-module(没有安装git的请安装,执行语句的时候没有git会提示你,yum install git 安装之
cd /usr/local/nginx-1.19.5/module
git clone https://github.com/arut/nginx-rtmp-module.git
d)进入解压后的nginx目录中,指定nginx安装目录并且指定了安装的模块
cd /usr/local/nginx-1.19.5/
./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx-1.19.5/module/nginx-rtmp-module
make -j3 && make install
3. 配置nginx与rtmp模块 ,先备份原始的配置文件ngnix.conf
mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf ,写入如下内容
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 9909;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
rtmp {
server {
listen 1935;
max_streams 512;
application live {
live on;
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
}
}
未在防火墙上开放9909和1935端口的先开放这两个端口
5.启动nginx
/usr/local/nginx/sbin/nginx
此时如果没有问题,那么你就可以访问http://x.x.x.x:9909了,会出现welcome界面。x.x.x.x是该安装nginx的机器的ip
6.启动推流
a)ffmpeg推流
ffmpeg -re -i test.mp4 -vcodec libx264 -acodec aac -f flv rtmp://x.x.x.x:1935/live/0001
- acc:RTMP的音频格式
- flv: RTMP的视频格式
- 命令中的0001可以随意命令,拉取流时一样就可以
b)obs推流也可以,自行研究
7.拉取流,可以使用VLC播放器,在菜单 “媒体”-->“打开网络串流”,输入rtmp://x.x.x.x/live/0001,即可以播放了