[linux]ubantu系统下平滑升级nginx版本

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

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

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

第一种方法 公钥升级

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

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

3.停止并卸载
sudo nginx -s stop
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
#查看可安装的nginx版本
sudo apt-cache show nginx
$ sudo apt-get install nginx

7.安装完成,查看nginx版本
sudo nginx -v
在etc/nginx/conf.d/ 中会多出来一个默认配置文件default.conf,可以删了或者改个名。之前的配置文件还在,检查一下配置文件,证书之类的没问题的话,就可以愉快的启nginx了。

可以把本分的ngixn配置替换过去
cp nginx_backup/nginx.conf /etc/nginx/

sudo systemctl start nginx

如果启动出现pid没有权限之类的问题:

#查看
cat /lib/systemd/system/nginx.service
 
#文件里面关于pid的路径是否和/etc/nginx/nginx.conf的一样,如果不一样,把/lib/systemd/system/nginx.service的路径改为/etc/nginx/nginx.conf里配置pid文件的路径即可。
 
#改完/lib/systemd/system/nginx.service后,需要执行:
sudo systemctl daemon-reload
 
#然后再启动nginx一下
sudo systemctl start nginx
#看一下服务状态
sudo systemctl status 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

你可能感兴趣的:(nginx,linux,服务器)