[关闭]
@yibo 2017-05-11T07:09:51.000000Z 字数 2503 阅读 1050

Nginx 平滑升级、增删模块

nginx


以添加 VeryNginx 扩展,并将 Nginx 从 1.10.0 升级到 1.13.0 为例

安装 VeryNginx

  1. wget https://github.com/alexazhou/VeryNginx/archive/v0.3.3.zip
  2. unzip v0.3.3.zip
  3. cd VeryNginx-0.3.3/
  4. python install.py install verynginx
  5. cd ..

安装 VeryNginx 用到了 Python 脚本,但这个项目跟 Python 没有半毛钱关系,不信可以看下 install.py 中的 install_verynginx 方法,只做了拷贝文件和修改配置目录权限两件事。

VeryNginx 默认会被装到 /opt/verynginx/ 目录,本文使用默认配置。

编译 Nginx

VeryNginx 依赖的 http_stub_status_modulehttp_ssl_module 只需要在 configure 时加上就可以。lua-nginx-module 稍微麻烦一点,它有以下依赖:

下面分别来搞定它们。本文使用 CentOS 7,全部采用默认路径安装。如果你的环境跟我不一样,一些命令请自行调整。

LuaJIT

下载并安装 LuaJIT

  1. wget http://luajit.org/download/LuaJIT-2.1.0-beta3.zip
  2. unzip LuaJIT-2.1.0-beta3.zip
  3. cd LuaJIT-2.1.0-beta3/
  4. make
  5. make install
  6. cd ..

设置环境变量:

  1. export LUAJIT_LIB=/usr/local/lib
  2. export LUAJIT_INC=/usr/local/include/luajit-2.1/

ngx_devel_kit

下载并解压 ngx_devel_kit

  1. wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.zip
  2. unzip v0.3.0.zip

ngx_lua

下载并解压 ngx_lua

  1. wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc1.zip
  2. unzip v0.10.9rc1.zip

Nginx

  1. cd /opt/source/
  2. wget http://nginx.org/download/nginx-1.13.0.tar.gz
  3. tar -zxvf nginx-1.13.0.tar.gz
  4. cd nginx-1.13.0
  5. ./configure \
  6. --prefix=/opt/nginx-1.13.0 \
  7. --user=nginx \
  8. --group=nginx \
  9. --with-stream \
  10. --with-http_stub_status_module \
  11. --with-http_ssl_module \
  12. --with-http_realip_module \
  13. --add-module=../ngx_cache_purge-2.3 \
  14. --with-http_sub_module \
  15. --with-http_v2_module \
  16. --with-ld-opt="-Wl,-rpath,/usr/local/lib/" \
  17. --add-module=../ngx_devel_kit-0.3.0 \
  18. --add-module=../lua-nginx-module-0.10.9rc1
  19. make

注意:这里只进行 make 操作,不用 make install

将之前老版本的 nginx copy 一份,在此副本上进行操作

  1. cp -r /opt/nginx-1.10.0 /opt/nginx-1.13.0
  2. rm -rf /opt/nginx
  3. ln -s /opt/nginx-1.13.0 /opt/nginx
  4. cd /opt/nginx/sbin
  5. mv nginx nginx.old
  6. cp /opt/source/nginx-1.13.0/objs/nginx /opt/nginx/sbin/

让nginx把nginx.pid文件修改成nginx.pid.oldbin,随即启动nginx,实现不间断。

在 nginx-1.13.0 的源码文件夹内执行 make upgrade 等效于以下命令。不过 make upgrade 在 nginx 新老版本不是同一目录的情况下,会出错。

  1. kill -USR2 `cat /opt/nginx-1.13.0/logs/nginx.pid` 更新配置文件,这个 pid 文件实际是从老版本复制过来的
  2. kill -QUIT `cat /opt/nginx-1.10.0/logs/nginx.pid.oldbin` 优雅的关闭

配置 VeryNginx

在 Nginx 中引入 VeryNginx 的配置文件,就可以让 VeryNginx 工作起来。首先要修改的是 Nginx 的主配置。

在主配置文件的最外层,加入以下配置:

  1. include /opt/verynginx/verynginx/nginx_conf/in_external.conf;

在主配置的 http 段落中,加入以下配置:

  1. include /opt/verynginx/verynginx/nginx_conf/in_http_block.conf;

在具体站点配置的 server 段落中,加入以下配置:

  1. include /opt/verynginx/verynginx/nginx_conf/in_server_block.conf;

加完之后,建议通过 -t 参数确保配置无误:

  1. /usr/local/nginx/sbin/nginx -t

如果提示 test is successful,说明配置无误,可以重启 Nginx 服务;否则请根据提示排查。

如果一切顺利,访问 http://yourdomain.com/verynginx/index.html 就可以见到 VeryNginx 的 Web 控制面板。默认用户名和密码都是 verynginx,登录后请务必修改。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注