Linux(Ubuntu)下升級Nginx服務器軟件至nginx-1.17.6

Linux下升級Nginx服務器軟件nginx-1.17.6

Nginx (音engine X)是一款是由俄羅斯的程序設計師Igor Sysoev所開發高性能的 Web和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。

根據Netcraft的數據,Nginx在2019年11月爲25.79%最繁忙的網站提供服務或代理服務。

目前 Nginx 最新版本是nginx-1.17.6,2019年11月19日發佈。

2019-11-19 nginx-1.17.6 mainline version has been released.
2019-05-21 nginx-1.17.0 mainline version has been released.
2019-03-26 nginx-1.15.10 mainline version has been released.
2017-12-26 nginx-1.13.8 mainline version has been released.
2016-12-27 nginx-1.11.8 mainline version has been released.
2015-12-09 nginx-1.9.9 mainline version has been released.
2014-12-23 nginx-1.7.9 mainline version has been released.
2013-12-17 nginx-1.5.8 mainline version has been released.
2012-12-11 nginx-1.2.6 stable version has been released.
2011-04-12 nginx-1.0.0 stable version has been released.

現在,我們來在Ubuntu機器上,使用源碼安裝Nginx,特此記錄如下。

1 安裝前準備:

查看系統版本:

(1) 使用 uname 命令查看系統系統

$ uname -rmsov
Linux 4.4.0-146-generic 
 #172-Ubuntu SMP Wed Apr 3 09:00:08 UTC 2019 x86_64 GNU/Linux

(2) 去 proc 目錄下查看 version 文件

$ cat /proc/version
Linux version 4.4.0-146-generic (buildd@lcy01-amd64-025) 
 (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10) ) 
 #172-Ubuntu SMP Wed Apr 3 09:00:08 UTC 2019

(3) 使用 lsb_release 命令查看

$ lsb_release -a
LSB Version: core-9.20160110ubuntu0.2-amd64
Distributor ID:	Ubuntu
Description: Ubuntu 16.04.6 LTS
Release: 16.04
Codename: xenial

創建安裝目錄:

爲了方便管理,創建 /usr/local/webserver 目錄。
以後所有的軟件安裝都可以加上 —prefix=/usr/loca/webserver,安裝到這個目錄下:

$ cd /usr/local/
$ 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:

$ cd ~
$ wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
$ tar zxf pcre-8.43.tar.gz

Nginx網站說明要下載的 PCRE 庫版本爲 4.4 — 8.43,而不是 PCRE2 。

PCRE: 是 “Perl Compatible Regular Expressions”,即“Perl兼容正則表達式”。

下載並解壓 zlib:

the zlib library – required by NGINX Gzip module for headers compression:

$ cd ~
$ wget http://zlib.net/zlib-1.2.11.tar.gz
$ tar zxvf zlib-1.2.11.tar.gz

zlib 是提供數據壓縮用的函式庫,由 Jean-loup Gailly 與 Mark Adler 所開發。

下載並解壓 openssl:

the OpenSSL library – required by NGINX SSL modules to support the HTTPS protocol:

$ cd ~
$ wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz
$ tar -zxf openssl-1.1.1c.tar.gz

OpenSSL 是一個著名的安全通信類庫,如果需要配置 SSL/TLS 協議(Https),需要安裝此類庫。其網站是: https://www.openssl.org

3 安裝 Nginx

下載並安裝 Nginx:

爲了不與原來的nginx目錄衝突,本次安裝目錄制定到 /usr/local/nginx-1.17.6

$ cd ~
$ wget http://nginx.org/download/nginx-1.17.6.tar.gz
$ tar zxf nginx-1.17.6.tar.gz
$ cd nginx-1.17.6/
$ ./configure --prefix=/usr/local/nginx-1.17.6 \
    --sbin-path=/usr/local/nginx-1.17.6/nginx \
    --conf-path=/usr/local/nginx-1.17.6/nginx.conf \
    --pid-path=/usr/local/nginx-1.17.6/nginx.pid \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-openssl=../openssl-1.1.1d \
    --with-pcre=../pcre-8.43 \
    --with-zlib=../zlib-1.2.11
$ sudo make 
$ sudo make install

參數說明:

參數 --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/webserver/nginx/nginx -v
nginx version: nginx/1.10.2

新安裝的 nginx-1.17.6:

$ /usr/local/nginx-1.17.6/nginx -v
nginx version: nginx/1.17.6

使用 nginx -t 測試配置文件是否正確:

$ /usr/local/nginx-1.17.6/nginx -t
nginx: the configuration file /usr/local/nginx-1.17.6/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.17.6/nginx.conf test is successful

啓動 nginx-1.17.6:

