環(huán)境準(zhǔn)備
下載Nginx
wget http://nginx.org/download/nginx-1.16.0.tar.gz
解壓Nginx
tar -zxvf nginx-1.16.0.tar.gz
下載Nginx RTMP模塊
wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz
解壓Nginx RTMP模塊
tar -zxvf v1.2.1.tar.gz
編譯安裝
./configure --help > nginx_configure_help.txt
將help輸出text方便查看進(jìn)入Nginx解壓目錄,边琉,執(zhí)行configure:
./configure --prefix=./bin --add-module=../nginx-rtmp-module-1.2.1
nginx 中 gzip 模塊需要 zlib 庫,rewrite模塊需要 pcre 庫记劝,ssl 功能需要 openssl 庫变姨。所以如果服務(wù)器未安裝這三個依賴庫的話會報錯,需要先安裝這三個依賴庫
執(zhí)行完生成 Makefile 文件
編譯:
make install
編譯完生成bin目錄
$ ls
auto bin CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
$ cd bin/
$ ls
conf html logs sbin
conf:配置相關(guān)
html:歡迎頁面厌丑、錯誤頁面
logs:日志存放區(qū)
sbin:可執(zhí)行文件存放區(qū)
修改配置
Nginx默認(rèn)不支持rtmp定欧,需要修改配置文件。
如何修改怒竿,可以參考:nginx-rtmp-module-1.2.1/test/nginx.conf
進(jìn)入bin/conf目錄忧额,找到 nginx.conf 文件。
worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
#rtmp標(biāo)簽
rtmp {
#服務(wù)標(biāo)簽愧口,一個rtmp服務(wù)中可以有多個server標(biāo)簽睦番,每個標(biāo)簽可監(jiān)聽不同端口號
server {
#注意端口占用,1935為默認(rèn)端口
listen 1935;
#應(yīng)用標(biāo)簽耍属,一個服務(wù)標(biāo)簽中可以有多個應(yīng)用標(biāo)簽
application myapp {
live on;
#丟棄閑置5s的連接
drop_idle_publisher 5s;
}
}
}
http {
server {
#注意端口占用
listen 8080;
#數(shù)據(jù)統(tǒng)計模塊托嚣,將流媒體的狀態(tài)記錄到 stat.xsl 中
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
#將stat.xsl 訪問目錄指定到nginx-rtmp-module中
location /stat.xsl {
#注意目錄
root /root/he/nginx-rtmp-module-1.2.1/;
}
#控制器模塊,可錄制直播視頻厚骗、踢出推流/拉流用戶示启、重定向推流/拉流用戶
location /control {
rtmp_control all;
}
location /rtmp-publisher {
#注意目錄
root /root/he/nginx-rtmp-module-1.2.1/test;
}
location / {
#注意目錄
root /root/he/nginx-rtmp-module-1.2.1/test/www;
}
}
}
啟動服務(wù)
進(jìn)入sbin目錄嘗試執(zhí)行nginx:
$ cd sbin/
$ ./nginx -t
nginx: [alert] could not open error log file: open() "./bin/logs/error.log" failed (2: No such file or directory)
-t 表示測試。
仔細(xì)看錯誤說明领舰,"./bin/logs/error.log" 文件找不到夫嗓?也就是當(dāng)前目錄下找不到 bin/logs/error.log。我們執(zhí)行的當(dāng)前目錄是sbin冲秽,里面只有可執(zhí)行文件nginx舍咖,當(dāng)然找不到了。所以需要到nginx根目錄下執(zhí)行锉桑。
$cd ../../
$ ./bin/sbin/nginx -t
nginx: the configuration file ./bin/conf/nginx.conf syntax is ok
nginx: configuration file ./bin/conf/nginx.conf test is successful
測試通過排霉,可以正式執(zhí)行,啟動服務(wù)了民轴。
$ ./bin/sbin/nginx
查看nginx服務(wù)進(jìn)程攻柠。
$ ps aux | grep nginx
root 7245 0.0 0.2 53368 5320 ? Ss 15:47 0:00 nginx: master process ./bin/sbin/nginx
nobody 7246 0.0 0.3 53768 6076 ? S 15:47 0:00 nginx: worker process
root 7263 0.0 0.0 112708 984 pts/4 R+ 15:50 0:00 grep --color=auto nginx
服務(wù)啟動成功球订。
測試服務(wù)
在windows瀏覽器中通過http來訪問:http://xxx.xxx.xxx.xxx:8080/
報錯:403 Forbidden
其實前面我們查看nginx進(jìn)程的時候,可以發(fā)現(xiàn)master process和worker process的用戶不一致瑰钮,一個是root而另一個是nobody
重新修改nginx.conf文件冒滩,添加root用戶。
#設(shè)置為root用戶
user root;
worker_processes 1;
error_log logs/error.log debug;
...
配置文件更改了浪谴,需要重新加載配置文件旦部。
./bin/sbin/nginx -s reload
刷新瀏覽器就正常了。
停止服務(wù)
1较店,從容停止服務(wù)
這種方法較stop相比就比較溫和一些了士八,需要進(jìn)程完成當(dāng)前工作后再停止。
./bin/sbin/nginx -s quit
2梁呈,立即停止服務(wù)
這種方法比較強(qiáng)硬婚度,無論進(jìn)程是否在工作,都直接停止進(jìn)程官卡。
./bin/sbin/nginx -s stop
3蝗茁,殺死進(jìn)程
pkill -9 nginx
直播推流測試
推流可以使用EV錄頻。
設(shè)置串流地址:
rtmp://xxx.xxx.xxx.xxx/myapp/
播放可以使用EV播放器寻咒。
播放網(wǎng)絡(luò)流地址:
rtmp://xxx.xxx.xxx.xxx/myapp/