本文发布于 2024-03-29, 最后更新于 880 天前,其中的信息可能已经有所发展或是发生改变。

文章摘要

Foreword Linux 为当前 nginx 添加 rtmp 模块非常的方便, sudo ./configure --add-module + sudo make 就完事儿了,但是windows比较复杂,没有包管理器,所以各个模块的源码要自己找,下面是我在 windows11 下的 nginx with rtmp module 的编译记录。 编译器工具链有 msvc toolchain 、 p…

Foreword

Linux为当前nginx添加rtmp模块非常的方便,sudo ./configure --add-module + sudo make就完事儿了,但是windows比较复杂,没有包管理器,所以各个模块的源码要自己找,下面是我在windows11下的nginx with rtmp module的编译记录。

编译器工具链有msvc toolchainperl

以下是我操作系统的具体信息:

text
OS: Windows 11 Pro x86_64 Kernel: 10.0.22621 CPU: Intel Ultra 7 155H (22) @ 3.000GHz

Prerequisite

首先下载Nginx的源码,可以使用git也可以使用Mercurial不过因为后续要借用git bash生成makefile所以下面统一使用git。在写这篇博客时,nginxmainline1.25.4,所以下面编译我也用1.25.4的版本。

首先在自定义目录(自己想把源码放哪儿就在哪儿打开)打开git-bash
输入下列指令clone nginx仓库

text
git clone https://github.com/nginx/nginx.git cd nginx git checkout release-1.25.4

clone 完成后,要查看一下当前版本所需依赖的版本,输入下列命令查看

text
cat -n misc/GNUmakefile

我的控制台输出如下,大概可以看到openssl的版本是3.0.13zlib的版本是1.3.1pcre2的版本是10.39

text
8 OBJS = objs.msvc8 9 OPENSSL = openssl-3.0.13 10 ZLIB = zlib-1.3.1 11 PCRE = pcre2-10.39

以下我下载指定版本的依赖源码使用的是git clone,当然也可以通过下载压缩包然后解压的方式,这里我给出两种方法所需要的操作步骤

bash (Auto identified)
mkdir objs mkdir objs/lib cd objs/lib git clone https://github.com/openssl/openssl.git git clone https://github.com/madler/zlib.git git clone https://github.com/pcre2project/pcre2.git cd openssl git checkout openssl-3.0.13 cd ../zlib/ git checkout v1.3.1 cd ../pcre2/ git checkout pcre2-10.39

然后接着下载rtmp module的源码

text
git clone https://github.com/arut/nginx-rtmp-module.git git checkout v1.2.2 cd ../../..

Main

源码部分的准备已经完成,下面正式开始编译部分,我们重新回到源代码主目录,运行configuration script,脚本的具体配置可以参考nginx官网

但如果想要更完整的nginx,即仅仅只是在发行版上增加rtmp module,最好的办法是先看看发行版的编译参数

下载1.25.4版本,并输入nginx -V,可以看到以下参数

stylus (Auto identified)
./nginx -V nginx version: nginx/1.25.4 built by cl 16.00.30319.01 for 80x86 built with OpenSSL 3.0.13 30 Jan 2024 TLS SNI support enabled configure arguments: --with-cc=cl --builddir=objs.msvc8 --with-debug --prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-path=logs/access.log --error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp --http-proxy-temp-path=temp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp --http-scgi-temp-path=temp/scgi_temp --http-uwsgi-temp-path=temp/uwsgi_temp --with-cc-opt=-DFD_SETSIZE=1024 --with-pcre=objs.msvc8/lib/pcre2-10.39 --with-zlib=objs.msvc8/lib/zlib-1.3.1 --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_slice_module --with-mail --with-stream --with-stream_realip_module --with-stream_ssl_preread_module --with-openssl=objs.msvc8/lib/openssl-3.0.13 --with-openssl-opt='no-asm no-tests -D_WIN32_WINNT=0x0501' --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module

我们根据我们现有的库的位置,修改脚本参数

stylus (Auto identified)
auto/configure --with-cc=cl --builddir=objs.msvc8 --with-debug --prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-path=logs/access.log --error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp --http-proxy-temp-path=temp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp --http-scgi-temp-path=temp/scgi_temp --http-uwsgi-temp-path=temp/uwsgi_temp --with-cc-opt=-DFD_SETSIZE=1024 --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_slice_module --with-mail --with-stream --with-stream_realip_module --with-stream_ssl_preread_module --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module --with-openssl=objs/lib/openssl --with-openssl-opt='no-asm no-tests -D_WIN32_WINNT=0x0501' --with-pcre=objs/lib/pcre2 --with-zlib=objs/lib/zlib --add-module=objs/lib/nginx-rtmp-module

运行后,得到如下输出,代表着makefile已经生成完毕

livecodeserver (Auto identified)
Configuration summary + using PCRE2 library: objs/lib/pcre2 + using OpenSSL library: objs/lib/openssl + using zlib library: objs/lib/zlib nginx path prefix: "" nginx binary file: "/nginx.exe" nginx modules path: "/modules" nginx configuration prefix: "/conf" nginx configuration file: "/conf/nginx.conf" nginx pid file: "/logs/nginx.pid" nginx error log file: "/logs/error.log" nginx http access log file: "/logs/access.log" nginx http client request body temporary files: "temp/client_body_temp" nginx http proxy temporary files: "temp/proxy_temp" nginx http fastcgi temporary files: "temp/fastcgi_temp" nginx http uwsgi temporary files: "temp/uwsgi_temp" nginx http scgi temporary files: "temp/scgi_temp"

生成后,手动修改makefile,使得msvc编译器不将warnings视为错误,因为我在编译rtmp-module的时候报了warnings导致编译失败。同时添加-MP选项使得msvc支持多进程编译,加快编译速度。

输入以下指令,编辑Makefile,Makefile的具体地址请以上面的autoconfigure 脚本生成的地址为准,不同环境生成地址可能不同。

text
vim ./objs.msvc8/Makefile

找到CFLAGS参数,删除-WX, 并添加-MP,然后保存。
以下是我修改后的参数:

text
CFLAGS = -MP -O2 -W4 -nologo -MT -Zi -Fdobjs.msvc8/nginx.pdb -DFD_SETSIZE=1024 -DNO_SYS_TYPES_H

接下来打开Visual Studio 2022 Developer Command PromptVisual Studio 版本不同,命令行名字可能也不同

切换到源码目录,运行nmake,等待一段时间,出现下列输出,即表示编译成功。

text
Finished searching libraries sed -e "s|%PREFIX%||" -e "s|%PID_PATH%|/logs/nginx.pid|" -e "s|%CONF_PATH%|/conf/nginx.conf|" -e "s|%ERROR_LOG_PATH%|/logs/error.log|" < docs/man/nginx.8 > objs.msvc8/nginx.8

切换到objs.msvc8目录运行./nginx -V,在configure arguments:的输出中如果能看到--add-module=objs/lib/nginx-rtmp-module,表示nginx已成果添加rtmp-module

End

其他nginx-module的编译方式类同。以上仅供参考,build toolchain版本的不同,上述编译步骤都可能略有不同,具体请根据自身开发环境进行调整。

Reference

九仞之行

严于律己,宽以待人,深自警省,讷言敏行

订阅
💬 评论 (0)

还没有评论呢 ~ 快来抢沙发吧 🛋️

编辑器加载中…