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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章