搭建基於rtmp協議的推流服務器。
環境Linux centos 7.6 + Nginx
1.安裝Nginx
安裝Nginx依賴庫:
#安裝Nginx的編譯環境gcc
yum install gcc-c++
#nginx的http模塊使用pcre解析正則表達式所以安裝perl兼容的正則表達式庫
yum install -y pcre pcre-devel
#nginx使用zlib對http包的內容進行gzip
yum install -y zlib zlib-devel
#nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),如果使用了https,需要安裝OpenSSL庫
yum install -y openssl openssl-devel
下載Nginx,下載地址: http://nginx.org/en/download.html 選擇下載的版本,我這裏選擇 nginx-1.15.3,進入到下載路徑,輸入下載命令:
cd /usr/local/
wget http://nginx.org/download/nginx-1.15.3.tar.gz
tar -zxvf nginx-1.15.3.tar.gz
rm nginx-1.15.3.tar.gz
mv nginx-1.15.3 nginx
cd nginx
./configure --prefix=/usr/local/nginx
make
make install
#遇到make錯誤 /usr/local/nginx 路徑不存在不管,繼續 make install
#添加Nginx環境變量,可以在命令行直接輸入Nginx命令
vim /etc/profile
#在最後添加Nginx的路徑
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
#重新編譯環境變量
source /etc/profile
#啓動nginx
cd sbin
./nginx
#我這邊啓動時報錯:
nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
2018/09/25 13:59:56 [emerg] 15555#0: open() "/usr/local/nginx/logs/access.log" failed (2: No such file or directory)
#需要手動創建logs文件夾
mkdir /usr/local/nginx/logs
#再啓動
./nginx
#重啓命令:
nginx -s reload
Nginx安裝完成,測試:打開瀏覽器輸入IP地址顯示歡迎界面則安裝啓動成功,如果顯示訪問超時,則可能是防火牆沒有打開80端口。打開80端口:
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
2.安裝Nginx的rtmp拓展
nginx的rtmp拓展包github地址:https://github.com/arut/nginx-rtmp-module,可以使用git clone下拉或者直接下載,我這邊下載解壓放到:/opt/module/下。Nginx安裝rtmp拓展:
cd /usr/local/nginx
./configure --add-module=/opt/module/nginx-rtmp-module
make
make install
配置Nginx的rtmp服務站點:
vim /usr/local/nginx/conf/nginx.conf
# 在文件底部添加下面內容:
rtmp {
server {
listen 1935; #監聽的端口
chunk_size 4000;
application tv_file {
live on; #開啓實時
hls on; #開啓hls
hls_path /usr/local/nginx/html/tv_file; #rtmp推流請求路徑,文件存放路徑
hls_fragment 5s; #每個TS文件包含5秒的視頻內容
}
}
}
重啓Nginx:
nginx -s reload
測試:windows打開doc,輸入:
telnet 你的ip地址 1935
如果失敗,則開啓1935端口:
iptables -I INPUT -p tcp --dport 1935 -j ACCEPT
3.推拉流測試
推流。下載OBS Studio,官網下載太慢了,其他下載地址:https://pc.qq.com/detail/4/detail_23604.html
安裝完成,打開軟件,在來源版塊新建媒體源,本地文件選擇一個視頻視頻,勾選循環,去除勾選播放結束隱藏源,在控件版塊點擊設置,左邊的導航選擇流,然後流類型選擇自定義流媒體服務器,url輸入rtmp://你的IP:1935/tv_file,流名稱隨便設置一個,這裏設置zm:
設置完成點擊推流。
在服務器就看到m3u8文件的生成,推流成功。
拉流。測試拉流的網站:https://www.wowza.com/testplayers
設置如下: