您的位置:首页 > 运维架构 > Apache

Apache配置与应用——日志分割

2019-10-25 10:11 1431 查看

日志分割

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

rotatelogs分割工具

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

配置格式为

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

环境

一台Linux服务器(192.168.13.128)
一台win7测试主机

Apache自带rotatelogs日志分割工具

1,安装Apache服务

[root@localhost ~]# yum install httpd -y
[root@localhost ~]# cd /usr/sbin    ##切换到/usr/sbin目录下
[root@localhost sbin]# ls rotat*     ##日志分析工具位置
rotatelogs
[root@localhost sbin]# vim /etc/httpd/conf/httpd.conf     ##修改配置文件
Listen 192.168.13.128:80               ##修改ipv4监听端口
#Listen 80      ##将ipv6端口注释
ServerName www.kgc.com:80       ##修改域名
[root@localhost named]# systemctl stop firewalld.service    ##关闭防火墙
[root@localhost named]# setenforce 0   ##关闭增强功能
[root@localhost html]# systemctl start httpd.service  ##启动网络服务
[root@localhost sbin]# ls /var/log/httpd/   ##此时就有了日志文件了
accesslog  errorlog

2,配置http主配置文件信息

[root@localhost sbin]# cd /etc/httpd/conf
[root@localhost conf]# vim httpd.conf ##配置http主配置文件
#ErrorLog "logs/errorlog"
ErrorLog "| /usr/sbin/rotatelogs -l logs/www.kgc.com.error%Y%m%dlog 86400"
##此处添加工具绝对路径,生成日志的时间

CustomLog "| /usr/sbin/rotatelogs -l logs/www.kgc.com.access_%Y%m%dlog 86400" combined
##此处添加工具绝对路径,生成日志的时间

3,关闭重启服务,查看日志分割情况

[root@localhost conf]# systemctl stop httpd.service   ##关闭http服务
[root@localhost conf]# systemctl start httpd              ##启动http服务
[root@localhost conf]# cd /var/log/httpd/                   ##切换到日志目录下查看
[root@localhost httpd]# ls
access_log  error_log  www.kgc.com.error_20191025log   ##即为今日的分割日志文件
[root@localhost httpd]# date
2019年 10月 25日 星期五 09:12:31 CST
[root@localhost httpd]# date -s 10/26/19   ##修改时间为明天
2019年 10月 26日 星期六 00:00:00 CST
[root@localhost httpd]# systemctl stop httpd   ##关闭重启服务
[root@localhost httpd]# systemctl start httpd
[root@localhost httpd]# ls                  ##查看日志分割的情况
access_log  www.kgc.com.error_20191025log
error_log   www.kgc.com.error_20191026log

第三方工具cronolog工具

源码编译安装cronolog工具
配置网站日志文件转交给cronolog分割处理

配置格式为

  • > ErrorLog "| cronolog命令的绝对路径 日志文件路径/网站名-error_%Y%m%d.log"
  • > CustomlogLog "| cronolog命令的绝对路径 日志文件路径/网站名-error_%Y%m%d.log" combined

1,安装http服务,远程挂载第三方工具

[root@localhost ~]# yum install httpd -y
[root@localhost ~]# smbclient -L //192.168.10.88/
Sharename       Type      Comment
---------       ----      -------
LAMP-C7         Disk

[root@localhost ~]# mount.cifs //192.168.10.88/LAMP-C7 /mnt
##远程挂载软件包到/mnt目录
[root@localhost ~]# cd /mnt/   ##切换到/mnt目录下
[root@localhost mnt]# ls
apr-1.6.2.tar.gz       cronolog-1.6.2-14.el7.x86_64.rpm  LAMP-php5.6.txt
apr-util-1.6.0.tar.gz  Discuz_X2.5_SC_UTF8.zip           mysql-5.6.26.tar.gz
awstats-7.6.tar.gz     httpd-2.4.29.tar.bz2              php-5.6.11.tar.bz2
[root@localhost mnt]# rpm -ivh cronolog-1.6.2-14.el7.x86_64.rpm  ##安装工具

2,查看工具并修改http配置文件

[root@localhost mnt]# cd /usr/sbin
[root@localhost sbin]# ls cronolog*
cronolog
[root@localhost sbin]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.13.128:80      ##修改监听的地址
#Listen 80
ServerName www.kgc.com:80   ##修改域名
ErrorLog "| /usr/sbin/cronolog logs/www.kgc.com.error_%Y%m%d.log"
CustomLog "| /usr/sbin/cronolog logs/www.kgc.com.access_%Y%m%d.log" combined
##修改日志文件

3,重启http服务并查看日志文件

[root@localhost sbin]# systemctl restart httpd     ##重启http服务
[root@localhost sbin]# ls /var/log/httpd   ##查看日志文件
www.kgc.com.error_20191025.log

以上就是Apache的日志分割

谢谢阅读!!!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: