Linux下Nginx的安装、升级及动态添加模块

原文连接:http://xueliang.org/article/detail/20160615172540639html

系统基于ubuntu server 14.04.4 amd64nginx

安装

第一步 下载并解压Nginx压缩包

Nginx官网下载Nginx,或者在Linux上执行wget http://nginx.org/download/nginx-1.10.1.tar.gz命令直接下载
解压nginx-1.10.1.tar.gz文件:ubuntu

tar zxvf nginx-1.10.1.tar.gz

第二步 配置

cd nginx-1.10.1
./configure --prefix=/usr/local/nginx

注意:
若是以前没有安装C compiler(C 编译器),这一步将报以下错误信息:segmentfault

xueliang@dev:~/download/nginx-1.10.1$ ./configure --prefix=/usr/local/nginx
checking for OSui

    • Linux 4.2.0-27-generic x86_64code

    1. for C compiler ... not foundserver

    ./configure: error: C compiler cc is not foundhtm

    xueliang@dev:~/download/nginx-1.10.1$进程

    能够参考这篇文章安装C compiler,而后继续下面的操做ip

    若是以前没有安装PCRE,这一步将报以下错误信息:

    checking for PCRE library ... not found

    checking for PCRE library in /usr/local/ ... not found
    checking for PCRE library in /usr/include/pcre/ ... not found
    checking for PCRE library in /usr/pkg/ ... not found
    checking for PCRE library in /opt/local/ ... not found

    ./configure: error: the HTTP rewrite module requires the PCRE library.

    You can either disable the module by using --without-http_rewrite_module
    option, or install the PCRE library into the system, or build the PCRE library
    statically from the source with nginx by using --with-pcre=<path> option.

    xueliang@dev:~/download/nginx-1.10.1&dollar;

    能够参考这篇文章安装PCRE,而后继续下面的操做

    若是以前没有安装zlib,这一步将报以下错误信息:

    checking for md5 in system md library ... not found
    checking for md5 in system md5 library ... not found
    checking for md5 in system OpenSSL crypto library ... not found
    checking for sha1 in system md library ... not found
    checking for sha1 in system OpenSSL crypto library ... not found
    checking for zlib library ... not found

    ./configure: error: the HTTP gzip module requires the zlib library.
    You can either disable the module by using --without-http_gzip_module
    option, or install the zlib library into the system, or build the zlib library
    statically from the source with nginx by using --with-zlib=<path> option.

    xueliang@dev:~/download/nginx-1.10.1&dollar;

    能够参考这篇文章安装zlib,而后继续下面的操做

    也能够跳过此步,执行默认安装,--prefix的默认值为/usr/local/nginx,Nginx官网对此有说明:Building nginx from Sources

    第三步 编译

    make

    第四步 完成安装

    sudo make install

    平滑升级

    当须要对正在运行的Nginx进行升级时,能够在不中止Nginx的状况下,使用新版本或者重编译的可执行程序替换旧版本的可执行程序,这里咱们从nginx-1.10.1升级到nginx-1.11.1

    第一步 备份旧版本

    由于Nginx的升级,实质只是用新版本的可执行文件,替换旧版本的可执行程序,因此,对于备份,既能够只备份旧版本可执行文件,也能够打包备份整个旧版本安装目录,参考命令分别以下:
    只备份旧版本可执行文件

    sudo cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

    打包备份整个旧版本安装目录

    sudo tar -cvf /usr/local/nginx.bak /usr/local/nginx

    第二步 下载新版本并解压Nginx压缩包

    对于新版本Nginx压缩包的下载和解压,能够参考本文关于Nginx的安装部分的第1、二步。

    第三步 使用旧版本配置参数,配置并编译新版本Nginx

    由于只是对Nginx进行升级,并不涉及配置参数的修改,因此,咱们通常使用和旧版本相同的配置(固然你也可使用全新的配置信息),来编译新版本的Nginx,使用以下命令查看旧版本配置信息:

    /usr/local/nginx/sbin/nginx -V

    能够获得结果以下:

    xueliang@dev:~/download/nginx-1.11.1&dollar; /usr/local/nginx/sbin/nginx -V
    nginx version: nginx/1.10.1
    built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
    configure arguments: --prefix=/usr/local/nginx
    xueliang@dev:~/download/nginx-1.11.1&dollar;

    其中 [configure arguments: --prefix=/usr/local/nginx] 这一行即为旧版本Nginx配置信息,这里能够看出,旧版本只是指定了安装路径,使用[configure arguments:]后面的参数信息,对新版本Nginx做相同配置,而后进行编译:

    ./configure --prefix=/usr/local/nginx

    第四步 编译新版本Nginx可执行程序

    make

    第五步 用新版本Nginx可执行程序覆盖旧版本可执行程序

    在上一步的基础上,执行一下命令便可:

    sudo cp objs/nginx /usr/local/nginx/sbin/nginx

    执行这条命令,可能会报如下异常,提示文件被占用:

    xueliang@dev:~/download/nginx-1.11.1&dollar; sudo cp objs/nginx /usr/local/nginx/sbin/nginx
    cp: cannot create regular file ‘/usr/local/nginx/sbin/nginx’: Text file busy
    xueliang@dev:~/download/nginx-1.11.1&dollar;

    可使用如下命令进行强制覆盖:

    sudo cp -rfp objs/nginx /usr/local/nginx/sbin/nginx

    第六步 启动新版本Nginx主进程

    发送 USR2信号给旧版本主进程号:

    kill -USR2 旧版本的Nginx主进程号

    旧版本Nginx主进程接收到-USR2信号,将重命名它的.pid文件为.oldpid,而后执行新版本的Nginx可执行程序,依次启动新版本的主进程和工做进程:

    PID PPID USER %CPU VSZ WCHAN COMMAND
    33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx
    33134 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)
    33135 33126 nobody 0.0 1380 kqread nginx: worker process (nginx)
    33136 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)
    36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx
    36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
    36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
    36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)

    第七步 从容关闭旧版本的工做进程

    此时,新、旧版本的Nginx实例会同时运行,共同处理请求,若是此时给旧版本主进程发送WINCH 信号,旧版本主进程将会给它的工做进程发送消息,请求它们从容关闭,此后,旧版本的工做进程开始逐步退出:

    PID PPID USER %CPU VSZ WCHAN COMMAND
    33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx
    33135 33126 nobody 0.0 1380 kqread nginx: worker process is shutting down (nginx)
    36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx
    36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
    36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
    36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)

    从容关闭旧版本的工做进程命令:

    kill -WINCH 旧版本的Nginx主进程号

    第八步 从容关闭旧版本的主进程,完成Nginx的升级

    通过一段时间后,旧的工做进程(work process)处理完了全部已链接的请求后退出,仅由新版本的工做进程来处理新的请求了:

    PID PPID USER %CPU VSZ WCHAN COMMAND
    33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx
    36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx
    36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
    36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
    36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)

    应该注意的是,此时,旧版本的主进程还还没有关闭它监听的套接字,若是有须要,你仍能够恢复旧版本工做进程。若是因为某些缘由,新版本的可执行文件运行状况不理想,下面有几种方案可供参考:

    • 给旧版本主进程发送 HUP 信号。旧版本主进程将在不从新读取配置信息的状况下,从新开启工做进程。而后,经过给新版本主进程发送 QUIT 信号,全部新版本的进程将会从容关闭。

    • 给新版本主进程发送 TERM 信号。而后,他将会给它的工做进程发送消息,要求它们当即退出,紧接着,这些工做进程就会当即退出。(若是由于某些缘由,新版本进程没有退出,应该给新版本主进程发送 KILL 信号,强制新版本主进程退出。)新版本主进程退出的同时,旧版本主进程将会自动启动它的工做进程。
      新版本主进程退出后,旧版本主进程将会移除名字以.oldpid 结尾的文件,恢复为它的 .pid 文件。

    若是升级成功,应该给旧版本主进程发送 QUIT 信号,使其退出,只保留新版本进程:

    PID PPID USER %CPU VSZ WCHAN COMMAND
    36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx
    36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
    36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
    36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)

    添加模块

    刚接触Nginx时,只知道Nginx的功能是分模块的,并不清楚有些模块默认是不参与到构建中去的,好比ngx_http_ssl_module模块,是用来支持https协议的,默认状况下是没有构建到Nginx中的。
    随着业务不断扩展,若是须要Nginx支持某些模块,而这些模块默认不在Nginx的构建计划中,构建Nginx时,又没有指定加入这些模块,该怎么办呢?是否可以给已经运行的Nginx动态添加这些模块呢?答案是确定的!
    给运行中的Nginx动态添加模块的方案,与上面提到的平滑升级Nginx的方案很相似。下面咱们来看一下如何给运行中的Nginx添加 ngx_http_ssl_module 模块。

    第一步 查看运行中的Nginx版本,并下载、解压对应版本的压缩包

    查看Nginx版本:

    /usr/local/nginx/sbin/nginx -v

    结果:

    xueliang@dev:~&dollar; /usr/local/nginx/sbin/nginx -v

    nginx version: nginx/1.11.1
    xueliang@dev:~&dollar;

    或者:

    /usr/local/nginx/sbin/nginx -V

    结果:

    xueliang@dev:~&dollar; /usr/local/nginx/sbin/nginx -V

    nginx version: nginx/1.11.1
    built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
    configure arguments: --prefix=/usr/local/nginx
    xueliang@dev:~&dollar;

    这里能够看出,正在运行的Nginx版本为1.11.1,参照安装Nginx部分,下载并解压对应版本的Nginx

    第二步 编译Nginx,同时加入须要模块配置

    参考平滑升级的第三步,查看运行中的Nginx的配置参数,并在最后追加 -with-http_ssl_module
    如:原配置信息为 --prefix=/usr/local/nginx,则新配置信息为 --prefix=/usr/local/nginx --with_http_ssl_module,配置Nginx执行的命令以下:

    ./configure --prefix=/usr/local/nginx --with_http_ssl_module

    第三步 平滑重启Nginx,完成动态模块添加

    这一步能够参考平滑升级的第四至八步

    相关文章
    相关标签/搜索