环境: win10, cygwin 3.3, nginx 1.20.2
https://www.cygwin.com/setup-x86_64.exe
安装目录选择 c:\cygwin
即可, packages可选 d:\cygwin_package
用户目录在安装目录的home下, 需要安装的程序源码可以放在这里
在"Select Packages"时搜索并选择以下包:
双击版本号即可选择
需要额外的包, 可以再次运行setup程序选择安装.
安装完成后打开命令行, 输入以下几个命令, 检查安装结果:
#检查是否ok
$ cygcheck -c cygwin
Cygwin Package Information
Package Version Status
cygwin 3.3.3-1 OK
#查看gcc版本:
$ gcc --version
gcc (GCC) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#查看g++版本:
$ g++ --verion
官方链接: https://github.com/transcode-open/apt-cyg
# 获取安装源
wget -O apt-cyg.zip https://github.com/transcode-open/apt-cyg/archive/refs/heads/master.zip
# 解压
unzip apt-cyg.zip
cd apt-cyg-master
# 安装(就是一个shell脚本, 基本上1秒内执行完成)
install apt-cyg /bin
# 查看版本
apt-cyg --version
使用 apt-cyg install
依次安装以下包:
apt-cyg install openssl
#rewrite模块使用pcre
apt-cyg install pcre
#HTTP gzip模块使用zlib, 只安装zlib还不行
apt-cyg install zlib
apt-cyg install zlib-devel
#这个automake安装比较费时
apt-cyg install automake
apt-cyg install bison
apt-cyg install flex
apt-cyg install libiconv
apt-cyg install libcrypt-devel
apt-cyg install libmcrypt-devel
apt-cyg install libtool
apt-cyg install libxml2
apt-cyg install libxml2-devel
apt-cyg install patchutils
apt-cyg install jpeg
apt-cyg install libpcre-devel
apt-cyg install openssl-devel
curl-devel
不存在, 在cygwin安装程序安装 libcurl-devel
(apt-cyg中也可以安装) libmcrypt
不存在(因为已经被cygwin移除了, 见 https://cygwin.com/packages/summary/libmcrypt.html ) apt-cyg install crypt
安装的crypt在配置时无法识别, 但是 libcrypt-devel
包可以. pcre-devel
不存在, 没安装也没报错 cd ~
# 下载nginx-concat插件
wget -O nginx-http-concat-master.zip https://github.com/alibaba/nginx-http-concat/archive/refs/heads/master.zip
unzip nginx-http-concat-master.zip
# 注意: 当前用户路径是 /home/aben
cd ~
#下载源
wget -O nginx-1.20.2.tar.gz http://nginx.org/download/nginx-1.20.2.tar.gz
#解压
tar -xzvf nginx-1.20.2.tar.gz
修改配置文件 nginx-1.20.2\auto\cc\gcc
, 注释掉 CFLAGS="$CFLAGS -g"
这一行, 大概在172行处, 取消debug编译模式
编译 : make
然后 make install
注意: 指定prefix会导致复制到windows下时, 默认加载的配置文件就是指定的目录!!!
#把nginx编译到到`c:/nginx`下
mkdir /cygdrive/c/nginx
#进入编译目录
cd nginx-1.20.2
# 执行编译
./configure --prefix=/cygdrive/c/nginx --sbin-path=/cygdrive/c/nginx --conf-path=conf/nginx.conf --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_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-stream --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module --add-module=/home/aben/nginx-http-concat-master
#make
make
#安装
make install
上面的编译参数中"--add-module=/home/aben/nginx-http-concat-master" 中的路径是我本地的路径, 请自行修改
查看版本及编译参数 :
/cygdrive/c/nginx/nginx.exe -V
注意: 必须输入全路径, 不能cd到/cygdrive/c/nginx/目录后再nginx.exe -V
#启动
/cygdrive/c/nginx/nginx.exe
#停止
/cygdrive/c/nginx/nginx.exe -s stop
#重新加载配置
/cygdrive/c/nginx/nginx.exe -s reload
然后修改 nginx.conf
, 开启 concat
组件, 测试通过.
编译成功后,在Cygwin下运行正常,但如果要脱离Cygwin,需要从 c:\cygwin64\bin
目录下下拷贝 cygwin1.dll, cygz.dll, cygcrypto-1.1.dll, cygcrypt-2.dll, cygssl-1.1.dll, cygpcre-1.dll 等dll到nginx目录下。如果有提示缺少其它的.dll,也一并拷贝进去。
cygwin1.dll
很重要
实际编译后的版本号可能不同.
cygpcre
的有好几个版本的
如果测试时 nginx.exe -V
无法执行, 则把所有 c:/cygwin/bin
下的的dll都拷贝过去, 然后把能删除的再删除(已加载的肯定删除不了)
在windows下默认加载的配置文件是configure编译好的(用 nginx.exe -t
能看出来), 启动时可以指定配置文件:
nginx.exe -c conf/nginx.conf
注意
c:\nginx\config
要转换成 /cygdrive/c/nginx/config
如下一个站点配置, php无法解析, 因为root路径是cygwin格式的, windows版本的php无法识别.
server {
listen 80;
server_name localhost;
# 注意这里要用cygwin的路径
root /cygdrive/c/nginx/html;
#开启concat. 默认是off状态
concat on;
# 修正MIME
concat_types text/css application/javascript;
location / {
try_files $uri $uri/ =404;
index index.html index.htm index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:20074;
fastcgi_index index.php;
#还是按之前在windows/linux下的目录配置, php会无法识别
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
临时的解决方式是把 SCRIPT_FILENAME
改成:
fastcgi_param SCRIPT_FILENAME c:/nginx/html$fastcgi_script_name;
最好的方式还是应该把php也用cygwin编译出来, TODO
--prefix=.
的configure结果是: Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "."
nginx binary file: "./sbin/nginx"
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: "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"
这个在执行install时会报错
make -f objs/Makefile install
make[1]: Entering directory '/home/aben/nginx-1.20.2'
test -d '.' || mkdir -p '.'
test -d './sbin' \
|| mkdir -p './sbin'
test ! -f './sbin/nginx' \
|| mv './sbin/nginx' \
'./sbin/nginx.old'
cp objs/nginx './sbin/nginx'
test -d './conf' \
|| mkdir -p './conf'
cp conf/koi-win './conf'
cp: 'conf/koi-win' and './conf/koi-win' are the same file
make[1]: *** [objs/Makefile:1250: install] Error 1
make[1]: Leaving directory '/home/aben/nginx-1.20.2'
make: *** [Makefile:13: install] Error 2
虽然报错, 但是 nginx.exe
已经出来了.
但是, nginx.exe -t时判断各站点的include文件路径出错
make
, 报错如下: src/os/unix/ngx_user.c: In function ‘ngx_libc_crypt’:
src/os/unix/ngx_user.c:53:13: error: implicit declaration of function ‘crypt’ [-Werror=implicit-function-declaration]
53 | value = crypt((char *) key, (char *) salt);
| ^~~~~
src/os/unix/ngx_user.c:53:11: error: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Werror=int-conversion]
53 | value = crypt((char *) key, (char *) salt);
| ^
cc1: all warnings being treated as errors
make[1]: *** [objs/Makefile:803: objs/src/os/unix/ngx_user.o] Error 1
make[1]: Leaving directory '/home/aben/nginx-1.20.2'
make: *** [Makefile:10: build] Error 2
检查前面在configure时的信息, 搜索crypt相关的记录得到:
checking for crypt.h ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... not found
参考 https://forum.nginx.org/read.php?2,286977,286981#msg-286981 , 安装 libcrypt-devel
, OK