什么是平滑升级?
html
对于一个正在运行中nginx服务器,若是咱们须要对其进行升级到高版本的nginx。也许你会直接把nginx中止,而后完成升级操做,以后再次启动nginx。这种方式的缺点是显而易见的,一些正在处理的请求就会直接丢失了。(有点像狗血片里的XXX:“这日子无法儿过了!”刷东西。。。)强制升级,服务器上运行的进程会受到影响。所以,才须要在不中止nginx的状况下对nginx进行平滑升级。nginx
平滑升级:不会停掉在运行着的进程,这些进程会继续处理请求,但不会再接收请求,这些老进程处理完正在处理的这些请求以后,会中止。(有点像武侠片里的XXX:“等我干完手上这几件事情以后就金盆洗手”)
shell
在平滑升级中,新的进程会来接手处理工做。服务器
下面就是平滑升级的基本步骤:ui
本文环境:Fedora系统 原始nginx版本1.8.1atom
升级目标:nginx平滑升级到1.9.12spa
步骤:unix
1)下载nginx最新的包。本文选择的是nginx-1.9.12.tar.gzcode
2)按照本系列第一篇博客文章中介绍的,对nginx-1.9.12.tar.gz进行解压,configure,make三个步骤。可是,这里千万别make install。由于咱们此次是升级,而不是安装!!!htm
[neil@neilhost Downloads]$ cd ~/Documents/ [neil@neilhost Documents]$ ll 总用量 1800 drwxr-xr-x. 5 neil neil 4096 4月 22 2015 log drwxr-xr-x. 4 neil neil 4096 2月 29 20:17 MyCraft drwxr-xr-x. 9 neil neil 4096 3月 3 11:27 nginx-1.8.1 -rw-r-----. 1 neil neil 833473 3月 3 11:02 nginx-1.8.1.tar.gz -rw-r-----. 1 neil neil 899183 3月 8 16:01 nginx-1.9.12.tar.gz -rw-r--r--. 1 neil neil 81911 3月 3 13:40 nginxbushu.txt drwxr-xr-x. 5 neil neil 4096 5月 23 2015 ProgramAPP drwxr-xr-x. 3 neil neil 4096 6月 5 2015 workspace-sts-3.6.4.RELEASE [neil@neilhost Documents]$ tar -zxvf nginx-1.9.12.tar.gz nginx-1.9.12/ nginx-1.9.12/auto/ nginx-1.9.12/conf/ nginx-1.9.12/contrib/ nginx-1.9.12/src/ nginx-1.9.12/configure nginx-1.9.12/LICENSE ******此处略去XXX个字 nginx-1.9.12/auto/cc/msvc nginx-1.9.12/auto/cc/name nginx-1.9.12/auto/cc/owc nginx-1.9.12/auto/cc/sunc [neil@neilhost Documents]$ cd nginx-1.9.12/ [neil@neilhost nginx-1.9.12]$ ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src [neil@neilhost nginx-1.9.12]$ ./configure checking for OS + Linux 3.17.4-301.fc21.x86_64 x86_64 checking for C compiler ... found + using GNU C compiler + gcc version: 4.9.2 20150212 (Red Hat 4.9.2-6) (GCC) checking for gcc -pipe switch ... found checking for -Wl,-E switch ... found checking for gcc builtin atomic operations ... found checking for C99 variadic macros ... found ******此处略去XXX个字 checking for zlib library ... found creating objs/Makefile Configuration summary + using system PCRE library + OpenSSL library is not used + using builtin md5 code + sha1 library is not found + using system zlib library nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx modules path: "/usr/local/nginx/modules" nginx configuration prefix: "/usr/local/nginx/conf" nginx configuration file: "/usr/local/nginx/conf/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" [neil@neilhost nginx-1.9.12]$ make make -f objs/Makefile make[1]: Entering directory '/home/neil/Documents/nginx-1.9.12' cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/nginx.o \ src/core/nginx.c ******此处略去XXX个字 objs/src/http/modules/ngx_http_upstream_hash_module.o \ objs/src/http/modules/ngx_http_upstream_ip_hash_module.o \ objs/src/http/modules/ngx_http_upstream_least_conn_module.o \ objs/src/http/modules/ngx_http_upstream_keepalive_module.o \ objs/src/http/modules/ngx_http_upstream_zone_module.o \ objs/ngx_modules.o \ -ldl -lpthread -lcrypt -lpcre -lz \ -Wl,-E sed -e "s|%%PREFIX%%|/usr/local/nginx|" \ -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \ -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \ -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \ < man/nginx.8 > objs/nginx.8 make[1]: Leaving directory '/home/neil/Documents/nginx-1.9.12' [neil@neilhost nginx-1.9.12]$ ll 总用量 688 drwxr-xr-x. 6 neil neil 4096 3月 8 16:02 auto -rw-r--r--. 1 neil neil 259440 2月 24 22:53 CHANGES -rw-r--r--. 1 neil neil 395257 2月 24 22:53 CHANGES.ru drwxr-xr-x. 2 neil neil 4096 3月 8 16:02 conf -rwxr-xr-x. 1 neil neil 2481 2月 24 22:53 configure drwxr-xr-x. 4 neil neil 4096 3月 8 16:02 contrib drwxr-xr-x. 2 neil neil 4096 3月 8 16:02 html -rw-r--r--. 1 neil neil 1397 2月 24 22:53 LICENSE -rw-rw-r--. 1 neil neil 332 3月 8 16:03 Makefile drwxr-xr-x. 2 neil neil 4096 3月 8 16:02 man drwxrwxr-x. 3 neil neil 4096 3月 8 16:03 objs -rw-r--r--. 1 neil neil 49 2月 24 22:53 README drwxr-xr-x. 9 neil neil 4096 3月 8 16:02 src [neil@neilhost nginx-1.9.12]$
以后,按照升级的规范咱们须要对现有正在运行的nginx1.8.1的运行程序进行备份。
[neil@neilhost nginx-1.9.12]$ cd /usr/local/nginx/sbin/ [neil@neilhost sbin]$ ls nginx [neil@neilhost sbin]$ sudo cp nginx nginx.old [sudo] password for neil: [neil@neilhost sbin]$ ls nginx nginx.old [neil@neilhost sbin]$
这里,咱们能够先查看一下当前的状态。当前nginx的版本,以及nginx运行的状态:
[neil@neilhost sbin]$ pstree -p | grep nginx |-nginx(8562)---nginx(8563) [neil@neilhost sbin]$ sudo ./nginx -V nginx version: nginx/1.8.1 built by gcc 4.9.2 20150212 (Red Hat 4.9.2-6) (GCC) configure arguments: [neil@neilhost sbin]$
以后,咱们就开始平滑升级。咱们将下载、解压、configure、make好以后的nginx1.9.2的文件夹中的objs目录下的nginx程序复制并替换当前正在运行的nginx的sbin目录下的1.8.1版本的nginx。
[neil@neilhost objs]$ cd /usr/local/nginx/sbin/ [neil@neilhost sbin]$ ls nginx nginx.old [neil@neilhost sbin]$ sudo ./nginx -V nginx version: nginx/1.9.12 built by gcc 4.9.2 20150212 (Red Hat 4.9.2-6) (GCC) configure arguments: [neil@neilhost sbin]$ pstree -p | grep nginx |-nginx(8562)---nginx(8563) [neil@neilhost sbin]$
至此,咱们的nginx平滑升级就完成了。咱们发现,nginx的服务没有中止,可是此时nginx的版本已经升级到了1.9.12的版本。同时nginx的主进程和服务进程也没有所以改变ID。总体的过程是平滑的。而且,咱们还将老版本1.8.1的nginx进行备份,即nginx.old。
最后,若是你是个硬盘洁癖,就把那些下载的和make的代码包删除吧。
[neil@neilhost sbin]$ rm -f ~/Documents/nginx-1.9.12.tar.gz [neil@neilhost sbin]$ rm -rf ~/Documents/nginx-1.9.12/