Apache服务日志分割(rotatelogs、cronolog)

一、日志分割

1.1 日志分割应用场景介绍

  • 随着网站的访问量增加,默认情况下的Apache的单个日志文件也会越来越大
  1. 日志文件占用磁盘空间
  2. 查看相关信息不方便
  • 对日志文件进行分割
  1. Apache自带rotatelogs分割工具实现
  2. 第三方工具cronolog分割

1.2 rotatelogs分割工具

  • 配置网站的日志文件转交给rotatelogs分割处理
  • 配置格式
ErrorLog "| rotatelogs命令的绝对路径 -l 日志文件路径/网站名-error_%Y%m%d.log 86400"

CustomLog "| rotatelogs命令的绝对路径 -l 日志文件路径/网站名-access_%Y%m%d.log 86400" combined

1.3 实操:利用rotatelogs工具对日志文件分割

  • 实验环境:VMware Workstation 15.5、Centos 7.6、Xshell 6

  • 实验步骤:

1、安装http服务

[root@localhost ~]# yum -y install httpd

2、编辑http服务主配置文件,修改监听端口及域名

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf

a6RLng.png
a6RbjS.png

3、启动服务,查看生成的日志文件

[root@localhost ~]# systemctl start httpd     ## 启动服务
[root@localhost ~]# ls /var/log/httpd/        ## 查看httpd目录下的有哪些文件
access_log  error_log                         ## 有两个日志文件存在

4、关闭系统核心防护并清空防火墙规则

[root@localhost ~]# setenforce 0
[root@localhost ~]# iptables -F

5、编辑http服务配置文件,进行日志分割的相关配置

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
分别对182和217行进行修改:

ErrorLog "| /usr/sbin/rotatelogs -l logs/www.test.com.error_%Y%m%d.log 86400"
CustomLog "| /usr/sbin/rotatelogs -l logs/www.test.com.access_%Y%m%d.log 86400" combined

6、重启http服务

[root@localhost ~]# systemctl restart httpd

7、再次使用ls命令查看日志目录下的文件

[root@localhost ~]# ls /var/log/httpd/
access_log  error_log  www.test.com.access_20200805.log  www.test.com.error_20200805.log

a6RHc8.md.png

会发现多出了两个文件,都是网站名+日期的日志文件

1.4 cronolog日志分割工具

我们也可以使用第三方工具cronolog工具对日志进行分割

  • 源码编译安装cronolog工具
  • 配置网站日志文件转交给cronolog分割处理
  • 配置格式
ErrorLog "| cronolog命令的绝对路径 日志文件路径/网站名-error_%Y%m%d.log"

CustomLog "| cronolog命令的绝对路径 日志文件路径/网站名-access_%Y%m%d.log" combined

1.5 实操:利用cronolog工具对日志文件分割

  • 实验步骤:

1、将源码包(cronolog-1.6.2-14.el7.x86_64)上传到虚拟机,并进行安装

[root@localhost ~]# cd /opt
[root@localhost opt]# rpm -ivh cronolog-1.6.2-14.el7.x86_64.rpm 

2、进入日志文件目录,删除之前实验生成的日志文件

[root@localhost opt]# cd /var/log/httpd/
[root@localhost httpd]# rm -rf www*

3、修改http服务主配置文件

[root@localhost httpd]# vim /etc/httpd/conf/httpd.conf 

将刚才的182和217行进行修改:

ErrorLog "| /usr/sbin/cronolog logs/www.test.com.error_%Y%m%d.log"
CustomLog "| /usr/sbin/cronolog logs/www.test.com.access_%Y%m%d.log" combined

4、修改完成后重启服务,使用ls命令查看日志目录下的日志文件

[root@localhost httpd]# systemctl restart httpd    ## 重启服务

[root@localhost httpd]# ls /var/log/httpd/        ## 查看日志文件
access_log  error_log  www.test.com.access_20200805.log  www.test.com.error_20200805.log

日志文件成功生成!

猜你喜欢

转载自blog.csdn.net/u014042047/article/details/107828526