Apache配置------日志分割及使用AWStats日志分析

前言

​ 随着网站的访问量增加,默认情况下apache的单个日志文件也会越来越大,日志文件占用磁盘空间很大,查看相关信息不方便。如果不对日志进行分割,那么只能一次将大的日志(如Apache的日志)整个删除,这样也丢失了很多对网站比较宝贵的信息,因为这些日志可以用来进行访问分析、网络安全监察、网络运行状况监控等,因此管理好这些海量的日志对网站的意义是很大的。

一:Apache日分隔

日志分割对于网站的意义重大,以下两种工具将apache的日志按每天的日期进行自动分割:Apache自带的rotatelogs分割工具与第三方工具cronolog分割日志

  • 日志文件在日积月累下,占用磁盘空间会很大
  • 查看相关信息会不方便

1.1:对日志文件进行分割的两种工具

  • Apache自带rotatelogs分割工具实现,我们只要添加配置文件来开启即可
  • 第三方工具cronolog分割

1.2:rotatelogs分割工具

  • 配置网站的日志文件转交给rotatelogs分割处理
  • 配置格式————
ErrorLog "| /usr/sbin/rotatelogs -l logs/error_%Y%m%d.log 86400"
CustomLog "| /usr/sbin/rotatelogs -l logs/access_%Y%m%d.log 86400" combined

在实际生产环境中,一个服务器绝大多数对应N个子域名站点,为了方便同意管理,可以用虚拟主机的方式进行配置,并用网站名标识日志文件
例如:ErrorLog “| rotatelogs(命令的绝对路径) -l 日志文件路径/网站名-error_%Y%m%d.log 86400”

ErrorLog 表示错误日志名;Customlog访问日志;/user/sbin/rotalogs 表示命令的绝对路径;-l 指定日志文件的路径 ;%Y%m%d.log 86400 表示按天的分割,一天时间为86400秒

二:rotatelogs分隔日志实操

1:首先安装Apache

[root@localhost ~]# yum -y install httpd
[root@localhost ~]# rpm -q httpd
httpd-2.4.6-93.el7.centos.x86_64
#找到rotatelogs的路径   复制一下一会要用
[root@localhost ~]# which rotatelogs
/usr/sbin/rotat:elogs

2:编辑httpd配置文件

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

mark

mark

3:重启服务查看端口

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# netstat -ntap | grep 80
tcp        0      0 20.0.0.41:80            0.0.0.0:*               LISTEN      86943/httpd         
tcp        0      0 0.0.0.0:6000            0.0.0.0:*               LISTEN      18020/X             
tcp6       0      0 :::6000                 :::*                    LISTEN      18020/X           

4:查看日志文件

  • 发现错误日志跟登录日志 如果不启动服务是没有日志文件的这里要注意下
[root@localhost ~]# ls /var/log/httpd/
access_log  error_log

5:配置rotatelogs分隔

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

mark
mark

6:重启服务

[root@localhost ~]# systemctl start httpd
#检查apache配置文件
[root@localhost ~]# httpd -t
Syntax OK
[root@localhost ~]# ls /var/log/httpd/
access_log  error_log

7:去宿主机刷新一下网页
mark

8:再次查看日志文件已经生成新的日志文件

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

二:cronolog分割日志实操

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

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

2.1:安装cronolog

[root@localhost ~]# cd /opt
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# ls
cronolog-1.6.2-14.el7.x86_64.rpm  rh
[root@localhost opt]# rpm -ivh cronolog-1.6.2-14.el7.x86_64.rpm
#复制软件路径
[root@localhost opt]# which cronolog 
/usr/sbin/cronolog

2.2:配置日志分隔

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

在这里插入图片描述
mark

2.3:重启服务

[root@localhost opt]# systemctl restart httpd
#里面就一个错误日志跟登录日志
[root@localhost httpd]# ls
access_log  error_log

2.4:去宿主机点击刷新一下
在这里插入图片描述

2.5:再次查看已经生成新的日志文件了

[root@localhost httpd]# ls
access_log  error_log  www.kgc.com.access_20200805.log
##更该一下时间
[root@localhost httpd]# date
20200805日 星期三 20:11:16 CST
[root@localhost httpd]# date -s 07/02/20
20200702日 星期四 00:00:00 CST

#再次查看日志文件   生成72号的  日志可以看见任意一天的文件比较方便
[root@localhost httpd]# ls
access_log  www.kgc.com.access_20200702.log  www.kgc.com.error_20200702.log
error_log   www.kgc.com.access_20200805.log

三:部署AWStats日志分析系统

AWStats 日志分析系统介绍

  • Perl语言开发的一款开源日志分析系统
  • 可用来分析Apache、Samba、Vsftpd、IIS等服务器的访问日志
  • 信息结合crond等计划任务服务,可对日志内容定期进行分析

操作步骤

3.1:安装DNS跟Apache服务

[root@localhost ~]# yum -y install bind httpd
..省略内容

3.2:配置DNS

  • /etc/named.conf ##主配置文件

  • /etc/named.rfc1912.zones ##区配置文件

  • /var/named/named.localhost ##区域数据配置文件

  • 配置主配置文件

