直播推流服務器端搭建

環(huá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方便查看

  • 進入Nginx解壓目錄,,執(zhí)行configure:./configure --prefix=./bin --add-module=../nginx-rtmp-module-1.2.1

nginx 中 gzip 模塊需要 zlib 庫,rewrite模塊需要 pcre 庫,ssl 功能需要 openssl 庫。所以如果服務器未安裝這三個依賴庫的話會報錯,需要先安裝這三個依賴庫

  • 執(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:配置相關
html:歡迎頁面、錯誤頁面
logs:日志存放區(qū)
sbin:可執(zhí)行文件存放區(qū)

修改配置

Nginx默認不支持rtmp,需要修改配置文件。

如何修改,可以參考:nginx-rtmp-module-1.2.1/test/nginx.conf

進入bin/conf目錄,找到 nginx.conf 文件。

worker_processes  1;
error_log  logs/error.log debug;
events {
    worker_connections  1024;
}
#rtmp標簽
rtmp {
    #服務標簽,一個rtmp服務中可以有多個server標簽,每個標簽可監(jiān)聽不同端口號
    server {
        #注意端口占用,1935為默認端口
        listen 1935;
        #應用標簽,一個服務標簽中可以有多個應用標簽
        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;
        }
    }
}

啟動服務

進入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 表示測試。

仔細看錯誤說明,"./bin/logs/error.log" 文件找不到?也就是當前目錄下找不到 bin/logs/error.log。我們執(zhí)行的當前目錄是sbin,里面只有可執(zhí)行文件nginx,當然找不到了。所以需要到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í)行,啟動服務了。

$ ./bin/sbin/nginx 

查看nginx服務進程。

$ 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

服務啟動成功。

測試服務

在windows瀏覽器中通過http來訪問:http://xxx.xxx.xxx.xxx:8080/

報錯:403 Forbidden
其實前面我們查看nginx進程的時候,可以發(fā)現(xiàn)master process和worker process的用戶不一致,一個是root而另一個是nobody

重新修改nginx.conf文件,添加root用戶。

#設置為root用戶
user root;
worker_processes  1;
error_log  logs/error.log debug;
...

配置文件更改了,需要重新加載配置文件。

./bin/sbin/nginx -s reload

刷新瀏覽器就正常了。

停止服務

1,從容停止服務
這種方法較stop相比就比較溫和一些了,需要進程完成當前工作后再停止。
./bin/sbin/nginx -s quit
2,立即停止服務
這種方法比較強硬,無論進程是否在工作,都直接停止進程。
./bin/sbin/nginx -s stop
3,殺死進程
pkill -9 nginx

直播推流測試

推流可以使用EV錄頻。

設置串流地址:rtmp://xxx.xxx.xxx.xxx/myapp/

播放可以使用EV播放器。

播放網(wǎng)絡流地址:rtmp://xxx.xxx.xxx.xxx/myapp/

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容