免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5617 | 回复: 13

[Web] [实战]在不停止服务, 不丢失连接情况下更新NGINX到新版本(0.7.14 -> 0.7.17) [复制链接]

论坛徽章:
0
发表于 2008-10-08 13:32 |显示全部楼层
参考:http://wiki.codemongers.com/NginxCommandLine#utnbotf


一,准备工作:

备份nginx的二进制文件

  1. mv /usr/local/webserver/nginx/sbin/nginx /usr/local/webserver/nginx/sbin/nginx.old
复制代码

下载, 编译得到新版本的nginx
  1. wget http://sysoev.ru/nginx/nginx-0.7.17.tar.gz
  2. tar xzvf nginx-0.7.17.tar.gz
  3. cd nginx-0.7.17  
  4. ./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with- http_stub_status_module --with-http_ssl_module
  5. 根据需求配置...
  6. make
  7. cp objs/nginx /usr/local/webserver/nginx/sbin/
复制代码

将新版本的nginx可执行文件复制到相应目录


  1. cd /usr/local/webserver/nginx/sbin/
  2. 查看
  3. [root@main sbin]# ./nginx -V
  4. nginx version: nginx/0.7.17
  5. built by gcc 4.1.1 20070105 (Red Hat 4.1.1-52)
  6. configure arguments: --user=www --group=www --prefix=/usr/local/webserver/nginx --with- http_stub_status_module --with-http_ssl_module
  7. [root@main sbin]# ./nginx.old -V
  8. nginx version: nginx/0.7.14
  9. built by gcc 4.1.1 20070105 (Red Hat 4.1.1-52)
  10. configure arguments: --user=www --group=www --prefix=/usr/local/webserver/nginx --with- http_stub_status_module --with-http_ssl_module
复制代码


二,启动新版本nginx

  1. [root@main nginx]# kill -USR2 `cat /usr/local/webserver/nginx/nginx.pid`
复制代码

将发送USR2信号给nginx主进程. 其行为是将nginx.pid改成nginx.pid.oldbin, 然后启动新版本的nginx
  1. [root@main nginx]# ls /usr/local/webserver/nginx/
  2. client_body_temp  fastcgi_temp  logs       nginx.pid.oldbin  sbin
  3. conf              html          nginx.pid  proxy_temp

  4. [root@main nginx]# ps aux | grep nginx
  5. root     18749  0.0  0.2   5168  1152 ?        Ss   10:44   0:00 nginx: master process  /usr/local/webserver/nginx/sbin/nginx
  6. www      18829  0.0  2.0  14516 10652 ?        S    10:46   0:00 nginx: worker process                 
  7. www      18830  0.0  1.9  14580 10276 ?        S    10:46   0:00 nginx: worker process                 
  8. www      18831  0.0  1.9  14580 10276 ?        S    10:46   0:00 nginx: worker process                 
  9. www      18832  0.0  2.1  14908 10956 ?        S    10:46   0:00 nginx: worker process                 
  10. www      18833  0.0  1.9  14580 10276 ?        S    10:46   0:00 nginx: worker process                 
  11. www      18834  0.0  1.9  14580 10276 ?        S    10:46   0:00 nginx: worker process                 
  12. www      18835  0.0  1.9  14580 10276 ?        S    10:46   0:00 nginx: worker process                 
  13. www      18836  0.0  1.9  14580 10276 ?        S    10:46   0:00 nginx: worker process                 
  14. root     23949  0.0  0.3   5100  1800 ?        S    11:34   0:00 nginx: master process  /usr/local/webserver/nginx/sbin/nginx
  15. www      23950  0.0  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  16. www      23951  0.1  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  17. www      23952  0.1  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  18. www      23953  0.1  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  19. www      23954  0.1  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  20. www      23955  0.1  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  21. www      23958  0.1  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  22. www      23959  0.1  1.9  14512 10196 ?        S    11:34   0:00 nginx: worker process
复制代码

这时新版本与旧版本的nginx同时运行

三,关闭旧版本nignx的工作进程

  1. [root@main nginx]# kill -WINCH 18749
复制代码

