Nginx的平滑升级、启动方式的修改以及日志的自动切割

1.Nginx的平滑升级

上篇博文中讲到nginx可以做到不下线的在线更新,现在我们在Nginx主机上已经安装了1.17.1版本的Nginx,使用1.16.0稳定版本的“升级”nginx
(1)步骤一:下载1.16.1版本并解压 (使用/usr/local/nginx/sbin/nginx -V 查看上一个版本的具体编译信息)
./configure --prefix=/usr/local/nginx --with-file-aio 使用同样编译信息进行编译
make (不能make install 否则会全部清除全部Nginx配置)

[root@server1 nginx-1.16.0]# ps ax      #查看已经开启的Nginx进程
[root@server1 nginx-1.16.0]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
configure arguments: --prefix=/usr/local/nginx --with-file-aio
[root@server1 nginx-1.16.0]#  ./configure --prefix=/usr/local/nginx --with-file-aio

在这里插入图片描述在这里插入图片描述在这里插入图片描述(2) 步骤二
cd /nginx-16.0/objs,ls 可以看到有一个nginx文件(./nginx -V 查看具体编译信息)
cp -f nginx /usr/local/nginx/sbin/nginx

[root@server1 nginx-1.16.0]# cd objs
[root@server1 objs]# ls
autoconf.err  nginx    ngx_auto_config.h   ngx_modules.c  src
Makefile      nginx.8  ngx_auto_headers.h  ngx_modules.o
[root@server1 objs]# ./nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
configure arguments: --prefix=/usr/local/nginx --with-file-aio
[root@server1 objs]# cp -f nginx /usr/local/nginx/sbin/nginx        #强制将1.16.0版本的主进程覆盖掉原来的主进程
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y

在这里插入图片描述
(3)步骤三
ps -ef | grep nginx 可以看到之前版本的一个主进城(3925)和两个worker进城
kill -USR2 3925 使用新版本的Nginx文件启动服务,之后平缓停止原有Nginx进程,也就是所谓的”平滑升级”
ps -ef | grep nginx 可以看到生成新的主进城(6505)和worker进程
kill -WINCH 3925 平缓停止worker process,用于Nginx服务器平滑升级
此时 可以看到 ps -ef | grrep nginx 只存在一个旧的主进程(3925)和新的主进程(6505)和worker进程,不停掉旧的主进程,为了更新失败的话 就回退
更新成功:/usr/local/nginx/sbin/nginx -V 可以看到版本已经更新

[root@server1 nginx]# ps -ef | grep nginx
root      3925     1  0 18:21 ?        00:00:00 nginx: master process ./nginx
nginx     3926  3925  0 18:21 ?        00:00:00 nginx: worker process
nginx     3927  3925  0 18:21 ?        00:00:00 nginx: worker process
root      6504  1123  0 19:15 pts/0    00:00:00 grep --color=auto nginx
[root@server1 nginx]# kill -USR2 3925
[root@server1 nginx]# ps -ef | grep nginx
root      3925     1  0 18:21 ?        00:00:00 nginx: master process ./nginx
nginx     3926  3925  0 18:21 ?        00:00:00 nginx: worker process
nginx     3927  3925  0 18:21 ?        00:00:00 nginx: worker process
root      6505  3925  0 19:16 ?        00:00:00 nginx: master process ./nginx
nginx     6506  6505  0 19:16 ?        00:00:00 nginx: worker process
nginx     6507  6505  0 19:16 ?        00:00:00 nginx: worker process
root      6509  1123  0 19:16 pts/0    00:00:00 grep --color=auto nginx
[root@server1 nginx]# kill -WINCH 3925
[root@server1 nginx]# ps -ef | grep nginx
root      3925     1  0 18:21 ?        00:00:00 nginx: master process ./nginx
root      6505  3925  0 19:16 ?        00:00:00 nginx: master process ./nginx
nginx     6506  6505  0 19:16 ?        00:00:00 nginx: worker process
nginx     6507  6505  0 19:16 ?        00:00:00 nginx: worker process
root      6512  1123  0 19:17 pts/0    00:00:00 grep --color=auto nginx
[root@server1 nginx]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
configure arguments: --prefix=/usr/local/nginx --with-file-aio

