一、下載
1、Nginx:https://github.com/nginx/nginx
2、OpenSSL:https://github.com/openssl/openssl
3、rtmp:https://github.com/arut/nginx-rtmp-module
tips: 找到release界面,右擊最新版本,複製連接地址,然後在linux命令行中用wget下載:
#wget https://github.com/nginx/nginx/archive/release-1.19.0.tar.gz
//#wget https://github.com/openssl/openssl/archive/openssl-3.0.0-alpha4.tar.gz
#wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1g.tar.gz
#wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz
當前openssl的最新版本是3.0.0.alpha4, 但實測發現後面配置時報錯,因此改用了1.1.1g。
二、安裝依賴工具(PCRE、ZLIB)
pcre用於重寫rewrite,zlib用於gzip壓縮。
1. pcre
(注意nginx不支持pcre2,所以當你找到pcre2版本時請忽略)
#wget https://netix.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz
#tar zxvf pcre-8.40.tar.gz
#cd pcre-8.40
#./configure
#make
#make install
安裝成功的正確提示:
常見問題:
./configure時如果報錯:
error: Invalid C++ compiler or C++ compiler flags
解決辦法:
sudo apt-get install build-essential
2. zlib
zlib適用於數據壓縮的函數式庫,是一個免費的、通用的、法律上不受阻礙(即沒有被任何專利覆蓋)的無損數據壓縮庫。幾乎適用於任何計算器硬件和操作系統。
#wget http://www.zlib.net/zlib-1.2.11.tar.gz
#tar zxvf zlib-1.2.11.tar.gz
#cd zlib-1.2.11
#./configure
#make
#make install
三、編譯Nginx
1. 編譯OpenSSL
./config --prefix=`pwd`/libs
make
make install
常見問題:試過openssl-3.0.0,配置時會報錯
2. 編譯Nginx
先準備好rtmp和openssl的路徑,方便後面拷貝:
配置編譯安裝:
./auto/configure --add-module=<路徑1> --with-openssl=<路徑2>
make
make install
Nginx生成目錄:/usr/local/nginx
四、配置Nginx
用:nginx-rtmp-module-1.2.1/test/nginx.conf
替換
/usr/local/nginx/conf/nginx.conf
看下nginx.conf裏面配置什麼:
worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024; #最大連接數
}
rtmp {
server {
listen 1935; #監聽端口
application myapp {
live on;
#record keyframes;
#record_path /tmp;
#record_max_size 128K;
#record_interval 30s;
#record_suffix .this.is.flv;
#on_publish http://localhost:8080/publish;
#on_play http://localhost:8080/play;
#on_record_done http://localhost:8080/record_done;
}
}
}
http {
server {
listen 8080;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /path/to/nginx-rtmp-module/;
}
location /control {
rtmp_control all;
}
#location /publish {
# return 201;
#}
#location /play {
# return 202;
#}
#location /record_done {
# return 203;
#}
location /rtmp-publisher {
root /path/to/nginx-rtmp-module/test;
}
location / {
location / {
root /path/to/nginx-rtmp-module/test/www;
}
}
}
- 最大連接數:1024
- rtmp服務端口:1935
- http服務端口:8080
- rtmp的應用名稱:myapp
五、啓動停止Nginx
1、啓動:
#su
#cd /usr/local/nginx/sbin
#./nginx
2、停止:
#./nginx -s stop
六、測試推流
1. 用ifconfig查看rtmp服務器的ip地址
2. 準備一個視頻
(這是h264編碼的《小婦人》預告片)
3. 用ffmpeg推流:
ffmpeg -re -i xfr.mp4 -vcodec libx264 -acodec aac -f flv rtmp://192.168.4.222/myapp/mystream
4. 用VLC播放:
rtmp://192.168.4.222/myapp/mystream
終於看到我們推送的視頻流了!
七、附錄
ffmpeg下載:http://ffmpeg.org/
VLC下載:https://www.videolan.org/vlc/