nginx-1.15.5 windows下 64位版本编译过程详解

编译源码、工具、脚本等和发布在下面地址:

https://download.csdn.net/download/travel981cn/10701520

源码准备:

官网发布的NGINX源码,不包含windows编译部分,但是包含了主要的源码(片断一):
下载地址:http://nginx.org/download/nginx-1.15.5.tar.gz

windows编译的部分,在Mercurial版本管理系统中,
要下载这部分源码,需要msys2安装python27,和Mercurial。

msys2下载地址为:http://repo.msys2.org/distrib/x86_64/msys2-x86_64-20180531.exe

开启msys2开始菜单中的MSYS2 MinGW 64-bit,输入msys2编译环境安装指令为:
pacman -S base-devel mingw-w64-x86_64-toolchain msys2-devel sys-utils

如果python是windows版本的,那么在widows下安装Mercurial指令为:
pip install Mercurial

如果是msys2下下载源码,上面的编译环境安装指令应该包括Mercurial包。

Mercurial 安装完成后,用下面命令下载源码(片断二):
hg clone http://hg.nginx.org/nginx
cd nginx
hg co 7369:64721be763b6

下载的源码是最新的,需要返回到7369:64721be763b6,这个版本才是nginx-1.15.5,
和(片断一)的版本才统一。然后将(片断一)和(片断二)合并。

hg不返回版本合并源码后能否成功编译我不清楚。你想试的话请先备份。

接下来还需要openssl,pcre,zlib的源码:

openssl:https://www.openssl.org/source/openssl-1.1.0i.tar.gz
pcre:   https://ftp.pcre.org/pub/pcre/pcre-8.42.zip
zlib:   http://www.zlib.net/zlib-1.2.11.tar.gz

全部解压,并分别重命名为:openssl pcre zlib 复制到合并后的源码目录。

开始编译:

首先需要调用msvc的命令提示,这个命令提示会向msys2传递变量,最主要是的:
%INCLUDE% %LIB% %VCINSTALLDIR% %UniversalCRTSdkDir% 注意大小写。

call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64

然后调用msys2命令提示:
call msys64\msys2_shell.cmd -mingw64

%INCLUDE% 和%LIB%传入到msys2中,检查:

echo $INCLUDE
echo $LIB

但%PATH%变量不会被传递,需要手工设置:

PATH=`cygpath -u "${VCINSTALLDIR}"`/Tools/MSVC/14.15.26726/bin/Hostx64/x64:"$PATH"
PATH=`cygpath -u "${UniversalCRTSdkDir}"`/bin/"${UCRTVersion}"/x64:"$PATH"
PATH=/d/platform/nasm:/d/platform/perl/bin:$PATH

上面的设置里指出了nasm和perl,编译openssl需要用到,请根据你系统环境修改路径。
盘符路径请用 /c/windows /d/build/ 的形式,路径分隔符请用/

验证一下路径是否正确,编译pcre要用到rc.exe,一定要把SDK的bin目录加入,也就是上面的:
PATH=`cygpath -u "${UniversalCRTSdkDir}"`/bin/"${UCRTVersion}"/x64:"$PATH" 这一行。

which cl.exe 
which rc.exe
which perl.exe
which nasm.exe

然后进入到源码目录,我的目录是/d/project/build/nginx-1.15.5

编译参数:
auto/configure \
--with-cc=cl \
--builddir=objs \
--prefix=  \
--conf-path=conf/nginx.conf \
--error-log-path=logs/nginx_error.log \
--http-log-path=logs/nginx_access.log \
--pid-path=logs/nginx.pid \
--http-client-body-temp-path=temp/client_body_temp \
--http-proxy-temp-path=temp/proxy_temp \
--http-fastcgi-temp-path=temp/fastcgi_temp \
--http-uwsgi-temp-path=temp/uwsgi_temp \
--http-scgi-temp-path=temp/scgi_temp \
--with-cc-opt=-DFD_SETSIZE=1024 \
--with-http_ssl_module \
--with-select_module \
--with-openssl=openssl \
--with-pcre=pcre \
--with-zlib=zlib

注意,如果要用到64位编译,这里需要修改nginx-1.15.5\auto\lib\openssl\makefile.msvc文件:
将第9行 VC-WIN32 改成 VC-WIN64A,

或者用sed命令来改:
sed -i "s/VC\-WIN32/VC\-WIN64A/g" auto/lib/openssl/makefile.msvc

