Nginx在线服务状态下平滑升级

1 查看当前nginx版本

nginx -V 
nginx version: nginx/1.19.2
built by gcc 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1) 
built with OpenSSL 1.1.1  11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.19.2/debian/debuild-base/nginx-1.19.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

2 备份当前版本

/backupcp /usr/sbin/nginx nginx
/backup cp -r /etc/nginx nginx_conf_bak

3 升级nginx

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
 wget https://nginx.org/download/nginx-1.21.0.tar.gz
 tar -xf nginx-1.21.0.tar.gz

 nginx-1.21.0./configure  --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.19.2/debian/debuild-base/nginx-1.19.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

 nginx path prefix: "/etc/nginx"
  nginx binary file: "/usr/sbin/nginx"
  nginx modules path: "/usr/lib/nginx/modules"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/var/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/cache/nginx/client_temp"
  nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
  nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
  nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
  nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"

make -j 2
#make install (不建议执行这一步)

mv '/usr/sbin/nginx' '/usr/sbin/nginx.old'
cp objs/nginx '/usr/sbin/nginx'

重新加载nginx sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

$ nginx -v
nginx version: nginx/1.21.0

进行平滑升级
-HUP 平滑启动(相当于reload)
-USR2 平滑升级可执行程序,主要用在版本升级
-WINCH 从容关闭工作进程
-USR1 重新打开日志文件,主要用在日志切割(相当于reopen)

kill -USR2 旧版本主进程号
 kill  -USR2 748 

新的master进程已经正常开启,但老的work进程也存在,所以我们使用下面的命令,将老的work进程发出平滑停止的信号
 kill -WINCH 748 
如果在版本升级完成后,没有任何问题,需要关闭老的master进程的话,可以使用下面的命令
 kill -QUIT 748

如上已平滑升级完成

Nginx在线服务状态下平滑升级

4 nginx make install 做了哪些操作

make -f objs/Makefile install
make[1]: Entering directory '/home/gtj/nginx-1.21.0'
test -d '/etc/nginx' || mkdir -p '/etc/nginx'
test -d '/usr/sbin' \
    || mkdir -p '/usr/sbin'
test ! -f '/usr/sbin/nginx' \
    || mv '/usr/sbin/nginx' \
        '/usr/sbin/nginx.old'
cp objs/nginx '/usr/sbin/nginx'
test -d '/etc/nginx' \
    || mkdir -p '/etc/nginx'
cp conf/koi-win '/etc/nginx'
cp conf/koi-utf '/etc/nginx'
cp conf/win-utf '/etc/nginx'
test -f '/etc/nginx/mime.types' \
    || cp conf/mime.types '/etc/nginx'
cp conf/mime.types '/etc/nginx/mime.types.default'
test -f '/etc/nginx/fastcgi_params' \
    || cp conf/fastcgi_params '/etc/nginx'
cp conf/fastcgi_params \
    '/etc/nginx/fastcgi_params.default'
test -f '/etc/nginx/fastcgi.conf' \
    || cp conf/fastcgi.conf '/etc/nginx'
cp conf/fastcgi.conf '/etc/nginx/fastcgi.conf.default'
test -f '/etc/nginx/uwsgi_params' \
    || cp conf/uwsgi_params '/etc/nginx'
cp conf/uwsgi_params \
    '/etc/nginx/uwsgi_params.default'
test -f '/etc/nginx/scgi_params' \
    || cp conf/scgi_params '/etc/nginx'
cp conf/scgi_params \
    '/etc/nginx/scgi_params.default'
test -f '/etc/nginx/nginx.conf' \
    || cp conf/nginx.conf '/etc/nginx/nginx.conf'
cp conf/nginx.conf '/etc/nginx/nginx.conf.default'
test -d '/var/run' \
    || mkdir -p '/var/run'
test -d '/var/log/nginx' \
    || mkdir -p '/var/log/nginx'
test -d '/etc/nginx/html' \
    || cp -R html '/etc/nginx'
test -d '/var/log/nginx' \
    || mkdir -p '/var/log/nginx'
make[1]: Leaving directory '/home/gtj/nginx-1.21.0'

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: