Nginx热部署和日志切割

1.热部署

Nginx有三种安装方式:

1.RPM包安装

2.yum安装

3.二进制编译安装

当你选择yum安装时,Nginx安装的版本往往不是当前最新且稳定的版本

例如,我在我自己的虚拟机上面yum安装的Nginx版本则是1.14.1的

[root@k8s-master03 ~]#nginx -v
nginx version: nginx/1.14.1

而在nginx官网上面比较新的稳定版本则是1.18.0的

在这里插入图片描述

因此我们可以将nginx进行更新,而nginx有一个特性则是热部署(不需要进行nginx的重启则可以完成版本的更迭)

扫描二维码关注公众号,回复: 13005681 查看本文章

1.环境

OS:centos8

nginx:1.14.1->1.18.0

2.流程

下面则是我进行热部署的一个流程:

前提:1.确保自己的系统安装好了编译安装所必须的软件 yum install -y gcc gcc-c++ autoconf automake make
	 2.下载了基本的模块依赖 gzip的zlip,rewrite重写的pcre,https需要的openssl等	
	 yum install zlib zlib-devel  pcre-devel openssl openssl-devel httpd-tools vim -y
以上软件如果没有,可以执行一下上述命令。否则在之后的操作可能会报错。
如果遇到了报错,可以百度报错也可以解决方法,另外对于nginx的错误,可以通过查看nginx的错误日志去查询定位错误

错误:
由于自己是通过yum进行安装的nginx,因此有很多模块都没有安装,除了上述的模块依赖之外,还需要安装以下模块,否则在编译安装新的nginx时,会报一些模块错误
yum -y install libxslt-devel
yum -y install gd-devel
yum -y install perl-devel perl-ExtUtils-Embed

1.下载新版nginx压缩包
2.备份旧版nginx二进制文件
3.编译安装
4.退出旧版nginx进程

3.热部署具体操作

1.官网下载新的版本的二进制文件(我下载的是nginx1.18.0版本的)

[root@k8s-master03 ~]#wget http://nginx.org/download/nginx-1.18.0.tar.gz

[root@k8s-master03 ~]#ls nginx-1.18.0.tar.gz 
nginx-1.18.0.tar.gz

2.备份旧版的nginx二进制文件

[root@k8s-master03 ~]#which nginx     #寻找nginx的二进制文件
/usr/sbin/nginx

[root@k8s-master03 ~]#mv /usr/sbin/nginx /usr/sbin/nginx.old   #给旧的二进制文件改名(备份)

[root@k8s-master03 ~]#ls /usr/sbin/nginx.old
/usr/sbin/nginx.old

3.检查旧版本nginx的编译参数

因为此时我们已经将二进制文件改名为nginx.old的,因此待会·查看编译参数时,需要使用nginx.old来查看

再者,由于我们最开始使用的是yum安装的nginx,所以他默认的编译参数比较多,我们都需要进行复制下来,保证下一步编译安装新版本的nginx的编译参数一致,configure arguments后面接的则是编译使用的参数

[root@k8s-master03 ~]#nginx.old -V  
nginx version: nginx/1.14.1
built by gcc 8.2.1 20180905 (Red Hat 8.2.1-3) (GCC) 
built with OpenSSL 1.1.1 FIPS  11 Sep 2018 (running with OpenSSL 1.1.1c FIPS  28 May 2019)
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_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_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

4.编译安装新版nginx

[root@k8s-master03 ~]#tar -zxvf nginx-1.18.0.tar.gz  #解压

[root@k8s-master03 ~]#cd nginx-1.18.0/

[root@k8s-master03 ~]#./configure  --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_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_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'    #编译安装

[root@k8s-master03 ~]#make && make install

此时可以发现有两个版本的nginx程序
[root@k8s-master03 sbin]#ls /usr/sbin |grep nginx
nginx
nginx.old

5.发送一个USR2信号给旧的master process

(nginx中USR2信号代表停止接受新的连接,等待当前连接停止,重新载入配置文件,重新打开日志文件,重启服务器,从而实现相对平滑的不关机的更改)

[root@k8s-master03 sbin]#kill -USR2 `cat /run/nginx.pid` #/run/nginx.pid是nginx进程号文件

此时查看nginx进程会发现:

在这里插入图片描述

6.退出旧的nginx进程

[root@k8s-node01 nginx-1.18.0]#ls /run/ |grep nginx #查看nginx的pid文件
nginx.pid
nginx.pid.oldbin

[root@k8s-master03 run]#kill -WINCH `cat /run/nginx.pid.oldbin` #热部署必做步骤

此时发现,旧版本的worker进程已经退出,但是旧的master进程并不会自动退出,而是继续存在作为之后的版本回退(回退的方法其实和升级的方法差不多)

在这里插入图片描述

7.完成热部署

1.通过ip地址去测试nginx是否能够正常访问
2.如果可以正常访问,则可以选择性的关闭旧的nginx的master进程   kill `cat /run/nginx.pid.oldbin`
3.如果不可哟正常访问,则可以选择进行版本回退

2.日志切割

在实际生产中,web的访问量是比较大的,因此日志中的内容相对而言也是庞大的,所以为了避免日志文件过大,不便于查看,我们需要对日志文件进行定期的切割

1.查看目前的nginx的日志

[root@k8s-master03 nginx]#ll /var/log/nginx
total 16
-rw-rw-r-- 1 nginx root 1071 Jan 11 16:15 access.log
-rw-r--r-- 1 root  root  257 Jan  4 10:49 access.log-20210108.gz
-rw-rw-r-- 1 nginx root 2872 Jan 11 16:15 error.log
-rw-r--r-- 1 root  root  285 Jan  4 10:49 error.log-20210108.gz

发现此时已经有了压缩的日志文件,这是因为我们是使用yum安装的nginx,因此存在着默认的nginx切割功能(yum源安装nginx,会默认安装一个日志切割的配置文件/etc/logrotate.d/nginx)

centos系统默认自带logrotate,logrotate是一个日志管理工具。我们可以查看一下默认的切割配置文件中的内容

[root@k8s-master03 nginx]#cat /etc/logrotate.d/nginx
/var/log/nginx/*log {
    create 0664 nginx root
    daily
    rotate 10
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

如果是使用二进制编译安装的nginx,则默认不存在日志切割功能,因此我们需要自己写一个日志切割功能的定时脚本

1.编写脚本
[root@k8s-master03 script]#cat cut_nginx_log.sh 
#!/bin/bash

#Name: yup
#Desc: Nginx log cutting

#定义log文件的目录
log_path="/var/log/nginx/"

#创建保存切割后日志的目录 /year/month/
mkdir -p ${log_path}$(date +"%y")/$(date +"%m")/

#重命名日志文件
mv ${log_path}access.log  ${log_path}$(date +"%y")/$(date +"%m")/access_$(date +"%m_%d").log

#执行命令 nginx -s reopen"的作用和"kill -USR1 NginxPid"的作用是一样
kill -USR1 `cat /run/nginx.pid`

2.赋予x权限
[root@k8s-master03 script]#chmod +x cut_nginx_log.sh

3.运行脚本,检测
[root@k8s-master03 script]#./cut_nginx_log.sh 

4.查看
[root@k8s-master03 script]#tree /var/log/nginx
/var/log/nginx
├── 21
│   └── 01
│       └── access_01_11.log

5.在脚本中 还可以选择将切割后的日志进行压缩打包,进一步节省空间

6.设置定时任务
[root@k8s-master03 script]#crontab -e
#每天的23:55执行nginx的日志切割脚本
55 23 * * *  bash ~/script/cut_nginx_log.sh

猜你喜欢

转载自blog.csdn.net/qq_42502583/article/details/112482812