服務(wù)器OS:CentOS release 6.6
LNMP腳本版本:1.3
本地OS:OSX 10.11.6
1、Nginx環(huán)境搭建(基于lnmp環(huán)境)
lnmp環(huán)境一鍵安裝腳本
按照??教程安裝!
//下載并安裝lnmp環(huán)境
wget -c http://soft.vpser.net/lnmp/lnmp1.3.tar.gz && tar zxf lnmp1.3.tar.gz && cd lnmp1.3 && ./install.sh lnmp
安裝完成后訪問服務(wù)器地址會(huì)出現(xiàn)如下界面
PS:安裝時(shí)生成的解壓文件夾lnmp1.3先別刪除!!!
2想幻、安裝Nginx的擴(kuò)展rtmp模塊
//下載擴(kuò)展包
wget https://github.com/arut/nginx-rtmp-module/archive/v1.1.10.tar.gz
//解壓擴(kuò)展包
tar -xzvf v1.1.10.tar.gz
//為nginx創(chuàng)建擴(kuò)展模塊目錄
mkdir /usr/local/nginx/extend_module
//將解壓后的nginx-rtmp-module目錄移動(dòng)到nginx擴(kuò)展模塊目錄下
mv nginx-rtmp-module-1.1.10/ /usr/local/nginx/extend_module/nginx-rtmp-module
nginx -V //查看nginx配置參數(shù)
復(fù)制configure arguments:后的所有參數(shù)!
例如:--user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module
關(guān)閉nginx蒸痹、php-fpm服務(wù)
//關(guān)閉nginx
service nginx stop
//關(guān)閉php-fpm
service php-fpm stop
進(jìn)入安裝時(shí)生成的文件夾:lnmp1.3/lnmp1.3/src
//解壓nginx源碼包
tar -xzvf nginx-1.10.0.tar.gz
//進(jìn)入nginx源碼目錄
cd nginx-1.10.0
//安裝rtmp擴(kuò)展模塊
./configure 剛才復(fù)制的nginx configure參數(shù) --add-module=rtmp擴(kuò)展包目錄
如:
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --add-module=/usr/local/nginx/extend_module/nginx-rtmp-module
//編譯
make
//安裝
make install
//重啟nginx
service nginx start
service php-fpm start
//查看安裝狀態(tài)
nginx -V
!上圖如果出現(xiàn)nginx-rtmp-module說(shuō)明安裝成功!
3排霉、配置rtmp
進(jìn)入cd /usr/local/nginx/conf 目錄
//編輯配置
vim nginx.conf
#尾部加入
rtmp {
server {
listen 1935; #監(jiān)聽的端口
chunk_size 4000;
application hls { #rtmp推流請(qǐng)求路徑
live on;
record off;
}
}
}
參數(shù)說(shuō)明:
rtmp是協(xié)議名稱
server 說(shuō)明內(nèi)部中是服務(wù)器相關(guān)配置
listen 監(jiān)聽的端口號(hào), rtmp協(xié)議的默認(rèn)端口號(hào)是1935
application 訪問的應(yīng)用路徑是 hls
live on; 開啟實(shí)時(shí)
record off; 不記錄數(shù)據(jù)
保存修改后:nginx -s reload //重載下nginx配置
4成畦、服務(wù)器開放1935端口
(如服務(wù)器防火墻已關(guān)閉跳過(guò)此步)
//開放1935端口
/sbin/iptables -I INPUT -p tcp --dport 1935 -j ACCEPT
//保存配置
/etc/rc.d/init.d/iptables save
//重啟服務(wù)
/etc/rc.d/init.d/iptables restart
//查看端口開放狀態(tài)
/etc/init.d/iptables status
5取试、本地安裝推流工具(ffmepg)及rtmp播放器(VLC)測(cè)試
ffmepg安裝:brew install ffmpeg
VLC播放器下載:VLC
//ffmepg推流(本地準(zhǔn)備一個(gè)視頻文件)
ffmpeg -re -i 本地視頻文件的絕對(duì)路徑(如/Users/flycorn/Downloads/demo-hls.mp4) -vcodec copy -f flv rtmp://服務(wù)器IP:1935/hls/test
// 如:
ffmpeg -re -i /Users/flycorn/Downloads/demo-hls.mp4 -vcodec copy -f flv rtmp://服務(wù)器IP:1935/hls/test
PS:
如推流時(shí)出現(xiàn)
Connection to tcp://服務(wù)器IP:1935 failed: Connection refused
請(qǐng)先執(zhí)行:
nginx -s reload
如不成功執(zhí)行:
service nginx reload
推流界面如下:
開始推流后这揣,打開VLC播放器驗(yàn)證結(jié)果
5悔常、配置HLS
進(jìn)入/usr/local/nginx/conf 目錄
//編輯配置
vim nginx.conf
//修改rtmp配置
rtmp {
server {
listen 1935; #監(jiān)聽的端口
chunk_size 4000;
application hls { #rtmp推流請(qǐng)求路徑
live on;
hls on;
hls_path /home/hls/test; #視頻流文件目錄(自己創(chuàng)建)
hls_fragment 3s;
}
}
}
//修改server的配置
server
{
listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
server_name www.lnmp.org;
index index.html index.htm index.php;
root /home/wwwroot/default;
#error_page 404 /404.html;
include enable-php.conf;
#加入hls支持
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
#或 application/x-mpegURL
video/mp2t ts;
}
alias /home/hls/test/; #視頻流文件目錄(自己創(chuàng)建)
expires -1;
add_header Cache-Control no-cache;
}
#end...
#以下代碼省略.....
}
進(jìn)入/home目錄并創(chuàng)建 hls及其子目錄test
配置改完后執(zhí)行nginx -s reload
在/home/wwwroot/default目錄下創(chuàng)建test.html文件
//test.html
<html>
<body>
<video autoplay webkit-playsinline controls>
<source src="http://服務(wù)器IP/hls/test.m3u8" type="application/vnd.apple.mpegurl" />
<p class="warning">Your browser does not support HTML5 video.</p>
</video>
<video controls>
<source src="http://服務(wù)器IP/hls/test.m3u8" type="application/x-mpegURL">
</video>
</body>
</html>
使用ffmpeg進(jìn)行推流:
ffmpeg -re -i /Users/flycorn/Downloads/demo-hls.mp4 -vcodec copy -f flv rtmp://服務(wù)器IP:1935/hls/test
此時(shí)服務(wù)器上的/home/hls/test/目錄下會(huì)生成許多ts文件及test.m3u8文件!
使用Safari瀏覽器訪問 http://服務(wù)器IP/test.html
如直播沒有聲音给赞,請(qǐng)參考ffmpeg沒有聲音!~
PS:瀏覽器對(duì)hls的支持
也可使用第三方插件實(shí)現(xiàn)對(duì)hls的支持机打!如videojs-contrib-hls