下载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进行播放;