Ubuntu (Linux) 平滑编译和安装或升级Nginx 1.23.1

nginx版本更新迭代,需要升级解决漏洞的。

1. nginx平滑升级

因nginx版本更新迭代,现需要将nginx升级最新版本,目前最新版本nginx为1.23.1。为了不影响nginx运行,采用平滑升级的方式。

下载查看全新版本&获取源码 nginx官网地址:🔗 http://nginx.org/en/download.html

注意:请在 /root 下目录进行,或登录的用户目录

💽下载最新版nginx版本(目前是1.23.1)

1
wget http://nginx.org/download/nginx-1.23.1.tar.gz

🎁解压 & 进入源码目录

1
2
tar -zxvf nginx-1.23.1.tar.gz
cd nginx-1.23.1

2. 准备编译环境

编译环境大几大操作系统不同,以ubuntu 20.04 为主,会标注 Debian CentOS redhat 命令

🍘安装编译环境

注意:需分三步安装,ubuntu 一起安装会不识别

CentOS

1
2
sudo yum update
sudo yum -y install gcc g++ build-essential automake

ubuntu / Debian

1
2
3
4
sudo apt update
sudo apt install gcc
sudo apt install g++
sudo apt install build-essential automake

🥗安装pcre软件包(使nginx支持http rewrite模块)

CentOS

1
sudo yum -y install pcre pcre-devel

ubuntu / Debian

1
sudo apt install libpcre3 libpcre3-dev

🥩安装openssl-devel(使nginx支持ssl)

Centos

1
sudo yum -y install openssl 

Ubuntu 名字改了,很多人装不上是不知道

1
sudo apt install libssl-dev

🥣安装zlib

CentOS

1
sudo yum -y install zlib zlib-devel

Ubuntu 名字改了,很多人装不上是不知道

1
2
sudo apt install zlib1g
sudo apt install zlib1g-dev

3. 准备configure命令

编译安装前要做好配置,不然很多模块没有加载就不能用了。如果是以前有Nginx 可以看看以前用了什么模块,把配置加上去

🔭查看以前的版本和配置

如果全新安装不需要,跳过,升级前请自行保存nginx.conf 文件 和 虚拟机等配置文件。

1
nginx -V

得到如下结果,如果需要以前的模块请记住configure arguments 后面的信息,以后用得上

1
2
3
4
5
6
7
root@154:~# nginx -V
nginx version: nginx/1.22.1
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1) 
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
# ==> configure arguments:后面开始,如果需要保留配置
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -ffile-prefix-map=/data/builder/debuild/nginx-1.22.0/debian/debuild-base/nginx-1.22.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

🔦编辑前加入自行需要的配置

./configure + 需要的模块

但是可以使用 configure --help 或在 NGINX.org 的文档中更详细地检查这些配置标志。

以下为常用已编辑版本的官方的配置,供参考我个人够用

1
./configure  --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -ffile-prefix-map=/data/builder/debuild/nginx-1.22.0/debian/debuild-base/nginx-1.22.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

最精简版的配置

1
./configure --sbin-path=/usr/bin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-pcre --pid-path=/var/run/nginx.pid --with-http_ssl_module

–sbin-path 是 nginx 可执行文件的位置, /usr/bin/ 是 Ubuntu 可执行文件的正常路径。

–conf-path 为/etc/nginx,表示nginx配置文件的路径,是Ubuntu中配置文件的通用路径,即使使用包管理器安装,nginx配置文件也位于此处。

–.error-log-path 设置错误日志的保存位置。

–http-log-path 留下访问日志。

–with-pcre 告诉 NGINX 将 pcre 库用于正则表达式。

通过 –pid-path 设置 pid 的路径。

通过 –with-http_ssl_module 指示使用 ssl 模块。

对于 –with-http_ssl_module,sudo apt install libssl-dev 这个需要安装才行 .

4.最后,开始编译:

🎊make 就是开始编译了

1
2
3
4
#开始编译,依据CPU性能来定
make
#安装编译好的二进制,安装或升级
make install

5.还原配置&检查进程

将配置文件还原或检查是否安装成功

编译完成后可执行文件生成在objs文件夹下

先进入objs文件夹,输入测试语法文件命令,检查配置文件是否正常

1
2
cd objs
./nginx -t

例外:

这一步会出现module加载失败的问题,大致问题就是新版本使用的模块版本高于本地安装的旧版本

这个问题也很好解决,因为我们编译安装的nginx二进制文件内已经有了相关函数,直接调用本机依赖库,已经不再需要在配置文件中启用相关模块,直接在配置文件中关闭以前所有启用的模块即可

进入nginx module配置文件目录,将以前版本启用的模块都关闭掉

这里将软连接移动到modules_available文件夹相当于关闭模块

1
mv /etc/nginx/modules-enabled/* /etc/nginx/modules-available/

此时再测试配置文件语法正常

检查nginx版本,是否启用

1
nginx -V

再次检查配置文件是否正常

1
nginx -t

🎢重启服务完成升级

重启服务完成

1
systemctl restart nginx
Licensed under CC BY-NC-SA 4.0
这是一个墨茶的博客
Built with Hugo
主题 StackJimmy 设计