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

目录

要求:

一、安装编译环境

二、编译安装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重启就没问题了,搞定!!!!

  • 21
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在Linux编译安装Nginx,您可以按照以下步骤进行操作: 1. 首先,您需要下载Nginx的源码包。您可以使用wget命令来下载,例如: ``` wget -c https://nginx.org/download/nginx-1.18.0.tar.gz ``` 2. 下载完成后,您需要解压源码包。可以使用以下命令进行解压: ``` tar -zxvf nginx-1.18.0.tar.gz ``` 3. 解压完成后,进入解压后的目录: ``` cd nginx-1.18.0 ``` 4. 在进入目录后,您可以进行配置。可以使用以下命令进行配置: ``` ./configure ``` 5. 配置完成后,您可以使用make命令编译Nginx: ``` make ``` 6. 编译完成后,您可以使用make install命令将Nginx安装到指定的目录中: ``` make install ``` 7. 安装完成后,您可以使用以下命令启动Nginx: ``` /usr/local/nginx/sbin/nginx ``` 8. 您可以使用以下命令检查Nginx的配置文件是否正确: ``` /usr/local/nginx/sbin/nginx -t ``` 以上是在Linux编译安装Nginx的基本步骤。请根据您的实际情况进行操作。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [linux 编译安装nginx](https://blog.csdn.net/weixin_52099680/article/details/113525732)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Linux编译安装nginx](https://blog.csdn.net/chaijunkun/article/details/7009183)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Linux安装编译nginx](https://blog.csdn.net/u014641168/article/details/129487741)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qq_46131155

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

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

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

打赏作者

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

抵扣说明:

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

余额充值