1. 程式人生 > >在Mac上搭建Nginx伺服器(視訊直播)

在Mac上搭建Nginx伺服器(視訊直播)

在Mac上搭建Nginx伺服器

前言:這裡搭建額Nginx伺服器,主要是起來作為視訊直播推流拉流的中轉站(流媒體處理伺服器).

按照下面的步驟開始:

1. 安裝Homebrew,執行命令

Homebrew簡稱brew,是macOS上的軟體管理包工具,可以在mac中解除安裝和安裝軟體,開啟終端,執行下面

命令列,安裝brew.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2.安裝Nginx伺服器

增加對Nginx的拓展,也就是從github上下載,homebrew對Nginx的拓展

brew tap homebrew/nginx

3.增加Nginx伺服器和rtmp模組

brew install nginx-full --with-rtmp-module

4.配置Nginx的ramp模組

檢視nginx-full資訊

brew info nginx-full

執行上面的命令後,我們可以看到如下資訊


Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that

nginx can run without sudo

nginx will load all files in
/usr/local/etc/nginx/servers/. - Tips - $ sudo chown root:wheel /usr/local/Cellar/nginx-full/1.10.1/bin/nginx $ sudo chmod u+s /usr/local/Cellar/nginx-full/1.10.1/bin/nginx Reload config: $ nginx -s reload #重新載入配置檔案 Reopen Logfile: $ nginx -s reopen #再次開啟日誌檔案 Stop process: $ nginx -s stop #停止伺服器 Waiting on
exit process $ nginx -s quit #退出伺服器

從上面可以看出,

  • Naginx安裝所在位置

/usr/local/Cellar/nginx-full/

  • Nginx配置檔案所在位置

/usr/local/etc/nginx/nginx.conf

  • Nginx伺服器根目錄所在位置

/usr/local/var/www

  • 執行命令,測試下是否成功啟動Nginx伺服器

    (這注意:naginx關機重啟之後是關閉的,執行下面命令開啟,如果不能,直接執行命令nginx開啟伺服器)

/usr/local/Cellar/nginx-full/1.10.0/bin/nginx

  • 在瀏覽器中輸入:# ,如果出現下圖所示,則開啟成功

這裡寫圖片描述

##5.修改Nginx.conf配置檔案,配置rtmp

用xcode開啟Nginx.conf,找到/usr/local/etc/nginx/nginx.conf檔案,用xocde開啟,開啟之後,在檔案的最後

補上一下程式碼段:


http { …… } 

#在http節點下面(也就是檔案的尾部)加上rtmp配置: 

rtmp { 

    server { 

        listen 1935; 

        application live { 

            live on; 

            record off;

          }

      }

 }
  • 說明:

    • rtmp是協議名稱

    • server 說明內部中是伺服器相關配置

    • listen 監聽的埠號, rtmp協議的預設埠號是1935

    • application 訪問的應用路徑是 live

    • live on; 開啟實時

    • record off; 不記錄資料

6.儲存檔案後,重新載入nginx的配置檔案

nginx -s reload

  • ##7.安裝ffmpeg工具

brew install ffmpeg

安裝ffmpeg需要一段時間,這裡提供一個支援rtmp協議的視訊播放器.Mac下可以用FLV

8.通過ffmpeg命令進行推流

ffmpeg -re -i 你的視訊檔案的絕對路徑(如/Users/lideshan/Downloads/liveVideo.mp4) -vcodec copy -f flv rtmp://localhost:1935/live/room

- 說明:

    - localhost 指的是我們本地電腦(IP地址)

    - live是app訪問路徑

    - room是推流上去的路徑(可以隨意寫,拉流的時候根據這個不同的路徑標識拉取不同的流(主播))

9.驗證推流上去的流,下面通過FLV播放器拉流

電腦上開啟vlc這個播放器軟體 點選File—->Open Network 在彈出來的框中選擇Network然後輸入URL:

rtmp://localhost:1935/live/room
這裡寫圖片描述

10.可能出現的問題

- 找不到C語言的編譯器clang

  錯誤資訊:

checking for OS 

+ Darwin 15.3.0 x86_64 

checking for C compiler ... not found 

./configure: error: C compiler clang is not found

這裡寫圖片描述

 解決辦法:需要去apple官網[下載](https://developer.apple.com/download/more/)命令列工具,安裝即可. 注意命令列工具的版本需要和你的Xcode對應.