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 编译器),这一步将报如下错误信息:
xueliang@dev:~/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
xueliang@dev:~/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.xueliang@dev:~/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.xueliang@dev:~/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
可以得到结果如下:
xueliang@dev:~/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
xueliang@dev:~/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
执行这条命令,可能会报以下异常,提示文件被占用:
xueliang@dev:~/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
xueliang@dev:~/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
结果:
xueliang@dev:~$ /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.11.1
xueliang@dev:~$
或者:
/usr/local/nginx/sbin/nginx -V
结果:
xueliang@dev:~$ /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:~$
这里可以看出,正在运行的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,完成动态模块添加
这一步可以参考平滑升级的第四至八步
Linux下Nginx的安装、升级及动态添加模块的更多相关文章
- centos 7下nginx搭建流媒体服务器【动态添加模块】
1.安装nginx依赖包 yum install gcc gcc-c++ openssl-devel zlib-devel pcre pcre-devel yamdi 2.下载解压nginx_mod_ ...
- Linux下nginx编译安装教程和编译参数详解
这篇文章主要介绍了Linux下nginx编译安装教程和编译参数详解,需要的朋友可以参考下 一.必要软件准备1.安装pcre 为了支持rewrite功能,我们需要安装pcre 复制代码代码如下: # y ...
- Linux下,Nginx的安装、升级及动态添加模块
系统基于ubuntu server 14.04.4 amd64 安装 第一步 下载并解压Nginx压缩包 从Nginx官网下载Nginx,或者在Linux上执行wget http://nginx.or ...
- Linux下Nginx的安装与配置
安装前需要安装pcre:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/1.解压缩: tar xjpf pcre-7.8.tar.b ...
- Linux下Nginx的安装(二)
一.安装Nginx ## 安装前准备 ## #GNU编译器集合 #Nginx编译需要PCRE #在Nginx的各种模块中需要使用gzip压缩 #在Nginx中,如果服务器提供安全网页时则会用到Open ...
- linux下nginx的安装和配置
准备目录 [root@sqh ~]# mkdir /usr/local/nginx [root@sqh ~]# cd /usr/local/nginx 添加一些支持和依赖 1.安装gcc 安装redi ...
- centOS linux 下nginx编译安装详解
Nginx的官方网站是 www.nginx.org Nginx的下载地址是:http://nginx.org/en/download.html 由 于官网的设计非常简洁不大气不上档次,所以我们 ...
- Linux 下Nginx编译安装
Untitled .note-content {font-family: 'Helvetica Neue', Arial, 'Hiragino Sans GB', STHeiti, 'Microsof ...
- linux下nginx的安装及配置
一.安装nginx前,我们首先要确保系统安装了g++.gcc.openssl-devel.pcre-devel和zlib-devel软件,可通过如图所示命令进行检测,如果以安装我们可以通过图二所示卸载 ...
随机推荐
- [C#]async/Await 使用小计
如果指定使用 异步 或 异步 修饰符,方法是异步方法,可以实现以下两个函数. • 清单异步方法可以使用 Await 或指定的 等待 悬挂点. 等待运算符通知编译器异步方法不能继续点的过去,直到等待 ...
- H5小内容(一)
HTML5目前最新的规范(标准)是2014年10月推出 2005年左右出现HTML5版本(非标准) W3C组织(两个组织定义H5规范) 学习(研究)HTML5是学习未来(将来主流) ...
- html锚点
ID模式 <h3><a href="#start">开始</a></h3> <div> 你好 <b/> &l ...
- hibernate配置文件详细解释
<!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式--> <?xml version='1.0' ...
- 关于 jquery.showLoading 中 出现的 图标不在页面中间的问题
很多人喜欢 showLoading 因为 这个实在是太简单了直接 showLoading() hideLoading() 就可以解决这个问题. 今天我们就来看一下 这个插件里面的一个错误 或者说 ...
- RSA算法原理及实现
参考资料: 阮哥的日志:http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html http://www.ruanyifeng ...
- Docker入门
-----------------------------------------Docker入门教程(一)介绍Docker入门教程(二)命令Docker入门教程(三)DockerFileDocker ...
- iphone 屏幕投射到Mac上
在实际的工作中,我们往往需要演示iPhone上面的程序,但是由于手机屏幕太小,无法同时给很多人看,这时候就需要进行屏幕投射.目前我需要实现的是投射到Mac上.我使用有线USB和无线Airplay两种方 ...
- Android Camera 预览图像被拉伸变形的解决方法【转】
问题描述: 预览图像被拉伸变形 问题原因: 由于预览图像大小跟SurfaceView 大小不一致引起 解决方法: 获取系统支持的所有预览尺寸[getSupportedPictureSizes],然后再 ...
- jquery mobile backbone
http://www.appliness.com/getting-started-with-html-mobile-application-development-using-jquery-mobil ...