目录一、使用说明二、安装软件2.1 下载依赖库2.3 下载并安装 StrawberryPerl2.4 下载并安装 MSYS 22.5 nginx源代码下载三、编译配置3.1 设置NGX_MSVC_VER3.2 配置 Makefile3.3 编译代码3.4 整理Nginx发布环境四、错误处理一、使用说明本文章主要记录Windows下利用MSYS2和VS的nmake编译nginx源码。这是Nginx官方说明 https://nginx.org/en/docs/howto_build_on_win32.htmlTo build nginx on the Microsoft Win32® platform you need:Microsoft Visual C compiler. Microsoft Visual Studio® 8 and 10 are known to work.MSYS or MSYS2.Perl, if you want to build OpenSSL® and nginx with SSL support. For example ActivePerl or Strawberry Perl.Git client.PCRE, zlib and OpenSSL libraries sources.需要安装软件Visual Studio 2022 (一定要包含C x64/x86 生成工具需要通过nmake执行编译)Openssl:实现安全套接字ssl功能Pcre:实现正则表达式解析Zlib:实现gzip压缩解压缩功能二、安装软件2.1 下载依赖库zlib源代码下载https://zlib.net/PCRE源代码https://sourceforge.net/projects/pcre/files/pcre/openssl源代码下载https://github.com/openssl/openssl/releases/tag/openssl-3.3.22.3 下载并安装 StrawberryPerl因为需要编译 OpenSSL 需要使用 perl 进行配置下载地址 https://strawberryperl.com/默认安装到结束2.4 下载并安装 MSYS 2在编译阶段用作配置 Makefile 下载地址https://www.msys2.org/全部默认安装2.5 nginx源代码下载地址 https://hg.nginx.org/nginx点左侧ZIP进行下载将文件解压在nginx源码根目录下创建objs文件夹再在objs下创建lib文件夹结构如下图将pcre-8.45.zip、zlib131.zip、openssl-openssl-3.3.2.zip解压到刚刚的lib目录三、编译配置3.1 设置NGX_MSVC_VER如果不指定版本会在配置时出现报错auto/cc/msvc: line 132: [: : integer expression expected需要根据你自己的实际vs版本号填写只有填写成功后面nmake才能编译成功。修改nginx\auto\cc下的msvc文件指定NGX_MSVC_VER版本号我vs是2022所以是19303.2 配置 Makefile使用 msys2 的任意环境进入 nginx 源码目录从开始菜单打开安装的msys2 msys进入到nginx源码根目录下cd /c/sourcecode/nginx执行以下命令用于生成适用于 MSVC 的 Makefile输入如下配置命令后回车(注意pcrezlib,openssl版本要与你实际的文件夹一致)auto/configure \ --with-cccl \ --prefix \ --conf-pathconf/nginx.conf \ --pid-pathlogs/nginx.pid \ --http-log-pathlogs/access.log \ --error-log-pathlogs/error.log \ --sbin-pathnginx.exe \ --http-client-body-temp-pathtemp/client_body_temp \ --http-proxy-temp-pathtemp/proxy_temp \ --http-fastcgi-temp-pathtemp/fastcgi_temp \ --http-scgi-temp-pathtemp/scgi_temp \ --http-uwsgi-temp-pathtemp/uwsgi_temp \ --with-cc-opt-DFD_SETSIZE32768 \ --with-pcreobjs/lib/pcre-8.45 \ --with-zlibobjs/lib/zlib-1.3.1 \ --with-opensslobjs/lib/openssl-3.3.2 \ --with-openssl-optno-asm no-tests -D_WIN32_WINNT0x0601 \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_stub_status_module \ --with-http_dav_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-mail_ssl_module \ --with-stream \ --with-stream_ssl_module \ --with-stream_ssl_preread_module执行成功会出现以下界面AdministratorDESKTOP-0JFTJUC MSYS /c/sourcecode/nginx # auto/configure \ --with-cccl \ --prefix \ --conf-pathconf/nginx.conf \ --pid-pathlogs/nginx.pid \ --http-log-pathlogs/access.log \ --error-log-pathlogs/error.log \ --sbin-pathnginx.exe \ --http-client-body-temp-pathtemp/client_body_temp \ --http-proxy-temp-pathtemp/proxy_temp \ --http-fastcgi-temp-pathtemp/fastcgi_temp \ --http-scgi-temp-pathtemp/scgi_temp \ --http-uwsgi-temp-pathtemp/uwsgi_temp \ --with-cc-opt-DFD_SETSIZE32768 \ --with-pcreobjs/lib/pcre-8.45 \ --with-zlibobjs/lib/zlib-1.3.1 \ --with-opensslobjs/lib/openssl-3.3.2 \ --with-openssl-optno-asm no-tests -D_WIN32_WINNT0x0601 \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_stub_status_module \ --with-http_dav_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-mail_ssl_module \ --with-stream \ --with-stream_ssl_module \ --with-stream_ssl_preread_module checking for OS MSYS_NT-10.0-19045 3.5.3-d8b21b8c.x86_64 x86_64 using Microsoft Visual C compiler cl version: 1930 checking for MSYS_NT-10.0-19045 specific features creating objs/Makefile Configuration summary using PCRE library: objs/lib/pcre-8.45 using OpenSSL library: objs/lib/openssl-3.3.2 using zlib library: objs/lib/zlib-1.3.1 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 AdministratorDESKTOP-0JFTJUC MSYS /c/sourcecode/nginx可以看到根目录下的objs下已经生成了makefile文件3.3 编译代码以管理员方式打开VS对应的 X64 Native Tools Command … for VS 2022进入到nginx根目录输入如下命令后回车nmake -f objs/Makefile就开始编译等待编译完成会在 nginx/objs/ 文件夹内出现 nginx.exe至此Nginx编译完成。打开objs目录显示如下3.4 整理Nginx发布环境nginx.exe运行依赖同级目录下存在conf、html、logs、temp目录及配置文件否则无法启动。将源码目录下的conf、contrib、html、logs、temp和objs\nginx.exe整理到一个目录下。启动nginx.exe一个nginx服务器就搭建起来了运行截图看生成的nginx编译信息 nginx -VC:\nginxnginx -V nginx version: nginx/1.27.2 built by cl 1930 built with OpenSSL 3.3.2 3 Sep 2024 TLS SNI support enabled configure arguments: --with-cccl --prefix --conf-pathconf/nginx.conf --pid-pathlogs/nginx.pid --http-log-pathlogs/access.log --error-log-pathlogs/error.log --sbin-pathnginx.exe --http-client-body-temp-pathtemp/client_body_temp --http-proxy-temp-pathtemp/proxy_temp --http-fastcgi-temp-pathtemp/fastcgi_temp --http-scgi-temp-pathtemp/scgi_temp --http-uwsgi-temp-pathtemp/uwsgi_temp --with-cc-opt-DFD_SETSIZE32768 --with-pcreobjs/lib/pcre-8.45 --with-zlibobjs/lib/zlib-1.3.1 --with-opensslobjs/lib/openssl-3.3.2 --with-openssl-optno-asm no-tests -D_WIN32_WINNT0x0601 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_stub_status_module --with-http_dav_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-mail_ssl_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module四、错误处理4.1 auto/cc/msvc: line 132: [: : integer expression expected这里报错原因是msvc版本缺失无法自动识别根据3.1章节进行设置4.2 找不到VS带的X64 Native Tools Command … for VS xxxx检查VS安装是否包含了C x64/x86 生成工具。4.3 编译过程出现sed is not recognized as an internal or external command,报错原因很明确nmake已经链接完库了但生成 man 文件这一步调用了sed而你的 CMD 环境里找不到sed.exe。A.下载安装sed for WindowsGnuWin32https://sourceforge.net/projects/gnuwin32/files/sed/4.2.1/sed-4.2.1-setup.exeB.安装后把 bin 目录加入系统PATHC:\Program Files (x86)\GnuWin32\bin
阅读完成 · 觉得有帮助?