php 直播服务器搭建,基于Nginx搭建RTMP/HLS视频直播服务器
1、Nginx環(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èn)服務(wù)器地址會(huì)出現(xiàn)如下界面
lnmp
PS:安裝時(shí)生成的解壓文件夾lnmp1.3先別刪除!!!
2、安裝Nginx的擴(kuò)展rtmp模塊github:https://github.com/arut/nginx-rtmp-module
//下載擴(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ù)
nginx
復(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
nginx
!上圖如果出現(xiàn)nginx-rtmp-module說(shuō)明安裝成功!
3、配置rtmp進(jìn)入cd /usr/local/nginx/conf 目錄
//編輯配置
vim nginx.conf
#尾部加入
rtmp {
server {
listen 1935; #監(jiān)聽(tīng)的端口
chunk_size 4000;
application hls { #rtmp推流請(qǐng)求路徑
live on;
record off;
}
}
}
參數(shù)說(shuō)明:
rtmp是協(xié)議名稱(chēng)
server 說(shuō)明內(nèi)部中是服務(wù)器相關(guān)配置
listen 監(jiān)聽(tīng)的端口號(hào),rtmp協(xié)議的默認(rèn)端口號(hào)是1935
application 訪問(wèn)的應(yīng)用路徑是 hls
live on; 開(kāi)啟實(shí)時(shí)
record off; 不記錄數(shù)據(jù)
保存修改后:nginx -s reload //重載下nginx配置
4、服務(wù)器開(kāi)放1935端口(如服務(wù)器防火墻已關(guān)閉跳過(guò)此步)
//開(kāi)放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
//查看端口開(kāi)放狀態(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
推流界面如下:
推流中
開(kāi)始推流后,打開(kāi)VLC播放器驗(yàn)證結(jié)果
VLC
Open media
直播視頻中
5、配置HLS進(jìn)入/usr/local/nginx/conf 目錄
//編輯配置
vim nginx.conf
//修改rtmp配置
rtmp {
server {
listen 1935; #監(jiān)聽(tīng)的端口
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
Your browser does not support HTML5 video.
使用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瀏覽器訪問(wèn) http://服務(wù)器IP/test.html
直播中
如直播沒(méi)有聲音,請(qǐng)參考ffmpeg沒(méi)有聲音!~
PS:瀏覽器對(duì)hls的支持
hls瀏覽器支持情況
也可使用第三方插件實(shí)現(xiàn)對(duì)hls的支持!如videojs-contrib-hls
總結(jié)
以上是生活随笔為你收集整理的php 直播服务器搭建,基于Nginx搭建RTMP/HLS视频直播服务器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: silverlight5 ToolKit
- 下一篇: 通讯录选择器A-Z