-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
#查看默認防火墻狀態(tài)(關閉后顯示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 創(chuàng)建群組
groupadd nginx
1.2 創(chuàng)建不能登錄的用戶
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; #監(jiān)聽的端口
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目錄下創(chuàng)建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過程中,執(zhí)行./configure時救斑,報yasm/nasm not found or too old. Use --disable-yasm for a crippledbuild錯誤惯吕,分析闲勺、解決如下:
分析:yasm是匯編編譯器旬蟋,ffmpeg為了提高效率使用了匯編指令,如MMX和SSE等敬肚。所以系統(tǒng)中未安裝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