[Nginx]使用源碼編譯安裝Nginx服務器

Compiling and Installing From the Sourcesphp

Update 2016/01/28: Updated for nginx-1.8.1 And openssl-1.0.2e.html

最近使用源碼安裝Nginx,遇到了一些問題,特此記錄下來。nginx

1 安裝前準備:

檢測系統版本: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

2 安裝nginx的依賴庫

安裝pcre:

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

安裝zlib:

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

安裝openssl:

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

3 安裝Nginx

下載並安裝Nginx:

$ 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庫路徑,正則表達式.

4 啓動Nginx:

查看版本:

$ /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

5 修改配置並重啓服務:

配置文件路徑:

/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;
}

從新啓動Nginx:

// 測試配置文件
$ 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

相關文章
相關標籤/搜索