[root@localhost ~]# vim /etc/named.conf
 11 
 12 options {
    
    
 13         listen-on port 53 {
    
     any; };  ##进来后把监听地址改为any
 14         listen-on-v6 port 53 {
    
     ::1; };
 15         directory       "/var/named";         ##目录
 16         dump-file       "/var/named/data/cache_dump.db";
 17         statistics-file "/var/named/data/named_stats.txt";
 18         memstatistics-file "/var/named/data/named_mem_stats.txt";
 19         recursing-file  "/var/named/data/named.recursing";
 20         secroots-file   "/var/named/data/named.secroots";
 21         allow-query     {
    
     any; };    ##把localhost改为any 任何都可以访问地址
  • 配置区域配置文件
zone "abc.com" IN {
    
    
        type master;
        file "abc.com.zone";
        allow-update {
    
     none; };
};
  • 配置区域数据
[root@tom03 ~]# cd /var/named/
[root@tom03 named]# ls
data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves
[root@tom03 named]# cp -p named.localhost abc.com.zone
[root@tom03 ~]# vim /var/named/abc.com.zone

mark

3.3:重启服务关闭安全功能

[root@localhost named]# setenforce 0
[root@localhost named]# iptables -F
[root@localhost named]# systemctl start named

3.4:客户机进行解析

mark
nslookup解析成功
mark

3.5:配置httpd

[root@tom03 ~]# vim /etc/httpd/conf/httpd.conf
#打开vipv4监听端口
Listen 20.0.0.43:80
#修改指定域名、S、
ServerName www.abc.com:80

[root@tom03 ~]# cd /var/www/html/
[root@tom03 html]# ls
[root@tom03 html]# vim index.html
#编写主页信息、、

<h1>hello</h1>

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

3.6:访问测试
mark

四:部署AWStats

[root@tom03 html]# rz -E
rz waiting to receive.
[root@tom03 html]# ls
awstats-7.6.tar.gz  index.html
[root@tom03 html]# mv awstats-7.6.tar.gz /opt
[root@tom03 html]# cd /opt
[root@tom03 opt]# ls
awstats-7.6.tar.gz  rh
#解压缩
[root@tom03 opt]# tar zxvf awstats-7.6.tar.gz 
[root@tom03 opt]# ls
awstats-7.6  awstats-7.6.tar.gz  rh
[root@tom03 opt]# mv awstats-7.6 /usr/local/awststs
[root@tom03 opt]# cd /usr/local/
[root@tom03 local]# ls
awststs  etc    include  lib64    sbin   src
bin      games  lib      libexec  share
[root@tom03 local]# cd awststs/
[root@tom03 awststs]# ls
docs  README.md  tools  wwwroot
[root@tom03 awststs]# cd tools/
[root@tom03 tools]# ls
awstats_buildstaticpages.pl  logresolvemerge.pl
awstats_configure.pl         maillogconvert.pl
awstats_exportlib.pl         nginx
awstats_updateall.pl         urlaliasbuilder.pl
dolibarr                     webmin
geoip_generator.pl           xslt
httpd_conf
#pl结尾的是脚本文件
[root@tom03 tools]# ./awstats_configure.pl 

mark

mark

mark

mark

一直回车键就行,

mark

[root@tom03 tools]# vim /etc/httpd/conf/httpd.conf
  • 下面是awstats写入的数据

mark

  • 对其修改
<Directory "/usr/local/awststs/wwwroot">
    Options None
    AllowOverride None
#    Order allow,deny          #添加注释
#    Allow from all            #添加注释
    Require all granted        #允许所有人可以访问
</Directory>
  • 修改站点统计配置文件
[root@tom03 tools]# cd /etc/awstats/
[root@tom03 awstats]# ls
awstats.www.abc.com.conf          #生成的新文件
[root@tom03 awstats]# vim awstats.www.abc.com.conf

mark

mark

  • dirdata 数据存储目录,因为这个目录不存在,所以创建这个目录
[root@tom03 lib]# mkdir awstats
#重启服务
[root@tom03 lib]# systemctl restart httpd
  • 下一步进行访问

http://localhost/awstats/awstats.pl?config=www.kgc.com中的localhost修改为域名

mark

  • 更新网页访问数据的统计
[root@tom03 lib]# cd /usr/local/awststs/
[root@tom03 awststs]# ls
docs  README.md  tools  wwwroot
[root@tom03 awststs]# cd tools/
[root@tom03 tools]# ls
awstats_buildstaticpages.pl  logresolvemerge.pl
awstats_configure.pl         maillogconvert.pl
awstats_exportlib.pl         nginx
awstats_updateall.pl         urlaliasbuilder.pl
dolibarr                     webmin
geoip_generator.pl           xslt
httpd_conf
#awstats_configure.pl 配置脚本    awstats_updateall.pl #更新数据
[root@localhost tools]# ./awstats_updateall.pl now
...省略内容
  • 再次访问网站,数据统计成功

mark

  • 配置周期性任务
[root@localhost tools]# crontab -e
'//添加一下内容'
*/4 * * * *     /usr/local/awstats/tools/awstats_updateall.pl now
[root@localhost tools]# systemctl start crond
You have new mail in /var/spool/mail/root
[root@localhost tools]# cd /var/www/html
#配置自动跳转
[root@localhost html]# vim xx.html
<html>
 <head>
   <meta http-equiv=refresh content="0;url=http://www.abc.com/awstats/awstats.pl?config=www.abc.com">
 </head>
 <body></body>
</html>
  • 重启服务
[root@localhost html]# systemctl restart httpd

mark

猜你喜欢

转载自blog.csdn.net/weixin_47151643/article/details/107971952
今日推荐