$ /usr/local/nginx-1.17.6/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

提示啓動失敗,因爲端口80已經被佔用(因爲機器原nginx正在運行中)。

先看一下默認的配置文件 nginx.conf:

# 文件路徑: /usr/local/nginx-1.17.6/nginx.conf 
#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
        # error_page  404  /404.html;
        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html { 
            root   html;
        }
    }
}

那我們修改一下監聽端口號,將默認監聽80改成其他未佔用的端口,比如8081

vi /usr/local/nginx-1.17.6/nginx.conf 

修復監聽端口爲 8082

# <---- 修改 server 部分 
server {
    #listen       80;
    listen       8081; # <-- 換一個未佔用的端口號,比如8081
    server_name  localhost;
    # ....
}

執行啓動命令:(先測試配置文件,再執行啓動)

$ /usr/local/nginx-1.17.6/nginx -t 
$ /usr/local/nginx-1.17.6/nginx

驗證服務是否啓動

(1)通過瀏覽器查看:

如果是安裝在本地,可以直接打開瀏覽器,輸入:http://127.0.0.1/, 可以看到:"Welcome to nginx! " 頁面。

如果是遠程服務器(沒有界面),可以直接使用 ip 訪問。比如訪問: http://106.14.34.47:8082

(2)通過curl命令查看:

如果沒有修改端口號,可以直接使用 curl 命令查看:

$ 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

如果修改了端口號,使用curl請求,帶上端口號:

$ curl -I 127.0.0.1:8081
HTTP/1.1 200 OK
Server: nginx/1.17.6
Date: Thu, 28 Nov 2019 09:04:07 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 28 Nov 2019 08:29:01 GMT
Connection: keep-alive
ETag: "5ddf854d-264"
Accept-Ranges: bytes

(2) 查看nginx進程

$ ps -ef |grep nginx

5 修改配置並重啓服務:

複製原nginx配置文件到:

$ mkdir /usr/local/nginx-1.17.6/conf.d
$ cp /usr/local/webserver/nginx-1.13.6/nginx.conf /usr/local/nginx-1.17.6/nginx.conf
$ cp /usr/local/webserver/nginx-1.13.6/conf.d/* /usr/local/nginx-1.17.6/conf.d/
include /usr/local/nginx-1.17.6/conf.d/*.conf;

如果是新安裝的,可以參考默認配置 nginx.conf.default

關閉 nginx : 直接結束nginx進程即可。

# 查看進程
$ ps -ef |grep nginx

# 也可以直接查看 nginx.pid 文件
$ cat nginx.pid 

# 結束進程
$ kill -9 22661

配置文件路徑:

/usr/local/nginx/nginx.conf

配置文件 nginx.conf 中的默認的代碼根文件配置是 root html;

/usr/local/nginx-1.17.6/html 目錄。

$ ls -l /usr/local/nginx-1.17.6/html
total 8
-rw-r--r-- 1 root root 494 Nov 28 16:29 50x.html
-rw-r--r-- 1 root root 612 Nov 28 16:29 index.html

比如我們的代碼存放在 /webroot/laravel-test,需要修改 root 配置。

修改根目錄:

# 默認的項目根目錄:
# 默認的是html目錄,即/usr/local/nginx/html/
location / {
    root   html;
    index  index.html index.htm;
}

# 修改項目根目錄:
location / {
    root   /webroot/laravel-test/public;
    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的源代碼的路徑,不是編譯安裝後的路徑。
PCRE 不用單獨編譯安裝,直接指定路徑,nginx 自己來編譯安裝。

查看配置幫助:
$  ./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 同級的目錄中即可。

解決: 將PCRE路徑指定爲源代碼的路徑,比如:with-pcre=/softwares/pcre-8.38

報錯3 : MAC 環境變編譯報錯 (openssl)

$ 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: MAC 環境變編譯報錯 (openssl)

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

分析: 這個報錯在網上有好多種原因造成的,因此也有許多的解決辦的。 (這是使用Mac之前遇到的錯誤)
解決:
(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 來顯示當前主機的硬件架構類型。

# mac mini
$ arch
i386

# aliyun ubuntu 16
$ arch
x86_64

參考:

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

更新記錄

2016-01-28: Updated for nginx-1.8.1 And openssl-1.0.2e.
2017-07-01: 新增http_v2_module模塊
2018-05-07: 升級到nginx-1.14.0.
2019-05-31: 升級到nginx-1.17.0, openssl-1.1.1c.
2019-11-28: 系統更新至Ubuntu 16.04.6 LTS,nginx更新至nginx-1.17.6.

[END]

發佈了47 篇原創文章 · 獲贊 19 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章