一、查看旧版本信息
可以通过nginx -V
命令,来查看当前nginx的版本信息,和配置参数。
[root@nb001 sbin]# nginx -V
-bash: nginx: command not found
[root@nb001 sbin]# ./nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_stub_status_module --with-http_ssl_module
旧版本的sbin目录情况:
[root@nb001 sbin]# ls -lrth
total 14M
-rwxr-xr-x 1 root root 3.8M Nov 1 2021 nginx.old.1
-rwxr-xr-x 1 root root 3.8M Nov 1 2021 nginx.2021年11月3日17:35:42
-rwxr-xr-x 1 root root 5.8M Nov 3 2021 nginx
二、升级
2.1 下载解压最新安装包
点击下载nginx,这里一般是要下载稳定版。如图:
2.2 上传解压
[root@nb001 softs]# tar -zxvf nginx-1.26.2.tar.gz
nginx-1.26.2/
nginx-1.26.2/man/
。。。
[root@nb001 nginx-1.26.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
2.3 执行编译
首先执行./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
这个命令的参数和之前版本的保持一致即可
[root@nb001 nginx-1.26.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
checking for OS+ Linux 3.10.0-1127.19.1.el7.x86_64 x86_64
checking for C compiler ... found+ using GNU C compiler+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
checking for gcc -pipe switch ... found
。。。省略。。。
checking for zlib library ... found
creating objs/MakefileConfiguration summary+ using system PCRE library+ using system OpenSSL library+ using system zlib librarynginx path prefix: "/usr/local/nginx"nginx binary file: "/usr/local/nginx/sbin/nginx"nginx modules path: "/usr/local/nginx/modules"nginx configuration prefix: "/usr/local/nginx/conf"nginx configuration file: "/usr/local/nginx/conf/nginx.conf"nginx pid file: "/usr/local/nginx/logs/nginx.pid"nginx error log file: "/usr/local/nginx/logs/error.log"nginx http access log file: "/usr/local/nginx/logs/access.log"nginx http client request body temporary files: "client_body_temp"nginx http proxy temporary files: "proxy_temp"nginx http fastcgi temporary files: "fastcgi_temp"nginx http uwsgi temporary files: "uwsgi_temp"nginx http scgi temporary files: "scgi_temp"[root@nb001 nginx-1.26.2]#
执行成功后,接着执行make
命令:
[root@nb001 nginx-1.26.2]# make
make -f objs/Makefile
make[1]: Entering directory `/data/softs/nginx-1.26.2'
。。。省略。。。
objs/src/http/modules/ngx_http_stub_status_module.o \
objs/ngx_modules.o \
-ldl -lpthread -lcrypt -lpcre -lssl -lcrypto -ldl -lpthread -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \-e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \-e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/data/softs/nginx-1.26.2'
执行完make
命令之后,可以发现在objs目录下生成了nginx文件
[root@nb001 nginx-1.26.2]# cd objs/
[root@nb001 objs]# ls
autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
最后,执行make install
,这里install命令都是会看是否存在的,不存在执行安装,存在就不执行对应的复制步骤了。一般可以放心执行。当然如不放心,可以手动复制处理。在此建议备份后(如conf文件,sbin文件,甚至html文件)再执行。
[root@nb001 nginx-1.26.2]# make install
make -f objs/Makefile install
make[1]: Entering directory `/data/softs/nginx-1.26.2'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \|| mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' \|| mv '/usr/local/nginx/sbin/nginx' \'/usr/local/nginx/sbin/nginx.old'
cp objs/nginx '/usr/local/nginx/sbin/nginx'
test -d '/usr/local/nginx/conf' \|| mkdir -p '/usr/local/nginx/conf'
cp conf/koi-win '/usr/local/nginx/conf'
cp conf/koi-utf '/usr/local/nginx/conf'
cp conf/win-utf '/usr/local/nginx/conf'
test -f '/usr/local/nginx/conf/mime.types' \|| cp conf/mime.types '/usr/local/nginx/conf'
cp conf/mime.types '/usr/local/nginx/conf/mime.types.default'
test -f '/usr/local/nginx/conf/fastcgi_params' \|| cp conf/fastcgi_params '/usr/local/nginx/conf'
cp conf/fastcgi_params \'/usr/local/nginx/conf/fastcgi_params.default'
test -f '/usr/local/nginx/conf/fastcgi.conf' \|| cp conf/fastcgi.conf '/usr/local/nginx/conf'
cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default'
test -f '/usr/local/nginx/conf/uwsgi_params' \|| cp conf/uwsgi_params '/usr/local/nginx/conf'
cp conf/uwsgi_params \'/usr/local/nginx/conf/uwsgi_params.default'
test -f '/usr/local/nginx/conf/scgi_params' \|| cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params \'/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \|| cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \|| cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \|| mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/data/softs/nginx-1.26.2'
[root@nb001 nginx-1.26.2]#
去看sbin目录,和旧版本的sbin目录对比发现:上一个nginx被命名为nginx.old, 并将最新的nginx复制到了sbin下。
且经过查看conf/nginx.conf没有被覆盖。
[root@nb001 sbin]# ls -lrth
total 25M
-rwxr-xr-x 1 root root 3.8M Nov 1 2021 nginx.old.1
-rwxr-xr-x 1 root root 3.8M Nov 1 2021 nginx.2021年11月3日17:35:42
-rwxr-xr-x 1 root root 5.8M Nov 3 2021 nginx.old
-rwxr-xr-x 1 root root 5.8M Oct 9 09:39 nginx
[root@nb001 sbin]#
三、校验和最后一步
3.1 查看当前运行的nginx
访问发现,目前还是旧版本。这里访问你的ip随便一个路径就行
3.2 最后一步升级操作
需要在新版本目录下执行 make upgrade
操作,完成进程更新。
[root@nb001 nginx-1.26.2]# make upgrade
/usr/local/nginx/sbin/nginx -t
nginx: [warn] protocol options redefined for 0.0.0.0:443 in /usr/local/nginx/conf/vhost_cvec/ss-prod.conf:2
nginx: [warn] protocol options redefined for 0.0.0.0:443 in /usr/local/nginx/conf/vhost_cvec/us.conf:10
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
[root@nb001 nginx-1.26.2]#
3.3 再次查看当前运行的nginx
访问发现,已经是新版本啦。
至此,实现了nginx版本的平滑无感知,不停机升级 , 有需要的小伙伴快去试试吧。