[关闭]
@yibo 2015-10-10T05:38:11.000000Z 字数 1159 阅读 825

Nginx 平滑升级脚本

nginx bash


  1. #!/usr/bin/env bash
  2. # 升级nginx,加stream模块——tcp代理
  3. oldVersion=1.8
  4. newVersion=1.9.2
  5. newPath=/opt/nginx-$newVersion
  6. srcPath=/opt/source/nginx-$newVersion
  7. # copy一份当前的nginx作为副本,并在该副本上进行升级操作
  8. cd /opt
  9. cp -r nginx-$oldVersion nginx-$newVersion
  10. # 加参数编译
  11. cd $srcPath
  12. ./configure --prefix=/opt/nginx-$newVersion \
  13. --user=nginx --group=nginx \
  14. --with-stream \
  15. --with-http_stub_status_module \
  16. --with-http_ssl_module \
  17. --with-http_realip_module \
  18. --add-module=../ngx_cache_purge-2.3 \
  19. --with-http_image_filter_module \
  20. --with-http_sub_module \
  21. --with-http_spdy_module
  22. # 执行make编译,但不要执行make install
  23. make
  24. # 重命名nginx旧版本二进制文件(期间nginx并不会停止服务)
  25. rm -rf $newPath/sbin/nginx.old
  26. mv $newPath/sbin/nginx $newPath/sbin/nginx.old
  27. # 然后拷贝一份新编译的二进制文件
  28. cp $srcPath/objs/nginx $newPath/sbin/
  29. # 向nginx.conf中追加新的配置
  30. cat >>$newPath/conf/nginx.conf<<EOF
  31. stream {
  32. upstream backend {
  33. #hash $remote_addr consistent;
  34. server 10.10.5.11:1024;
  35. }
  36. server {
  37. listen 9999;
  38. proxy_pass backend;
  39. }
  40. }
  41. EOF
  42. # 为了保险起见,还是用vim瞜一眼配置吧
  43. vim $newPath/conf/nginx.conf
  44. # 检验一下配置文件有没有错
  45. $newPath/sbin/nginx -t -c $newPath/conf/nginx.conf
  46. echo -n "是否开始升级? (yes/no)"
  47. read confirm
  48. if [ "$confirm" == 'yes' ];then
  49. cd $srcPath
  50. # 在源码目录运行
  51. make upgrade
  52. # 查看一下版本是否正确
  53. $newPath/sbin/nginx -V
  54. # 重新设置一下软连接
  55. cd /opt
  56. rm -rf nginx
  57. ln -s nginx-1.9.2 nginx
  58. fi
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注