1. 概述
- 有时由于添加了自己编写的c++模块,或者改了源码的一些名称、配置什么的,需要自行编译nginx。
- 可以先下下来源码,然后需要的话就自己改下源码或增加模块,最后再编译成linux或者windows平台来用。
- 参考
2. 在Windows上编译
- 准备(zlib、pcre、openssl可以统一放在objs/libs下面,后面的执行的configure脚本文件可以指定目录)
- nginx源码:
- 可以用Mercurial源码下载器,自行下载 64位 or 32 位,在其安装目录下(或者加到环境变量Path中)运行命令:hg clone http://hg.nginx.org/nginx
- 或者去网站下载地址,Github地址
- 不同版本的目录结构有所不同。比如用于生成Makefile的configure文件在1.12.2中位于根目录,而在1.14.2中位于auto目录下。
- 有的版本可能会出错
- perl环境:安装ActivePerl或者Strawberry Perl,ssl环境,自行下载 64位 or 32 位
- zlib:ssl相关,库函数,比如用1.2.11
- PCRE:ssl相关,库函数,比如用8.42,下载地址
- OpenSSL:版本不要太高,否则可能出错,比如用1.0.1s
- MSYS(GNU环境)
- 可以用MinGW去下载,然后运行msys.bat。下载mingw-get-setup.exe直接执行直接下一步即可,安装完成之后找到文件bin/mingw-get.exe打开,用这个来安装msys点击左侧的Basic Setup右侧选择mingw32-base和msys.base只有点击菜单isntallation-》Apply Changes 开始下载,完成之后点击左侧All Packages-》MSYS-》MSY Base System检查右侧菜单的选中组件中是否有msys-make、msys-tar因为这两个组件会在接下来用到。
- 也可以去Sourceforge下载,或者用msys2
- Visual Studio,会用到其VS2012 x86 Native Tools Command Prompt工具
- nginx源码:
- 创建编译命令集
- 在Nginx根目录下,创建build.bat,内容为(不同版本nginx的configure文件--shell脚本文件,位置不同;注意修改依赖的库的路径;官网demo上的命令略有不同):
auto/configure --with-cc=cl --builddir=objs --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 \ --with-cc-opt=-DFD_SETSIZE=1024 --with-pcre=objs/lib/pcre-8.42 \ --with-zlib=objs/lib/zlib-1.2.11 --with-openssl=objs/lib/openssl-1.0.1s \ --with-select_module --with-http_ssl_module
- 生成MakeFile资源
- 运行MSYS(安装完成后,自动在桌面有快捷方式)切换到Nginx根目录,执行以下命令:build.bat
- 如果使用MinGW装的GNU环境,那么可以运行msys.bat
- 编译Nginx源码(过程1分半钟)
- 运行VS命令行(比如VS2012为其目录下的VS2012 x86 Native Tools Command Prompt,也可能是x64的),切换到Nginx根目录,执行以下命令:nmake -f objs/Makefile
- 编译完成后,objs目录下会生成nginx.exe
3. 可能遇到的问题
- 'sed' is not recognized as an internal or external command,
- 编译源码时发生,如果不解决,运行生成的nginx.exe会报错:NMAKE : fatal error U1073: don't know how to make 'src/os/win32/ngx_win32_config.h'