在这里插入图片描述
(4)升级失败怎么回退
kill -HUP 3925 使用新的配置文件启动进程,之后平缓停止原有进程,也就是所谓的”平滑重启”
ps -ef | grep nginx (旧worker进程已经启动
kill -WINGH 6505
ps -ef | grep nginx 新worker进程停掉
cd /nginx-1.17.1/objs
cp -f nginx /usr/local/nginx/sbin/nginx
ps -ef | grep nginx
kill -9 6505 停掉新主进程
ps -ef | grep nginx 可以看到旧版本进程已经启动正常

[root@server1 objs]# kill -HUP 3925
[root@server1 nginx]# kill -WINCH 6505
[root@server1 nginx-1.17.1]# cd /root/nginx-1.17.1/objs/
[root@server1 objs]# cp -f nginx /usr/local/nginx/sbin/nginx
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y
[root@server1 objs]# kill -9 6505
[root@server1 objs]# ps -ef | grep nginx
root      3925     1  0 18:21 ?        00:00:00 nginx: master process ./nginx
nginx     6525  3925  0 19:30 ?        00:00:00 nginx: worker process
nginx     6526  3925  0 19:30 ?        00:00:00 nginx: worker process
root      6532  1123  0 19:32 pts/0    00:00:00 grep --color=auto nginx
[root@server1 objs]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
configure arguments: --prefix=/usr/local/nginx --with-file-aio

在这里插入图片描述

2.Nginx的启动方式的修改

我们希望像httpd一样,可以通过systemctl 命令来管理nginx,所需操作如下

[root@server1 ~]# cp /usr/lib/systemd/system/httpd.service /etc/systemd/system/nginx.service
[root@server1 ~]# vim /etc/systemd/system/nginx.service
[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target

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

[Install]
WantedBy=multi-user.target

[root@server1 ~]# ps ax 因为Nginx已经开启 先关闭/usr/local/nginx/sbin/nginx -s stop
[root@server1 ~]# systemctl start nginx     #开启成功

在这里插入图片描述
在这里插入图片描述

3.日志的切割

不论以何种方式访问Nginx主机,都会生成access.log连接日志,会造成该文件太大,故需要进行日志的切割,所谓切割,我们将实现的就是将每天的日志都存放在自己的日志文件中。操作如下:
(1)日志的切割:

[root@server1 ~]# cd /usr/local/nginx
[root@server1 nginx]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp
[root@server1 nginx]# cd logs
[root@server1 logs]# ls
access.log  error.log  nginx.pid
[root@server1 logs]# cat access.log 
172.25.33.2 - - [24/Jul/2019:18:32:07 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0"
[root@server2 ~]# ab -c 1 -n 100000 http://172.25.33.1/index.html     #生成1个并发,十万个访问
[root@server1 logs]# du -sh access.log 
9.8M	access.log

在这里插入图片描述

在这里插入图片描述

[root@server1 logs]# mv access.log `date +%F -d -1day`_access.log   #先备份 (获取今天时间 date +%F 昨天date +%F -d -1day) 
[root@server1 logs]# /usr/local/nginx/sbin/nginx -s reopen
  #会再生成/usr/local/nginx/logs/access.log 连接日志
[root@server1 logs]# ls
2019-07-23_access.log  access.log  error.log  nginx.pid

[root@server2 ~]# ab -c 1 -n 1000 http://172.25.33.1/index.html
现在再来1000个请求就会记录在/usr/local/nginx/logs/access.log日志中

在这里插入图片描述(2)脚本实现日志的切割:
方式一:

[root@server1 ~]cd /usr/local/nginx/logs
[root@server1 logs]vim backup.sh
[root@server1 logs]mkdir /usr/local/nginx/logs/oldlogs
[root@server1 logs]chmod +x backup.sh
[root@server1 logs]# ls
2019-07-23_access.log  access.log  backup.sh  error.log  nginx.pid  oldlogs
[root@server1 logs]# sh backup.sh 
[root@server1 logs]# cd oldlogs/
[root@server1 oldlogs]# ls
2019-07-23_access.log  2019-07-23_error.log

在这里插入图片描述
在这里插入图片描述
方式二:

[root@server1 logs]# cd oldlogs/
[root@server1 oldlogs]# ls
2019-07-23_access.log  2019-07-23_error.log
[root@server1 oldlogs]# cd ..
[root@server1 logs]# sh backup.sh 
[root@server1 logs]# cd oldlogs/
[root@server1 oldlogs]# ls
2019-07-22_access.log  2019-07-23_access.log
2019-07-22_error.log   2019-07-23_error.log

在这里插入图片描述
在这里插入图片描述

(3)将脚本加入定时任务中:

[root@server1 oldlogs]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@server1 oldlogs]# crontab -l
0 0 1 * * /bin/bash /usr/local/nginx/logs/backup.sh
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值