下載nginx和nginx-rtmp-module
wget http://nginx.org/download/nginx-1.19.4.tar.gz
git clone https://github.91chifun.workers.dev//https://github.com/arut/nginx-rtmp-module.git (快速通道下載)
生成makefile編譯文件
tar -zxvf nginx-1.19.4.tar.gz (解壓)
cd nginx-1.19.4
./configure --prefix=/data/server/nginx --with-http_ssl_module --add-module=../nginx-rtmp-module
到這一步的時候碰到如下錯誤:(注意如果確實pcre,zlib的話,使用homebrew安裝就可以了,安裝后的目錄系統會自動搜索到)
checking for OpenSSL library ... not found
checking for OpenSSL library in /usr/local/ ... not found
checking for OpenSSL library in /usr/pkg/ ... not found
checking for OpenSSL library in /opt/local/ ... not found
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
原因有可能最后openssl沒有安裝到系統會自動搜索的那幾個目錄。我的mac上就不是。按照提示加上路徑即可(注意
/usr/local/Cellar/openssl@1.1/1.1.1h
是你自己mac
電腦上openssl
的路徑,如果沒有安裝OpenSSL
,則先安裝OpenSSL
:):
brew install openssl
./configure --prefix=/Users/wangqi/ndk/nginx/nginx-1.19.4/bin --add-module=../nginx-rtmp-module-1.2.1/ --with-openssl=/usr/local/Cellar/openssl@1.1/1.1.1h
如果添加路徑后編譯成功:
Configuration summary
+ using system PCRE library
+ using OpenSSL library: /usr/local/Cellar/openssl@1.1/1.1.1h
+ using system zlib library
nginx path prefix: "/Users/wangqi/ndk/nginx"
nginx binary file: "/Users/wangqi/ndk/nginx/sbin/nginx"
nginx modules path: "/Users/wangqi/ndk/nginx/modules"
nginx configuration prefix: "/Users/wangqi/ndk/nginx/conf"
nginx configuration file: "/Users/wangqi/ndk/nginx/conf/nginx.conf"
nginx pid file: "/Users/wangqi/ndk/nginx/logs/nginx.pid"
nginx error log file: "/Users/wangqi/ndk/nginx/logs/error.log"
nginx http access log file: "/Users/wangqi/ndk/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp
編譯成功執行make && make install
可能出現如下錯誤:
/Library/Developer/CommandLineTools/usr/bin/make -f objs/Makefile
cd /usr/local/opt/openssl@1.1 \
&& if [ -f Makefile ]; then /Library/Developer/CommandLineTools/usr/bin/make clean; fi \
&& ./config --prefix=/usr/local/opt/openssl@1.1/.openssl no-shared no-threads \
&& /Library/Developer/CommandLineTools/usr/bin/make \
&& /Library/Developer/CommandLineTools/usr/bin/make install_sw LIBDIR=lib
/bin/sh: ./config: No such file or directory
make[1]: *** [/usr/local/opt/openssl@1.1/.openssl/include/openssl/ssl.h] Error 127
make: *** [build] Error 2
則需要修改當前目錄下
auto/lib/openssl/conf
文件,執行vim auto/lib/openssl/conf
打開文件,替換以下代碼:
CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"
替換成(其實就是去掉了一層路徑):
CORE_INCS="$CORE_INCS $OPENSSL/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libcrypto.a"
注意:修改完之后需要重新執行一遍 configure,否則修改不生效,如下:
./configure --prefix=/Users/wangqi/ndk/nginx/nginx-1.19.4/bin --add-module=../nginx-rtmp-module-1.2.1/ --with-openssl=/usr/local/Cellar/openssl@1.1/1.1.1h
make && make install
上面重新執行代碼也可以直接執行:nginx -s reload
執行完成后就會在當前目錄下生成一個
bin
文件夾,里面包含了我們編譯成功生成的文件,如下四個文件夾:
conf logs html sbin
配置nginx.conf
文件:
#進入目錄
cd bin/conf/
#打開文件
vim nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
#最多支持高并發的數量,最多1024個人觀看,可以調整
worker_connections 1024;
}
#rtmp協議
rtmp {
server {
#默認端口
listen 1935;
#直播應用名稱(自定義)
application myapp{
live on; #代表打開直播
drop_idle_publisher 5s;#丟棄空閑的發布者,如果連接服務器超過5s則丟棄掉。(因為并發數量很寶貴)
}
}
}
#代表支持http協議
http {
server {
listen 8081;#服務監聽端口
#以下是分發路徑,比如你本地訪問http://localhost:8081/stat,則指向下面路徑
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
#http://localhost:8081/stat.xsl
location /stat.xsl {
root /Users/wangqi/ndk/nginx/nginx-rtmp-module-1.2.1/;
}
#http://localhost:8081/control
location /control {
rtmp_control all;
}
#http://localhost:8081/rtmp-publisher
location /rtmp-publisher {
root /Users/wangqi/ndk/nginx/nginx-rtmp-module-1.2.1/test;
}
#http://localhost:8081/
location / {
root /Users/wangqi/ndk/nginx/nginx-rtmp-module-1.2.1/test/www;
}
}
}
保存文件,回到
nginx-1.19.4
目錄下,執行.bin/sbin/nginx
,其實就是執行我們bin->sbin
目錄下的nginx
文件。
可能報錯提示端口被占用(1935和8081端口)
#運行以下,查看占用這兩個的進程
sudo lsof -i:1935
sudo lsof -i:8081
#查看占用進程
kylin0628-Mac:~ wangqi$ sudo lsof -i:1935
Password:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 35299 wangqi 6u IPv4 0x2d203dd3a5956239 0t0 TCP *:macromedia-fcs (LISTEN)
nginx 35300 wangqi 6u IPv4 0x2d203dd3a5956239 0t0 TCP *:macromedia-fcs (LISTEN)
kylin0628-Mac:~ wangqi$ sudo lsof -i:8081
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 35299 wangqi 7u IPv4 0x2d203dd3afdfc759 0t0 TCP *:sunproxyadmin (LISTEN)
nginx 35300 wangqi 7u IPv4 0x2d203dd3afdfc759 0t0 TCP *:sunproxyadmin (LISTEN)
#殺掉占用進程,重新執行nginx(如果執行沒有任何返回則執行成功)
kill -9 35299
kill -9 35300
.bin/sbin/nginx
1. 使用第三方軟件拉流測試。VLC
1.1: 下載成功安裝
安裝成功配置拉流地址展示添加成功,右鍵點擊播放
推流測試:
ffmpeg -re -i 你的視頻文件的絕對路徑(如/Users/wangqi/ndk/nginx/video.mp4) -vcodec copy -f flv rtmp://localhost:1935/myapp/room
(1935
是我們上面rtmp
的默認進程號,myapp
就是我們上面自定義的application
名字,room
代表房間可隨便寫,注意這是本地一個rtmp
協議)
// 如:ffmpeg -re -i /Users/wangqi/ndk/nginx/video.mp4 -vcodec copy -f flv rtmp://localhost:1935/myapp/room
推流過程
拉流準備播放完成,在推流過程中就會彈出推流的視頻。順序是先準備拉流,再推。以上是把本地的視頻文件推流到服務器然后利用VLC進行播放;