-1. 准备工作
-1.1 selinux
# 查看
getenforce
# 临时关闭
# 设置SELinux 成为permissive模式
# setenforce 1 设置SELinux 成为enforcing模式
setenforce 0
# 永久关闭
vi /etc/selinux/config
-1.2 firewalld
centos 7 默认开启的是firewalld防火墙
#停止firewall
systemctl stop firewalld.service
#禁止firewall开机启动
systemctl disable firewalld.service
#查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
firewall-cmd --state
0. yum 源配置(国内服务器)
//must update yum after os installed
yum update yum
//install wget
yum install -y wget
//备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
//CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
//CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
//CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
// 是上述设置生效
yum makecache
1 准备工作
1.1 创建群组
groupadd nginx
1.2 创建不能登录的用户
useradd -s /sbin/nologin -g nigix -M nginx
2 nginx
2.1 安装基本的编译工具
yum install gc gcc gcc-c++ pcre-devel zlib-devel openssl-devel
2.2 安装git并下载rtmp模块
yum install -y git
# 保存目录
cd /usr/local/src
git clone https://github.com/arut/nginx-rtmp-module.git
2.3 下载nginx
wget http://nginx.org/download/nginx-1.15.2.tar.gz
tar zxvf nginx-1.15.2.tar.gz
2.4 编译nginx
cd nginx-1.15.2
./configure --user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--add-module=/usr/local/src/nginx-rtmp-module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module
make && make install
2.5 设置启动脚本
vi /etc/init.d/nginx
# 填入一下内容
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|
restart|configtest}"
RETVAL=1
esac
exit $RETVAL
# 更改权限
chmod 755 /etc/init.d/nginx
# 添加到启动项配置
chkconfig --add nginx
# 开机启动
chkconfig nginx on
# 启动命令
service nginx start
#
service nginx stop
service nginx restart
2.6 rtmp 配置
cd /usr/local/nginx/conf
vi nginx.conf
# 填入如下配置
# nginx rtmp配置 跟http{}同级
rtmp {
server {
listen 1935; #监听的端口
chunk_size 4000;
application rtmplive {
live on;
max_connections 1024;
}
application hls { #rtmp推流请求路径
# enable live streaming
live on;
# record first 200M of stream
record all;
record_path /home/live_record;
record_max_size 200M;
hls on;
hls_path /home/hls;
hls_fragment 1s;
hls_playlist_length 5;
allow play all;
#on_publish 'http://when start publish live call this url';
#on_done 'http://when live stop call this url';
}
}
}
在mine.types 中添加以下内容:
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
application/x-mpegURL m3u8;
在conf目录下创建conf.d 文件夹,用于存放配置文件
# hls.conf 内容
server {
listen 8080;
server_name 10.142.153.131;
# This URL provides RTMP statistics in XML
location /stat {
rtmp_stat all;
# Use this stylesheet to view XML as web page
# in browser
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# XML stylesheet to view RTMP stats.
# Copy stat.xsl wherever you want
# and put the full directory path here
root /usr/local/nginx/html/;
}
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /home;
add_header Cache-Control no-cache;
# 添加这行用于允许跨域
add_header 'Access-Control-Allow-Origin' '*';
}
}
3 ffmpeg
3.0 yasm编译器安装
安装ffmpeg过程中,执行./configure时,报yasm/nasm not found or too old. Use --disable-yasm for a crippledbuild错误,分析、解决如下:
分析:yasm是汇编编译器,ffmpeg为了提高效率使用了汇编指令,如MMX和SSE等。所以系统中未安装yasm时,就会报上面错误。
# 下载
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
# 解压
tar zxvf yasm-1.3.0.tar.gz
#
cd yasm-1.3.0
#
./configure
#
make && make install
3.1 下载
wget https://ffmpeg.org/releases/ffmpeg-4.0.2.tar.bz2
3.2 编译安装
# 安装 bzip2
yum install -y bzip2
# 解压
tar -xjvf ffmpeg-4.0.2.tar.bz2
cd ffmpeg-4.0.2
# 编译设置
./configure --enable-shared --prefix=/usr/local/ffmpeg
#
make && make install
4 OBS 推流测试
QA
1. nginx 404
# location 中添加下面一行允许跨域
# https://stackoverflow.com/questions/44664270/nginx-hls-stream-not-working
add_header 'Access-Control-Allow-Origin' '*';
2.m3u8文件存在,但是无法播放直播
https://github.com/arut/nginx-rtmp-module/issues/737
# rtmp 直播无法用localhost解析
server_name 192.168.1.111;# 填写实际ip