Nginx平滑升级(以1.18到1.20为例,其他同理)
Nginx平滑升级(以1.18到1.20为例,其他同理)
1. 简介
在不停掉老进程的情况下,启动新进程;老进程负责处理仍然没有处理完的请求,但不再接受处理请求;新进程接受新请求;老进程处理 完所有请求,关闭所有连接后,停止。
一般有两种情况下需要升级 nginx,一种是确实要升级 nginx 的版本,另一种是要为 nginx 添加新的模块。
2. 准备
旧版nginx备份
root@nginx:/etc# which nginx
/usr/sbin/nginx
root@nginx:/etc# cp /usr/sbin/nginx /usr/sbin/nginx.bak.20210809
root@nginx:~# cd /etc
root@nginx:/etc# cp -r nginx nginx.bak.20210809/
root@nginx:~# cd /usr/lib/nginx/
root@nginx:/usr/lib/nginx# cp -r modules/ modules.bak.20210809
环境准备
root@nginx:~/ytreel# apt-get update
如下这些软件,不一定需要全装,取决于configure arguments的具体内容。不想在服务器上装太多没用的东西,就在configure的时候,一步一步看报错缺什么就装什么。怕麻烦就直接执行下面的命令全安装。
root@nginx:~/ytreel# apt-get install -y libpcre3 libpcre3-dev openssl libssl-dev libxslt1-dev libxml2 libxml2-dev libgd-dev
源码下载
源码官网下载地址:http://nginx.org/en/download.html
本次下载的为当前Stable version的最新版1.20.1
3. 源码安装
查看现有nginx的编译参数
root@nginx:~/ytreel# nginx -V
nginx version: nginx/1.18.0 (Ubuntu)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-KTLRnK/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
解压缩新版nginx
root@nginx:~/ytreel# tar zxvf nginx-1.20.1.tar.gz
配置和编译新版nginx(注意不要执行make install操作,make install会将原来的配置文件覆盖)
root@nginx:~/ytreel# cd nginx-1.20.1/
root@nginx:~/ytreel/nginx-1.20.1# ./configure --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-KTLRnK/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid image--modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
root@nginx:~/ytreel/nginx-1.20.1# make
4. 平滑升级
确认旧版nginx二进制文件和配置文件已备份。
拷贝新版nginx二进制文件
root@nginx:~/ytreel/nginx-1.20.1# cp -f objs/nginx /usr/sbin/nginx
拷贝编译生成的库文件
root@nginx:~/ytreel/nginx-1.20.1# cp objs/*.so /usr/lib/nginx/modules/
查看当前nginx版本是否已更新
root@nginx:~/ytreel/nginx-1.20.1# nginx -V
使用新版nginx二进制文件测试配置文件是否正确
root@nginx:~/ytreel/nginx-1.20.1# nginx -t
如果版本没有变化,执行下面的命令重新加载nginx的配置:
root@nginx:~/ytreel/nginx-1.20.1# nginx -s reload
之后再去查看版本就是更新后的新版本了