Compiling and Installing From the Sourcesphp
Update 2016/01/28: Updated for nginx-1.8.1 And openssl-1.0.2e.html
最近使用源码安装Nginx,遇到了一些问题,特此记录下来。nginx
检测系统版本:c++
$ uname -rms Darwin 15.3.0 x86_64
检测GCC版本:web
$ gcc -v Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1 Apple LLVM version 7.0.2 (clang-700.1.81) Target: x86_64-apple-darwin15.3.0 Thread model: posix
建立安装目录:正则表达式
为了方便管理,在/usr/local/下新建一个目录webserver,之后全部的软件安装都加 —prefix=/usr/loca/webserver,都会安装到这个目录下:express
$sudo mkdir webserver $pwd /usr/local/webserver
the PCRE library – required by NGINX Core and Rewrite modules and provides support for regular expressions:macos
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.bz2 tar jxvf pcre-8.38.tar.bz2 cd pcre-8.38 ./configure --prefix=/usr/local/pcre-8.38 make make test sudo make install
./configure --prefix=/usr/local/pcre-8.38 --with-openssl=../openssl-1.0.2esegmentfault
the zlib library – required by NGINX Gzip module for headers compression:浏览器
wget http://zlib.net/zlib-1.2.8.tar.gz tar zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8 ./configure --prefix=/usr/local/zlib make make test
the OpenSSL library – required by NGINX SSL modules to support the HTTPS protocol:
$ wget http://www.openssl.org/source/openssl-1.0.2e.tar.gz $ tar -zxf openssl-1.0.2e.tar.gz $ cd openssl-1.0.2e $ ./Configure darwin64-x86_64-cc --prefix=/usr/local/openssl --openssldir=/usr/local/openssl $ make $ sudo make install
查看OpenSSL支持的平台列表: $./Configure LIST
注意: If configuring for 64-bit OS X, then use a command similar to: $./Configure darwin64-x86_64-cc shared enable-ec_nistp_64_gcc_128 no-ssl2 no-ssl3 no-comp --openssldir=/usr/local/ssl/macos-x86_64 $make depend $sudo make install
If configuring for 32-bit OS X, then use a command similar to: ./Configure darwin-i386-cc shared no-ssl2 no-ssl3 no-comp --openssldir=/usr/local/ssl/macosx-i386 $make depend $sudo make install
$ wget http://nginx.org/download/nginx-1.8.1.tar.gz $ tar zxf nginx-1.8.1.tar.gz $ export KERNEL_BITS=64 $./configure --prefix=/usr/local/nginx \ --sbin-path=/usr/local/nginx/nginx \ --conf-path=/usr/local/nginx/nginx.conf \ --pid-path=/usr/local/nginx/nginx.pid \ --with-http_ssl_module \ --with-openssl=../openssl-1.0.2e \ --with-pcre=../pcre-8.38 \ --with-zlib=../zlib-1.2.8
--sbin-path=path — 设置可执行文件名. 默认是:prefix/sbin/nginx. --conf-path=path — 设置配置文件名(nginx.conf). 默认是:prefix/conf/nginx.conf. --pid-path=path — 设置存放进程ID的nginx.pid文件名.默认是:prefix/logs/nginx.pid. --with-http_ssl_module 支持https协议,依赖OpenSSL库. --with-pcre=path 设置PCRE库路径,正则表达式.
查看版本:
$ /usr/local/nginx/nginx -v nginx version: nginx/1.8.1
执行启动命令:
$ sudo /usr/local/nginx/nginx
浏览器查看:
打开浏览器输入:http://127.0.0.1/ 能够看到:"Welcome to nginx! " 页面
命令查看:
$ curl -I 127.0.0.1 HTTP/1.1 200 OK Server: nginx/1.8.1 Date: Mon, 01 Feb 2016 12:29:30 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Mon, 01 Feb 2016 10:25:21 GMT Connection: keep-alive ETag: "56af3291-264" Accept-Ranges: bytes
/usr/local/nginx/nginx.conf
默认的项目根目录: 默认的是html目录,即/usr/local/nginx/html/ location / { root html; index index.html index.htm; } 修改项目根目录: location / { root /Users/WangTom/workspace/webroot/; index index.html index.htm; }
// 测试配置文件 $ sudo /usr/local/nginx/nginx -t nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/nginx.conf test is successful //从新加载配置文件 $ sudo /usr/local/nginx/nginx -s reload
报错1: 没有指定 OpenSSL 库.
内容:
./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl=<path> option.
分析: 启用with-http_ssl_module模块,但没有启用openssl, 启用的SSL模块须要依赖openssl库 解决: configure时增长参数 [with-openssl=../openssl-1.0.2e].
报错2: 参数配置路径出错
参数with-pcre,若是指定的是with-pcre=/usr/local/pcre-8.38,则执行 make 时会报错:
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f objs/Makefile cd /usr/local/pcre-8.38 \ && if [ -f Makefile ]; then /Applications/Xcode.app/Contents/Developer/usr/bin/make distclean; fi \ && CC="cc" CFLAGS="-O2 -pipe " \ ./configure --disable-shared /bin/sh: ./configure: No such file or directory make[1]: *** [/usr/local/pcre-8.38/Makefile] Error 127 make: *** [build] Error 2
分析: set path to PCRE library sources, 注意是PCRE的源代码的路径,不是编译安装后的路径
查看配置帮助: $ ./configure --help | grep 'pcre' --without-pcre disable PCRE library usage --with-pcre force PCRE library usage --with-pcre=DIR set path to PCRE library sources --with-pcre-opt=OPTIONS set additional build options for PCRE --with-pcre-jit build PCRE with JIT compilation support
注意是 PCRE library sources, 是PCRE的源代码。 直接去官网下载PCRE, 解压至与 nginx-1.8.1 平级的目录中
解决: 将PCRE路径指定为源代码的路径,好比:with-pcre=/softwares/pcre-8.38
报错3 : $ sudo ./config --prefix=/usr/local/openssl Operating system: i686-apple-darwinDarwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64 WARNING! If you wish to build 64-bit library, then you have to invoke './Configure darwin64-x86_64-cc' manually. You have about 5 seconds to press Ctrl-C to abort.
解决: 配置命令使用 Configure, 而不是config, 加参数平台参数:darwin64-x86_64-cc $ sudo ./Configure darwin64-x86_64-cc --prefix=/usr/local/openssl 备注: 查看支持的平台列表:$./Configure LIST
报错4:
Undefined symbols for architecture x86_64: "_SSL_CTX_set_alpn_select_cb", referenced from: _ngx_http_ssl_merge_srv_conf in ngx_http_ssl_module.o "_SSL_CTX_set_next_protos_advertised_cb", referenced from: _ngx_http_ssl_merge_srv_conf in ngx_http_ssl_module.o "_SSL_select_next_proto", referenced from: _ngx_http_ssl_alpn_select in ngx_http_ssl_module.o "_X509_check_host", referenced from: _ngx_ssl_check_host in ngx_event_openssl.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[1]: *** [objs/nginx] Error 1 make: *** [build] Error 2
分析: 这个报错在网上有好多种缘由形成的,所以也有许多的解决办的 解决: (1)将openssl-1.0.2e目录中, 文件Makefile中的[darwin-i386-cc]所有替换成[darwin64-x86_64-cc] (2)检测本身的系统是以32位模式运行,仍是以64位模式运行:
(3)要为当前启动磁盘选择 64 位内核,请在“终端”中使用下列命令:sudo systemsetup -setkernelbootarchitecture x86_64
// 可是设置没有生效: $ sudo systemsetup -setkernelbootarchitecture x86_64 Password: setting kernel architecture to: x86_64 changes to kernel architecture failed to save!
(4) 执行配置NGINX命令前,增长 export KERNEL_BITS=64 命令,指定系统的运行模式为64位的。
报错5: 重启NGIXN报错: 内容:
nginx: [error] open() "/usr/local/nginx/nginx.pid" failed (2: No such file or directory)
分析: 解决:使用nginx -c的参数指定nginx.conf文件的位置:
/usr/local/nginx/nginx -c /usr/local/nginx/nginx.conf
注意这里的nginx路径,在编译时有指定,可能像网上的有些不一样,默认应该是:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
检测配置文件是否正确: (出错了,没有找到nginx.conf文件) $ sudo /usr/local/nginx/nginx -t nginx: [emerg] open() "/usr/local/nginx/nginx.conf" failed (2: No such file or directory) nginx: configuration file /usr/local/nginx/nginx.conf test failed 复制配置文件:(复制一份) $sudo cp nginx.conf.default nginx.conf 再次检测: (OK) $ sudo /usr/local/nginx/nginx -t nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/nginx.conf test is successful
$ arch i386 $ uname -a Darwin MacWang.local 15.3.0 Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64 $ ioreg -l -p IODeviceTree | grep "firmware-abi" | sed -e 's/[^0-9A-Z]//g' EFI64
http://nginx.org/en/docs/configure.html
https://www.nginx.com/resources/admin-guide/installing-nginx-open-source/
http://segmentfault.com/a/1190000003822041?_ea=392297
http://lists.apple.com/archives/macnetworkprog/2015/Jun/msg00025.html
http://www.iyunv.com/thread-18789-1-1.html
https://wiki.openssl.org/index.php/Compilation_and_Installation#Mac
http://www.nooidea.com/2011/02/switch-mac-into-64-bit.html