Linux下对nginx进行平滑升级(编译安装方式)

本文详细描述了如何在不影响业务运行和用户体验的前提下,安全地将Nginx从1.16.1版本升级到1.24.0,包括安装编译环境、配置升级过程和解决可能出现的问题。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

目录

要求:

一、安装编译环境

二、编译安装nginx1.16.1

1、下载nginx1.16.1tar包  官网下载地址:nginx: download

2、解压nginx1.16.1包

3、进入nginx-1.16.1/目录

4、进行编译配置

5、编译安装

6、给nginx配置一个systemctl管理(配置的路径要与nginx安装的路径一致)

7、启动nginx

二、安装nginx1.24.0

1、下载nginx-1.24.0,解压并进入nginx-1.24.0目录

2、将nginx-1.16.1配置的编译文件复制

3、执行make,不要make install

4、移动当前版本的nginx文件

5、将nginx-1.24.0目录下的nginx放到/usr/local/nginx/sbin/目录下

6、生成新的nginx  master进程

7、平滑停止旧版本的nginx进程

8、可以看到当前版本已经升级到了1.24.0

三、升级后出现的问题

要求:

不停止业务运行,不能影响用户体验,将nginx1.16.1升级为nginx1.24.0。

一、安装编译环境

yum install epel-release         //安装epel扩展源
yum install gcc gcc-c++ make zlib-devel pcre pcre-devel openssl-devel perl-devel perl-ExtUtils-Embed gd-devel geoip-devel -y    //安装编译基础环境

二、编译安装nginx1.16.1

1、下载nginx1.16.1tar包  官网下载地址:nginx: download
2、解压nginx1.16.1包
tar xf nginx-1.16.1.tar.gz
3、进入nginx-1.16.1/目录
cd nginx-1.16.1/
4、进行编译配置
 ./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module \
--with-http_geoip_module          \
--with-http_geoip_module=dynamic  \
--with-http_sub_module            \
--with-http_dav_module            \
--with-http_flv_module            \
--with-http_mp4_module            \
--with-http_gunzip_module         \
--with-http_gzip_static_module    \
--with-http_auth_request_module   \
--with-http_random_index_module   \
--with-http_secure_link_module    \
--with-http_degradation_module    \
--with-http_slice_module          \
--with-http_stub_status_module
5、编译安装
make && make install
6、给nginx配置一个systemctl管理(配置的路径要与nginx安装的路径一致)
vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -S reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
7、启动nginx
systemctl start nginx

ps:如果启动失败,大概率是因为没有创建nginx用户

创建用户nginx,然后就可以正常启动nginx了

useradd -s /sbin/nologin nginx

二、安装nginx1.24.0

1、下载nginx-1.24.0,解压并进入nginx-1.24.0目录
tar xf nginx-1.24.0.tar.gz
cd nginx-1.24.0/
2、将nginx-1.16.1配置的编译文件复制
[root@web03 nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-pcre \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_addition_module \
> --with-http_sub_module \
> --with-http_dav_module \
> --with-http_flv_module \
> --with-http_mp4_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_random_index_module \
> --with-http_secure_link_module \
> --with-http_stub_status_module \
> --with-http_auth_request_module \
> --with-http_image_filter_module \
> --with-http_slice_module \
> --with-mail \
> --with-threads \
> --with-file-aio \
> --with-stream \
> --with-mail_ssl_module \
> --with-stream_ssl_module \
> --with-http_geoip_module          \
> --with-http_geoip_module=dynamic  \
> --with-http_sub_module            \
> --with-http_dav_module            \
> --with-http_flv_module            \
> --with-http_mp4_module            \
> --with-http_gunzip_module         \
> --with-http_gzip_static_module    \
> --with-http_auth_request_module   \
> --with-http_random_index_module   \
> --with-http_secure_link_module    \
> --with-http_degradation_module    \
> --with-http_slice_module          \
> --with-http_stub_status_module
3、执行make,不要make install
[root@web03 nginx-1.24.0]# make
4、移动当前版本的nginx文件
[root@web03 ~]# cd /usr/local/nginx/sbin/
[root@web03 sbin]# ls
nginx
[root@web03 sbin]# mv nginx /root/
5、将nginx-1.24.0目录下的nginx放到/usr/local/nginx/sbin/目录下
[root@web03 sbin]# mv /root/nginx-1.24.0/objs/nginx /usr/local/nginx/sbin/
[root@web03 sbin]# ls
nginx
[root@web03 sbin]#
6、生成新的nginx  master进程
[root@web03 ~]# cd /usr/local/nginx/logs/
[root@web03 logs]# cat nginx.pid
15210
[root@web03 logs]# kill -USR2 15210
[root@web03 logs]# ls
access.log  error.log  nginx.pid  nginx.pid.oldbin
[root@web03 logs]#

可以看到,当前的nginx进程文件自动命名为nginx.pid.oldbin,又生成了一个新的nginx.pid文件

7、平滑停止旧版本的nginx进程
[root@web03 logs]# cat nginx.pid.oldbin
15210
[root@web03 logs]# kill -WINCH 15210
[root@web03 logs]# kill -QUIT 15210
[root@web03 logs]# ls
access.log  error.log  nginx.pid
[root@web03 logs]#

旧版本的nginx进程已经被新版本的nginx进程替代

8、可以看到当前版本已经升级到了1.24.0
[root@web03 logs]# /usr/local/nginx/sbin/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 --user=nginx --group=nginx --with-pcre --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_image_filter_module --with-http_slice_module --with-mail --with-threads --with-file-aio --with-stream --with-mail_ssl_module --with-stream_ssl_module --with-http_geoip_module --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module
[root@web03 logs]#

9、访问页面测试:输入本机IP

三、升级后出现的问题

1、如果升级之后执行systemctl restart nginx 重启失败,可能是因为配置systemctl启动nginx文件有问题

vim /usr/lib/systemd/system/nginx.service

修改配置文件中ExecReload=/usr/local/nginx/sbin/nginx -S reload 将-S改为-s

再次systemctl restart nginx重启就没问题了,搞定!!!!

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_46131155

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值