参数说明:
auto/configure 一定是auto目录下的configure,只有这个文件是windows下编译用的。

--with-cc=cl,指定编译器用cl。

--builddir=objs,指定编译临时目录,最终生成的nginx.exe文件也在这里。
这个选项可忽略,不指定默认也是objs。

--prefix=  等号后是个空格,这个参数至关重要,别看他是空的。就是一定要指定。
在linux下,不指定默认是/usr/local
而在windows下,如果你不指定那默认也是/usr/local,而windows下,是没有这个目录的,
磁盘根目录下的usr是带盘符的,也就是/d/usr/local,呃。所以你的nginx会运行不起来。

--conf-path=conf/nginx.conf,指定主配置文件,默认就是conf/nginx.conf
如果不改变文件位置或是文件名,这个选项是可忽略的,conf前面不要有/,原理同上:/d/conf/

--error-log-path=logs/nginx_error.log
--http-log-path=logs/nginx_access.log

我这里修改了log文件的名字,所以编译时特意指出来。默认是conf/error.log conf/access.log
原则上,这二个文件的位置和名称,是可以在主配置文件nginx.conf里指定的,
但是,如果你选择不在这里指出参数,而仅修改nginx.conf的话,nginx会单独生成一个error.log
也就是说,如果编译时没有指定 --error-log-path=logs/nginx_error.log 
而仅在nginx.conf中修改 "error_log  logs/nginx_error.log",那么nginx会生成二个error.log
一个是默认的error.log,一个是你在nginx.conf中指定的nginx_error.log

--pid-path=logs/nginx.pid 默认值是 logs/nginx.pid 保留默认可忽略,就是可以不写这一行。

--http-client-body-temp-path=temp/client_body_temp
--http-proxy-temp-path=temp/proxy_temp
--http-fastcgi-temp-path=temp/fastcgi_temp
--http-uwsgi-temp-path=temp/uwsgi_temp
--http-scgi-temp-path=temp/scgi_temp

上面这几个temp全是默认值,默认的话,可忽略。

最近又测试了下,这几个路径如果不指定,默认会创建在nginx的根目录下。所以需要指定

--with-openssl=openssl 开启ssl验证。不开不能解析https。
--with-pcre=pcre       开启rewrite。
--with-zlib=zlib       开启服务端压缩。

这三个目录就是上面下回来的三个源码目录。不指定编译时找不到。一定要指定。

--with-cc-opt=-DFD_SETSIZE=1024
FD_SETSIZE是响应链接数,官方的指定是1024。
此设置下Nginx可以处理的链接数在4500个左右,参见http://forum.nginx.org/read.php?2,242426,242426实测
就是你单机自己玩的话应该够了,中小型网站也没问题,大型网站慢慢摸索去吧。

--with-http_ssl_module,支持https。静态编译进nginx。静态的意思是不再需要单独的openssl包。

--with-select_module,解释起来很复杂。低负载可以。高负载可选。

精简的编译参数如下,用默认值的忽略,必须的指定:

auto/configure \
--with-cc=cl \
--prefix= \
--error-log-path=logs/nginx_error.log \
--http-log-path=logs/nginx_access.log \
--http-client-body-temp-path=temp/client_body_temp \
--http-proxy-temp-path=temp/proxy_temp \
--http-fastcgi-temp-path=temp/fastcgi_temp \
--http-uwsgi-temp-path=temp/uwsgi_temp \
--http-scgi-temp-path=temp/scgi_temp \
--with-cc-opt=-DFD_SETSIZE=1024 \
--with-http_ssl_module \
--with-select_module \
--with-openssl=openssl \
--with-pcre=pcre \
--with-zlib=zlib
 

然后执行nmake,用vc的nmake而不是make。
nmake

注意,nmake install 会报错,所以不install了。
可以修改auto目录下的install文件,把install段 test -d 相关的部分删掉。不过很麻烦。
千万不要修改编译目录的makefile,就是默认的objs目录下的makefile,
如果你修改了这个文件,执行nmake install会重新编译。
nmake会侦测makefile文件的修改情况。有改动就重新编译。

nginx的发布:
如果编译成功完成,即使不install也可以发布。新建一个发布目录。
把objs目录下新生成的nginx.exe,复制到发布目录。
源码目录下的 conf contrib docs html 复制到发布目录。
在发布目录新建空文件夹,logs temp
发布完成。
 

你可能感兴趣的:(nginx-1.15.5 windows下 64位版本编译过程详解)