ubantu系统下平滑升级nginx版本

ubantu系统下平滑升级nginx版本

首先关于nginx升级有两种方法:

1.直接更新ubantu系统的nginx源文件,再通过apt安装直接获取nginx最新版本,需要获取nginx官网公钥

2.通过上传nginx.tar包,解压之后通过替换nginx二进制文件,make编译之后替换安装,关于第二种方法比较麻烦,因为nginx版本升级可能会导致nginx依赖的依赖库版本也要对应升级

在这里选择的是第2种方法,因为我们的服务设置了禁止访问外网ip,所以我没有办法从nginx官网获取公钥升级

第一种方法 公钥升级

1.备份nginx程序
whereis nginx
cp /usr/sbin/nginx ./backup

2.备份/etc/nginx
sudo cp -r /etc/nginx ./nginx

3.卸载
$ sudo apt remove nginx nginx-common nginx-full nginx-core

4.更新apt源 要注意codename,ubantu版本不同codename不同
# 进入目录
$ cd /etc/apt
# 备份源文件
$ sudo cp sources.list sources.list.bak
# 编辑配置文件
$ sudo vim sources.list
# 文件末尾添加以下两行并保存
deb [arch=amd64] http://nginx.org/packages/mainline/ubuntu/ bionic nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ bionic nginx

注: 18.04 “bionic” x86_64, aarch64/arm64
	20.04 “focal” x86_64, aarch64/arm64, s390x
	21.04 “hirsute” x86_64, aarch64/arm64
	21.10 “impish” x86_64, aarch64/arm64
	
5.导入nginx公钥
sudo wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key

6.安装nginx最新版本
$ sudo apt-get update
$ sudo apt-get install nginx

7.还原备份的配置文件
sudo cp -f /backup/nginx/nginx.conf /etc/nginx

8.重启服务
$ sudo systemctl restart nginx

$ sudo systemctl status nginx  查看nginx状态
$ sudo systemctl start nginx   启动nginx
$ sudo systemctl stop nginx  停止nginx

这种方法基本上不会遇到什么问题,就是更新了ubantu自带的nginx下载源

第二种 二进制文件替换升级

1.下载nginx升级包也可以提前下载好tar包上传到服务器
sudo wget http://nginx.org/download/nginx-1.23.4.tar.gz

2.解压升级包
sudo tar zxvf nginx-1.23.4.tar.gz

3.进入解压后的新nginx文件夹
cd nginx-1.23.4/

4.查看当前版本得到编译参数(/usr/sbin/nginx是nginx的二进制执行文件)
  编译参数在后续升级中要保持一致
sudo /usr/sbin/nginx -V 不仅可以查看版本号,还可以查看nginx的编译参数
sudo nginx -v           只是查看nginx版本号

参数:--with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-YlUNvj/nginx-1.14.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-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_geoip_module=dynamic --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-stream_ssl_preread_module --with-mail=dynamic --with-mail_ssl_module

5.用上面编译参数,参数中的版本需要修改
sudo ./configure --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-YlUNvj/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time....

6.编译make,千万别make install
sudo make
make编译完后,在objs目录下就多了个nginx,这个就是新版本的二进制文件

7.备份原本的nginx可执行文件,有助于恢复操作
sudo mv /usr/sbin/nginx /usr/sbin/nginx.bak

8.将新生成nginx执行文件复制到/usr/sbin下
cp /objs/nginx /usr/sbin/nginx

9.检测配置文件是否正确
sudo /usr/sbin/nginx -t

10.重新加载配置文件
sudo nginx -s reload

11.执行升级(通过拷贝替换的方式可以不执行这一步了)
make upgrade

12.查看nginx版本号和编译参数
sudo /usr/sbin/nginx -V

第二种方法会存在很多报错情况,在这里重点记录一下

1.当./configure --with-cc-opt='-g -O2 -fdebug-prefix-map...报错,这种错误都是nginx版本和原来的依赖库版本不对应,要升级依赖库版本

./configure: error: the HTTP rewrite module requires the PCRE library.
./configure: error: the HTTP XSLT module requires the libxml2/libxslt libraries.
./configure: error: the HTTP image filter module requires the GD library.
./configure: error: the HTTP rewrite module requires the PCRE library.
./configure: error: the HTTP cache module requires md5 functions from OpenSSL library.
./configure: error: the GeoIP module requires the GeoIP library.

解决:基本上大差不大,都是重新安装依赖库就可以了
	(当你打 libpcre时按tab看会出现什么东西,因为软件可能和你的不一样)
	 apt-get install libpcre3 libpcre3-dev
	 apt-get install libxslt1-dev
	 apt-get install libgd2-xpm libgd2-xpm-dev
	 apt-get install libpcre3 libpcre3-dev
	 apt-get install libssl-dev openssl
	 apt-get install libgeoip-dev	
2.当编译完成之后,nginx -t检查配置文件时,报其他错误module.so的版本问题
nginx: [emerg] module "/usr/share/nginx/modules/ngx_http_xslt_filter_module.so" version 1014000 instead of 1021004 in /etc/nginx/modules-enabled/50-mod-http-xslt-filter.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed

# 备份旧的module
sudo mv /usr/share/nginx/modules/ngx_http_xslt_filter_module.so /usr/share/nginx/modules/ngx_http_xslt_filter_module.so.old
# 拷贝新的module
sudo cp objs/ngx_http_xslt_filter_module.so  /usr/share/nginx/modules/ngx_http_xslt_filter_module.so
其他类似的so文件报错也是大差不差,因为我们./configure modules-path=/usr/lib/nginx/modules编译时候指定了moudules模块,所以直接把老版的so文件备份,从objs目录下拷贝新的so文件到modules目录下即可

最后 
nginx -t               检查配置文件
nginx -s reload        重新加载配置文件
systemctl status nginx 查看nginx运行状态
systemctl start nginx  运行nginx
systemctl stop nginx   通知nginx
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值