CentOS7.4 nginx+rtmp+ffmpeg

-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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末憎亚,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子弄慰,更是在濱河造成了極大的恐慌第美,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件陆爽,死亡現(xiàn)場離奇詭異什往,居然都是意外死亡,警方通過查閱死者的電腦和手機慌闭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門别威,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人驴剔,你說我怎么就攤上這事兔港。” “怎么了仔拟?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長飒赃。 經常有香客問我利花,道長,這世上最難降的妖魔是什么载佳? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任炒事,我火速辦了婚禮,結果婚禮上蔫慧,老公的妹妹穿的比我還像新娘挠乳。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布睡扬。 她就那樣靜靜地躺著盟蚣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪卖怜。 梳的紋絲不亂的頭發(fā)上屎开,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天,我揣著相機與錄音马靠,去河邊找鬼奄抽。 笑死,一個胖子當著我的面吹牛甩鳄,可吹牛的內容都是我干的逞度。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼妙啃,長吁一口氣:“原來是場噩夢啊……” “哼档泽!你這毒婦竟也來了?” 一聲冷哼從身側響起彬祖,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤茁瘦,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后储笑,有當地人在樹林里發(fā)現(xiàn)了一具尸體甜熔,經...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年突倍,在試婚紗的時候發(fā)現(xiàn)自己被綠了腔稀。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡羽历,死狀恐怖焊虏,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情秕磷,我是刑警寧澤诵闭,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站澎嚣,受9級特大地震影響疏尿,放射性物質發(fā)生泄漏。R本人自食惡果不足惜易桃,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一褥琐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧晤郑,春花似錦敌呈、人聲如沸贸宏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽吭练。三九已至,卻和暖如春褐鸥,著一層夾襖步出監(jiān)牢的瞬間线脚,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工叫榕, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留浑侥,地道東北人。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓晰绎,卻偏偏與公主長得像寓落,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子荞下,可洞房花燭夜當晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內容