准备篇
CentOS 7.x默认使用的是firewall作为防火墙,这里改为iptables防火墙。
1、关闭firewall:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
systemctl mask firewalld
systemctl stop firewalld
yum remove firewalld
2、安装iptables防火墙
RTMP基于TCP, 默认使用端口1935,再开放81端口
yum install iptables-services #安装
vi /etc/sysconfig/iptables #编辑防火墙配置文件
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 81 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 1935 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
:wq! #保存退出
systemctl restart iptables.service #最后重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动
/usr/libexec/iptables/iptables.init restart #重启防火墙
3、关闭SELINUX
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效
4、软件包下载
4.1、nginx-rtmp-module #nginx流媒体插件
https://github.com/arut/nginx-rtmp-module
https://github.com/arut/nginx-rtmp-module/archive/master.zip
https://github.com/arut/nginx-rtmp-module/archive/refs/tags/v1.2.2.tar.gz
4.2、ngx_cache_purge
http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
4.3、openssl
https://www.openssl.org/source/openssl-1.1.1w.tar.gz
4.4、pcre
https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz/download
4.5、zlib
https://www.zlib.net/zlib-1.3.tar.gz
4.6、nginx
https://nginx.org/download/nginx-1.24.0.tar.gz
5、创建安装路径
nginx安装包存放目录:mkdir -p /data/server/nginx/packages
nginx安装目录:mkdir -p /data/server/nginx
nginx扩展安装目录:mkdir -p /data/server/nginx/install
安装篇
1、安装编译工具包
yum install make gcc gcc-c++ perl zlib-devel
2、安装nginx
2.1安装pcre
cd /data/server/nginx/packages
mkdir -p /data/server/nginx/install/pcre
tar zxvf pcre-8.45.tar.gz
cd pcre-8.45
./configure --prefix=/data/server/nginx/install/pcre
make
make install
2.2安装zlib
cd /data/server/nginx/packages
mkdir -p /data/server/nginx/install/zlib
tar zxvf zlib-1.3.tar.gz
cd zlib-1.3
./configure --prefix=/data/server/nginx/install/zlib
make
make install
2.3安装openssl
cd /data/server/nginx/packages
mkdir -p /data/server/nginx/install/openssl
tar zxvf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w
./config -fPIC shared zlib --prefix=/data/server/nginx/install/openssl
make
make install
2.4安装nginx
nginx默认运行账号和组是Linux系统的内置账号和组nobody
groupadd www
useradd -g www www -s /bin/false
cd /data/server/nginx/packages
tar zxvf ngx_cache_purge-2.3.tar.gz #加载ngx_cache_purge模块
tar zxvf nginx-rtmp-module-1.2.2.tar.gz #加载nginx-rtmp-module模块
tar zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
./configure --prefix=/data/server/nginx --user=www --group=www --without-http_memcached_module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_realip_module --with-stream --with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/data/server/nginx/client --http-proxy-temp-path=/data/server/nginx/proxy --http-fastcgi-temp-path=/data/server/nginx/fcgi --http-uwsgi-temp-path=/data/server/nginx/uwsgi --with-openssl=/data/server/nginx/packages/openssl-1.1.1w --with-zlib=/data/server/nginx/packages/zlib-1.3 --with-pcre=/data/server/nginx/packages/pcre-8.45 --add-module=../ngx_cache_purge-2.3 --add-module=../nginx-rtmp-module-1.2.2
注意:--with-openssl=/data/server/nginx/packages/openssl-1.1.1w --with-zlib=/data/server/nginx/packages/zlib-1.3 --with-pcre=/data/server/nginx/packages/pcre-8.45指向的是源码包解压的路径,而不是安装的路径,否则会报错。
make
make install
/data/server/nginx/sbin/nginx #启动Nginx
#查看nginx版本和安装模块信息
/data/server/nginx/sbin/nginx -V
/data/server/nginx/sbin/nginx -s stop #关闭Nginx
2.5配置nginx启动脚本
vi /data/server/nginx/nginx.sh
#!/bin/bash
NGINX_PATH="/data/server/nginx/sbin/nginx"
PID_FILE="/data/server/nginx/logs/nginx.pid"
function start_nginx() {
if [ -f $PID_FILE ]; then
echo "Nginx is already running."
else
echo "Starting Nginx..."
$NGINX_PATH
echo "Nginx started."
fi
}
function stop_nginx() {
if [ -f $PID_FILE ]; then
echo "Stopping Nginx..."
$NGINX_PATH -s stop
echo "Nginx stopped."
else
echo "Nginx is not running."
fi
}
function restart_nginx() {
if [ -f $PID_FILE ]; then
echo "Restarting Nginx..."
$NGINX_PATH -s stop
sleep 1
$NGINX_PATH
echo "Nginx restarted."
else
echo "Nginx is not running. Starting it now..."
$NGINX_PATH
echo "Nginx started."
fi
}
function reload_nginx() {
if [ -f $PID_FILE ]; then
echo "Reloading Nginx configuration..."
$NGINX_PATH -s reload
echo "Nginx configuration reloaded."
else
echo "Nginx is not running. Cannot reload the configuration."
fi
}
function status_nginx() {
if [ -f $PID_FILE ]; then
echo "Nginx is running with PID $(cat $PID_FILE)."
else
echo "Nginx is stopped."
fi
}
case "$1" in
start)
start_nginx
;;
stop)
stop_nginx
;;
restart)
restart_nginx
;;
reload)
reload_nginx
;;
status)
status_nginx
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status}"
exit 1
;;
esac
:wq! #保存退出
#添加执行权限
chmod +x /data/server/nginx/nginx.sh
sh /data/server/nginx/nginx.sh start #启动
配置篇
1、创建目录
mkdir -p /data/video/vod #视频点播文件存放目录
mkdir -p /data/video/videolive #视频直播文件存放目录
mkdir -p /data/video/live
mkdir -p /data/video/tmp/hls
上传视频文件到相应目录
/data/video/vod/vod.mp4
/data/video/videolive/videolive.mp4
chown www.www -R /data/video/ #设置目录权限
2、配置nginx.conf文件,添加rtmp模块
#备份文件配置
cp /data/server/nginx/conf/nginx.conf /data/server/nginx/conf/nginx.conf.bak
vi /data/server/nginx/conf/nginx.conf #编辑,添加配置 注意:rtmp配置和http是平级的
#在最后一行的“}” 上面添加以下代码
server {
listen 81;
root /data/video/tmp/hls;
index index.html;
location / {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
expires -1;
}
}
#在最后一行的“}” 下面添加以下代码
#rtmp config
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
record off;
}
application videolive {
live on;
record off;
play /data/video/videolive/;
}
application vod {
play /data/video/vod/;
}
application hls {
live on;
hls on;
hls_path /data/video/tmp/hls;
hls_fragment 5s;
}
}
}
sh /data/server/nginx/nginx.sh restart #重启
测试篇
1、在同一网段的终端电脑上安装VLC播放器
下载地址:http://www.videolan.org/
安装完成后打开播放器-媒体-打开网络串流-网络,添加地址
rtmp://192.168.30.6:1935/vod/vod.mp4
rtmp://192.168.30.6:1935/videolive/videolive.mp4
即可播放视频
2、安装OBS Studio或者EV录屏软件,设置好直播画面
2.1添加rtmp直播源
rtmp://192.168.30.6:1935/live
在VLC播放器添加地址
rtmp://192.168.30.6:1935/live
就能观看直播了
2.2添加hls直播源
rtmp://192.168.30.6:1935/hls/mystream
在VLC播放器添加地址
http://192.168.30.6:81/mystream.m3u8
就能观看直播了
在移动端手机浏览器里面可以直接打开这个链接观看,手机浏览器内置了对HLS协议的支持。
Safari浏览器默认也可以直接打开m3u8格式的视频。
至此,Nginx通过nginx-rtmp-module模块搭建流媒体服务器实现直播完成。