关于运维:基于Linux直接安装的Nginx版本升级方法

2次阅读

共计 2568 个字符,预计需要花费 7 分钟才能阅读完成。

引言

随着版本的迭代和破绽的发现,Nginx 作为一款软件防止不了打补丁的命运。

以下基于 Linux 间接装置的 Nginx 版本升级。

以下操作均在本地虚拟机中操作验证,请验证后再线上操作。基于 centos7 测试。

前置资源

  • 获取 nginx 的最新源码版本网址:http://freenginx.org/
  • 查看 nginx 的版本命令:nginx -V

基于源码装置 Nginx

装置编译环境

nginx 基于 c 语言编写的,所以装置之前须要装置编译环境。

### 装置编译环境和依赖包
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
## 验证编译环境是否装置胜利
gcc --version

gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

逆向编译命令

nginx 编译通过 ./configure 命令加指定参数来实现,对应的参数决定了应用程序的装置地位、日志文件地位、依赖库应用状况等。

所以在装置之前须要晓得如下信息:

  • 应用到的依赖库
  • 程序安装地位
  • 启用的模块

以上信息能够通过 ngingx -V 来获取。

## 进入 nginx 的装置目录
nginx -V
## 能够看到对应的配置参数信息
nginx version: nginx/1.24.0
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: --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-http_stub_status_module

装置新版本 nginx

  • 到正式网站下载源码:

举荐 Free 版本 http://freenginx.org/en/download.html
或者 F5 版本 http://nginx.org/en/download.html

freenginx 是 nginx 被 F5 收买进去的一个独立分支,由原来的 nginx 的外围人员保护,就像 mariadb 和 mysql 的关系。

  • 上传源码到指定服务器,开始装置。
# 解压源码
tar -xf nginx-1.25.4.tar.gz
cd nginx-1.25.4
## 依据逆向失去的参数进行编译
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-http_stub_status_module
##  如果短少对应依赖库,能够通过如下命令装置
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
## 也能够到对应库的正式网站下载安装,通过命令指定装置地位,比方
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-http_stub_status_module --with-pcre=/root/install_app/pcre2-10.43
## 装置
make
make install
## 查看装置后的版本
/usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.25.4
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: --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-http_stub_status_module --with-pcre=/root/install_app/pcre2-10.43

以上是通过笼罩的形式降级 nginx,留神提前做好备份,防止数据失落。

基于安装包装置 Nginx

nginx 在 linux 上做了很多散发包,包含 debian、centos、ubuntu 等。通过 nginx 官网的安装包降级 nginx,能够防止编译过程的麻烦。

确认 nginx 是通过安装包装置的,能够通过装置门路查看。个别 nginx 的装置门路为离开的,包含任意门路能够执行nginx -V

centos 通过 rpm -qa nginx 查找 nginx 的安装包是否存在。

降级过程如下:


## 装置 nginx 的仓库源,拜访 http://nginx.org/packages/centos/7/noarch/RPMS/ 下载 nginx-release-centos-7-0.el7.ngx.noarch.rpm  
wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
## 装置 nginx 源
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
## 查看 nginx 安装包列表
yum --showduplicates list nginx
## 装置最新版本的 nginx
yum install nginx.x86_64
## 验证版本
nginx -V

阐明

以上就是 nginx 1.21.5 降级到 1.25.4 的过程。不同镜像版本可能降级过程不统一。

请在虚拟机中操作验证,请勿间接线上生产主机操作。

对于作者

来自一线全栈程序员 nine 的八年摸索与实际,继续迭代中。欢送关注“雨林寻北”或增加集体卫星 codetrend(备注技术)。

正文完
 0