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

系统基于ubuntu server 14.04.4 amd64

安装

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

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

tar zxvf nginx-1.10.1.tar.gz

第二步 配置

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

注意:

① 如果之前没有安装C compiler(C 编译器),这一步将报如下错误信息:

[email protected]:~/download/nginx-1.10.1$ ./configure –prefix=/usr/local/nginx
checking for OS
+ Linux 4.2.0-27-generic x86_64
checking for C compiler … not found

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

[email protected]:~/download/nginx-1.10.1$

可以参考这篇文章安装C compiler,然后继续下面的操作

② 如果之前没有安装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= option.

[email protected]:~/download/nginx-1.10.1$

可以参考这篇文章安装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= option.

[email protected]:~/download/nginx-1.10.1$

可以参考这篇文章安装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的安装部分的第一、二步。

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

因为只是对Nginx进行升级,并不涉及配置参数的修改,所以,我们一般使用和旧版本相同的配置(当然你也可以使用全新的配置信息),来编译新版本的Nginx,使用如下命令查看旧版本配置信息:

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

可以得到结果如下:

[email protected]:~/download/nginx-1.11.1$ /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
[email protected]:~/download/nginx-1.11.1$

其中 [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

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

[email protected]:~/download/nginx-1.11.1$ sudo cp objs/nginx /usr/local/nginx/sbin/nginx
cp: cannot create regular file ‘/usr/local/nginx/sbin/nginx’: Text file busy
[email protected]:~/download/nginx-1.11.1$

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

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

结果:

[email protected]:~$ /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.11.1
[email protected]:~$

或者:

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

结果:

[email protected]:~$ /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
[email protected]:~$

这里可以看出,正在运行的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,完成动态模块添加

这一步可以参考平滑升级的第四至八步

时间: 2024-08-02 02:46:10

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

centos 7下nginx搭建流媒体服务器【动态添加模块】

1.安装nginx依赖包 yum install gcc gcc-c++ openssl-devel zlib-devel pcre pcre-devel yamdi 2.下载解压nginx_mod_h264_streaming,让nginx支持flv,mp4流播放 wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz 解压后需要修改src目录下的ngx_http_streaming_modul

linux下nginx的安装

以Red Hat Enterprise Linux 5为例进行讲解. 相关系列: linux下jdk的安装 linux下ant的安装 linux下redis的安装 linux下svn的安装 linux下nginx的安装 linux下graphviz的安装 linux下doxygen的安装 安装nginx版本为0.8.36 一.下载nginx 下载地址:http://www.nginx.org/ 选择nginx-0.8.36 将该下载包拷贝到/usr/local/下(随意了,找个地方就好) 二.安

Linux下nginx编译安装教程和编译参数详解

这篇文章主要介绍了Linux下nginx编译安装教程和编译参数详解,需要的朋友可以参考下 一.必要软件准备1.安装pcre 为了支持rewrite功能,我们需要安装pcre 复制代码代码如下: # yum install pcre* //如过你已经装了,请跳过这一步 2.安装openssl 需要ssl的支持,如果不需要ssl支持,请跳过这一步 复制代码代码如下: # yum install openssl* 3.gzip 类库安装 复制代码代码如下: yum install zlib zlib-

Linux下Nginx的安装步骤

一.下载pcre 官网下载:http://www.pcre.org/ # wget http://sourceforge.net/projects/pcre/files/pcre/8.35/pcre-8.35.tar.gz/download # cd /pcre-8.35 二.下载purge模块(用于删除Nginx缓存) # wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz # tar zxvf ngx_cache_pur

centOS linux 下nginx编译安装详解

Nginx的官方网站是 www.nginx.org Nginx的下载地址是:http://nginx.org/en/download.html 由 于官网的设计非常简洁不大气不上档次,所以我们可以很容易的找到我们需要的内容.打开页面发现有三个版本,分别是Mainline version(开发版).Stable version(稳定版).Legact version(历史稳定版).在这里我们下载最新的稳定版本nginx-1.6.2. 在安装Nginx之前,我们要确保系统已经安装了gcc,opens

Linux 下Nginx 的安装及负载均衡的简单配置

这次发布程序需要均衡负载,网上看了一下这方便的东西,觉得很不错,学完之后做下总结,一遍后期用到. 1.安装nginx之前需要安装的两个依赖,pcre-x.x.x.tar.gz 和pcre-devel-x.x.x.rpm这两个包(具体这两个有什么用处也没仔细研究过,不安装确实再安装nginx时失败) 1.1安装1.安装pcre-x.x.x.tar tar zxvf pcre-x.x.x.tar.gz cd pcre-x.x.x ./configure make && make install

linux下nginx编译安装(抄别人的,方便查看)

原路径:https://blog.csdn.net/youcijibi/article/details/75050993 正式开始前,编译环境gcc g++ 开发库之类的需要提前装好. 如果是ububtu平台初始安装编译安装则使用如下指令: apt-get install build-essential apt-get installlibtool如果是centos则如下:当没有make时: 安装make: yum -y install gcc automake autoconf libtool

Linux下Nginx web服务器的实现及功能模块指令详解

Nginx (engine x)是一个高性能的HTTP和反向代理服务器,也是一款轻量级的Web 服务器 关于http协议的相关概念: URL统一资源定位符的形式: shceme://username:[email protected]:port/path;params?query#frag http事务:一次请求和一次响应构成一次事务 request请求格式:         <method><URL><VERSION>         HEADERS         

linux下nginx安装

概述:Nginx是一个轻便的支持高并发的HTTP和反向代理服务器,运用很广.这里记录下Linux下Nginx的安装步骤,这里以centos系统为例,安装Nginx1.7.9,安装目录为/usr/local. 一.安装Nginx依赖环境 1.安装gcc-c++ # yum install -y gcc-c++ 2.下载pcre #cd /usr/local #wget http://sourceforge.net/projects/pcre/files/pcre/8.35/pcre-8.35.ta