将发送WINCH信号给旧版本的nginx的主进程, 其行为是优雅地关闭旧版本nginx的工作进程
一段时间后:

  1. [root@main nginx]# ps aux | grep nginx
  2. root     18749  0.0  0.2   5168  1152 ?        Ss   10:44   0:00 nginx: master process  /usr/local/webserver/nginx/sbin/nginx
  3. root     23949  0.0  0.3   5100  1800 ?        S    11:34   0:00 nginx: master process  /usr/local/webserver/nginx/sbin/nginx
  4. www      23950  0.0  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  5. www      23951  0.0  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  6. www      23952  0.0  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  7. www      23953  0.0  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  8. www      23954  0.0  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  9. www      23955  0.0  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  10. www      23958  0.0  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  11. www      23959  0.0  1.9  14512 10196 ?        S    11:34   0:00 nginx: worker process
复制代码
此时旧版本nginx的主进程依然存在


四,
A.如果测试成功, 关闭旧版本nginx的主进程

  1. [root@main nginx]# kill -QUIT 18749
复制代码
  1. [root@main nginx]# ps aux | grep nginx
  2. root     23949  0.0  0.3   5100  1800 ?        S    11:34   0:00 nginx: master process  /usr/local/webserver/nginx/sbin/nginx
  3. www      23950  0.0  2.0  14512 10604 ?        S    11:34   0:00 nginx: worker process                 
  4. www      23951  0.0  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  5. www      23952  0.0  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  6. www      23953  0.0  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  7. www      23954  0.0  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  8. www      23955  0.0  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  9. www      23958  0.0  1.9  14512 10208 ?        S    11:34   0:00 nginx: worker process                 
  10. www      23959  0.0  1.9  14512 10196 ?        S    11:34   0:00 nginx: worker process                 
  11. root     24710  0.0  0.1   3884   668 pts/2    R+   12:00   0:00 grep nginx
复制代码

随着旧版本nginx主进程的关闭, nginx.pid.oldbin也没有了.

  1. [root@main nginx]# ls
  2. client_body_temp  conf  fastcgi_temp  html  logs  nginx.pid  proxy_temp  sbin
复制代码

删除旧版本nginx二进制文件

  1. [root@main nginx]# rm sbin/nginx.old

复制代码


B.如果新版本测试失败, 这时还可以复原, 继续使用旧版本nginx

  1. kill -HUP 18749
复制代码

让旧nginx不加载配置文件的情况下启动工作进程


  1. kill -QUIT 23949
复制代码

优雅地关闭新nginx工作进程(但我测试时, 不但新nginx工作进程结束了, 其主进程也结束了, 难道文档太旧了, 还是其它什么原因?)

  1. kill -TERM 23949
复制代码

关闭新nginx主进程(如果不成功则kill -KILL 23949强制关闭)

随着新nginx主进程关闭, nginx.pid文件被删除, 而nginx.pid.oldbin则重命名成nginx.pid, 一切都恢复到尝试更新nginx之前.

  1. [root@main nginx]# ls
  2. client_body_temp  conf  fastcgi_temp  html  logs  nginx.pid  proxy_temp  sbin
复制代码



该方法也适用于增加/删除nginx模块

[ 本帖最后由 xi2008wang 于 2008-10-8 14:09 编辑 ]

论坛徽章:
0
发表于 2008-10-08 15:13 |显示全部楼层
没人顶

论坛徽章:
0
发表于 2008-10-08 15:25 |显示全部楼层
都哭了 帮你顶下吧

论坛徽章:
0
发表于 2008-10-08 15:33 |显示全部楼层
有必要这么来么?怎么确认没间断呢

论坛徽章:
0
发表于 2008-10-08 17:13 |显示全部楼层
原帖由 LAMP兄弟连 于 2008-10-8 15:25 发表
都哭了 帮你顶下吧

论坛徽章:
0
发表于 2008-10-08 17:14 |显示全部楼层
原帖由 heizi21 于 2008-10-8 15:33 发表
有必要这么来么?怎么确认没间断呢

nginx既然有这个功能, 当然能用上就最好了.试试就知道了

论坛徽章:
0
发表于 2008-10-08 17:24 |显示全部楼层
似乎现在NGINX比较流行, 大家都用啊

论坛徽章:
0
发表于 2008-10-08 17:43 |显示全部楼层
看来还是眼泪有用

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之水原三星
日期:2015-06-02 16:34:202015年亚冠纪念徽章
日期:2015-10-19 18:13:37程序设计版块每日发帖之星
日期:2015-11-08 06:20:00
发表于 2008-10-08 19:59 |显示全部楼层

论坛徽章:
0
发表于 2008-10-10 15:31 |显示全部楼层
nginx不是有make upgrade的么?需要这么麻烦么?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP