欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Apache日志分割(rotatelogs与cronolog)

程序员文章站 2022-06-25 21:46:08
...

日志分割

简介

随着网站的访问量增加,默认情况下Apache的单个日志文件也会越来越大
日志文件占用磁盘空间很大

  • 查看相关信息不方便
  • 对日志文件进行分割

工具

  1. Apache自带rotatelogs分割工具实现
  2. 第三方工具cronolog分割

rotatelogs分割工具

  • 配置网站的日志文件转交给rotatelogs分割处理

配置格式

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

编辑配置文件

[aaa@qq.com ~]# vi /etc/httpd.conf 
[aaa@qq.com ~]# systemctl start httpd
[aaa@qq.com ~]# systemctl status httpd

日志设置

ErrorLog "|/usr/bin/rotatelogs -l logs/error_log_%Y-%m-%d 86400"
-l:表示使用本地时间
/error_log:日志名称
%Y-%m-%d:年--86400:表示日志分隔的间隔是1天,单位是秒
CustomLog "|/usr/bin/rotatelogs -l logs/access_log_%Y-%m-%d 86400" common   //错误日志

Apache日志分割(rotatelogs与cronolog)

查看日志文件

[aaa@qq.com ~]# cd /usr/local/httpd/logs/
[aaa@qq.com logs]# ls -lh

Apache日志分割(rotatelogs与cronolog)

cronolog日志分割

解压缩安装包

[aaa@qq.com ~]# tar zxvf cronolog-1.6.2.tar.gz 

文件默认设置

[aaa@qq.com ~]# cd cronolog-1.6.2/
[aaa@qq.com cronolog-1.6.2]# ./configure

编译安装

[aaa@qq.com cronolog-1.6.2]# make && make install

查看日志

[aaa@qq.com cronolog-1.6.2]# vi /etc/httpd.conf 
ErrorLog "|/usr/local/sbin/cronolog logs/error.log_%Y-%m-%d"
CustomLog "|/usr/local/sbin/cronolog logs/access.log_%Y-%m-%d" common

Apache日志分割(rotatelogs与cronolog)

[aaa@qq.com ~]# systemctl start httpd
[aaa@qq.com ~]# httpd -t                              
[aaa@qq.com ~]# systemctl stop firewalld   
[aaa@qq.com ~]# setenforce 0                    
[aaa@qq.com ~]# cd /usr/local/httpd/logs/    
[aaa@qq.com logs]# ls -lh
-rw-r--r--.1 root root 286 1126 17:16 access_log
-rw-r--r-- l root root 383 1126 18:02 access.log_2020-11-26
-rw-r--r--.1 root root 288 1126 18:16 error_log
-rw-r--r-- l root root 286 1126 17:00 error.log_2020-11-26
-rw-r--r-- l root root   5 1126 18:00 httpd.pid

AWstats日志分析

  • 不同于日志分割,是对整个日志进行分析
[aaa@qq.com ~]# vi /etc/httpd.conf 
[aaa@qq.com ~]# systemctl start httpd
[aaa@qq.com ~]# httpd -t
[aaa@qq.com ~]# systemctl status httpd

ErrorLog "logs/error_log"
CustomLog "logs/access_log" common

Apache日志分割(rotatelogs与cronolog)

配置文件

修改配置

[aaa@qq.com modules]# vi /etc/httpd.conf 
[aaa@qq.com modules]# cd
LoadModule cgid_module modules/mod_cgid.so
LoadModule cgi_module modules/mod_cgi.so

解压安装包

[aaa@qq.com ~]# tar zxvf awstats-7.6.tar.gz 

配置映射

[aaa@qq.com ~]# mv awstats-7.6 /usr/local/awstats
[aaa@qq.com ~]# vi /etc/hosts   映射记录
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1       localhost localhost.localdomain localhost6 localhost6.localdomain6 
192.168.100.10 www.aaa.com l
[aaa@qq.com ~]# vi /etc/httpd.conf 
ServerName www.aaa.com:80
[aaa@qq.com ~]# cd /usr/local/
[aaa@qq.com local]# ls -lh
[aaa@qq.com local]# chown -R root.root awstats/
[aaa@qq.com local]# cd awstats/
[aaa@qq.com awstats]# ls -lh
[aaa@qq.comrver1 awstats]# cd tools/    //进入工具目录
[aaa@qq.com tools]# ./awstats_configure.pl   //找到配置脚本

对AWstats文件进行配置

[aaa@qq.com tools]# vi /etc/awstats/awstats.www.aaa.com.conf
LogFile="/usr/local/httpd/logs/access_log"
DirData="/var/lib/awstats"

Apache日志分割(rotatelogs与cronolog)

[aaa@qq.com tools]# cd
[aaa@qq.com ~]# cd /var/lib/awstats
[aaa@qq.com ~]# mkdir /var/lib/awstats

删除多余文件进行配置

[aaa@qq.com ~]# cd /usr/local/httpd/logs/
[aaa@qq.com logs]# ls -lh
[aaa@qq.com logs]# rm -rf access.log*
[aaa@qq.com logs]# rm -rf error.log*

[aaa@qq.com ~]# cd /usr/local/awstats/tools/
[aaa@qq.com tools]# ls -lh

日志更新

[aaa@qq.com tools]# ./awstats_updateall.pl now

Apache日志分割(rotatelogs与cronolog)

[aaa@qq.com ~]# vi /etc/httpd.conf 
[aaa@qq.com ~]# systemctl  stop httpd
[aaa@qq.com ~]# systemctl start  httpd
//添加
Require all granted

Apache日志分割(rotatelogs与cronolog)

[aaa@qq.com tools]# vi /etc/httpd.conf 

Apache日志分割(rotatelogs与cronolog)

编辑awstats的配置文件

[aaa@qq.com tools]# vi /etc/awstats/awstats.www.aa.com.conf
[aaa@qq.com tools]# systemctl start httpd

LogFile="/usr/local/httpd/logs/access_log"

Apache日志分割(rotatelogs与cronolog)

重启主机,查看日志

[aaa@qq.com ~]# cd /usr/local/httpd/logs/
[aaa@qq.com logs]# ls -lh

手动采集日志

[aaa@qq.com logs]# cd
[aaa@qq.com ~]# cd /usr/local/awstats/tools/
[aaa@qq.com tools]# ./awstats_updateall.pl now

Apache日志分割(rotatelogs与cronolog)

[aaa@qq.com ~]# mkdir /var/lib/awstats
[aaa@qq.com awstats]# systemctl stop httpd
[aaa@qq.com awstats]# systemctl start httpd
[aaa@qq.com tools]# ./awstats_updateall.pl  now

Apache日志分割(rotatelogs与cronolog)