Nginx平滑升级及添加新功能
前言:
因目前最新的稳定版本是nginx-1.22.0
,所以在此先安装前一个版本nginx-1.20.2
作升级演示,外加演示一个添加echo这个功能。
做本次实验的环境是要提前源码部署nginx的,不然要升级什么呢?
手动源码部署请参考此篇=>源码部署Nginx
懒癌患者请查看此篇=> 在线一键部署Nginx的shell脚本
Nginx平滑升级流程:
1)获取原版本的编译信息可使用nginx -V
命令
2)获取新版本或功能包
3)配置新版本或功能,预编译时带上原版本的编译信息+新版本或新功能(–add-module)
4)make进行编译,但不能执行make install(安装)操作
5)下面几步建议一条命令串完成,耗时短让网站访客无感知到更新
命令串为cp /usr/local/nginx/sbin/nginx{,-bak};pkill nginx;\cp objs/nginx /usr/local/nginx/sbin/;systemctl start nginx
上条命令的解释:先备份(建议cp)原版本的主程序(通常在/usr/local/nginx/sbin/的nginx),然后停止原程序的运行
把新版本程序移动到原程序的存放位置,然后启动
平滑升级总结:在Linux中的源码部署软件升级的流程几乎都是如此,所谓触类旁通。
升级流程演示
[root@localhost ~]# systemctl status nginx.service
● nginx.service - nginx server daemon
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2022-10-12 14:37:41 CST; 2min 38s ago
#获取原版本的编译信息,可以看到此时是1.20.2版本
#configure arguments后面的编译参数复制下来,等会儿要用
[root@localhost ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module
#获取新版本
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
#获取echo的功能包需要git命令从GitHub或Gitee拉取
[root@localhost src]# dnf -y install git
[root@localhost src]# git clone https://gitee.com/wujunze/nginx_module_echo.git
[root@localhost src]# ls
nginx-1.22.0.tar.gz nginx_module_echo
#预编译新版本,带上原版本的编译参数+新功能参数
[root@localhost src]# tar -xf nginx-1.22.0.tar.gz
[root@localhost src]# cd nginx-1.22.0/
[root@localhost nginx-1.22.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--add-module=../nginx_module_echo
#进行编译,但不要安装
[root@localhost nginx-1.22.0]# make
#查看编译好的nginx主程序
[root@localhost nginx-1.22.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@localhost nginx-1.22.0]# ls objs/
addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
#可以看到该nginx程序文件是1.22.0
[root@localhost nginx-1.22.0]# objs/nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=../nginx_module_echo
#把原nginx主程序文件备份,停止原nginx进程,把新nginx主程序文件copy到原nginx主程序文件存放的目录,最后启动新nginx的进程
[root@localhost nginx-1.22.0]# cp /usr/local/nginx/sbin/nginx{,-bak};pkill nginx;\cp objs/nginx /usr/local/nginx/sbin/;systemctl start nginx