AWStats日志分析理论与实验

前言:

AWStats是免费的一个非常强大的日志分析工具的Apache日志文件。 从apache分析日志后,它以易于理解的图形格式显示它们。 AWStats是高级Web统计的缩写,它可以在命令行界面或CGI上运行。

一、AWStats概述
1.1 AWStats日志分析系统
  • Perl语言开发的一款开源日志分析系统
  • 可用来分析Apache、Samba、Vsftpd、IIS等服务器的访问日志
  • 信息结合crond等周期性计划任务服务,可对日志内容进行周期性分析
1.2 AWStats主要功能
  • AWStats可以同进站点以下信息

    访问量,访问次数,页面浏览量,点击数,数据流量等

    精确到每月、每日、每小时的数据

    访问者国家

    访问者IP

    Robots/Spiders的统计

    访客持续时间

    对不同File type的统计信息

    Pages-URL的统计

    访客操作系统浏览器等信息

    其他信息(搜索关键字等)

二、AWStats 实验
2.1 实验环境

两台虚拟机,一台配置httpd基础网站和DNS域名解析服务及AWStats日志分析系统,一台模拟用户机进行访问测试

2.2.1 安装dns、httpd服务
[root@localhost ~]# yum install bind httpd -y
2.2.2 配置DNS服务
  • 修改全局配置文件
[root@localhost ~]# vim /etc/named.conf
options {
        listen-on port 53 { any; };     ##将127.0.0.1改为any
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        recursing-file  "/var/named/data/named.recursing";
        secroots-file   "/var/named/data/named.secroots";
        allow-query     { any; };      ##将localhost改为any
..................省略部分内容
  • 修改区域配置文件
[root@localhost ~]# vim /etc/named.rfc1912.zones
zone "ceshi" IN {                          #添加此段信息
        type master;
        file "ceshi.com.zone";
        allow-update { none; };
};
..................省略部分内容
  • 修改区域数据配置文件
[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
chroot      data     dyndb-ldap  named.empty      named.loopback
chroot_sdb  dynamic  named.ca    named.localhost  slaves
[root@localhost named]# cp -p named.localhost ceshi.com.zone 
[root@localhost named]# vim ceshi.com.zone 
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
wwww IN A       192.168.181.154    #将原本IPV6的数据删除,添加本地地址
  • 关闭防火墙、增强型安全功能,然后重启服务
[root@localhost named]# systemctl stop firewalld.service 
[root@localhost named]# setenforce 0
[root@localhost named]# systemctl start named
2.2.3 使用win10虚拟机上测试

在win10网卡上指定dhs地址为192.168.181.154
在这里插入图片描述

解析地址www.ceshi.com
在这里插入图片描述

2.2.4 配置httpd服务
[root@localhost named]# vim /etc/httpd/conf/httpd.conf 
Listen 192.168.181.154:80       ##找到监听地址,开启监听并将地址改为本地IP
#Listen 80                    	##关闭ipv6监听端口
..............省略部分内容
ServerName www.ceshi.com:80     ##开启服务名,并将域名改为www.ceshi.com
..............省略部分内容
##保存退出后开启httd服务
[root@localhost named]# systemctl start httpd
2.2.4 在win10上测试

直接在浏览器中输入www.ceshi.com

在这里插入图片描述

3.1 配置AWStats
3.1.1 下载、共享、挂载AWStats
  • 在主机上下载AWStats软件包并放入LAMP-C7共享文件夹中
  • 在linux虚拟机上挂载共享文件夹
[root@localhost named]# mkdir /abc
[root@localhost named]# mount.cifs //192.168.181.1/LAMP-C7 /abc #地址为宿主机IP地址
Password for root@//192.168.181.1/LAMP-C7:  
[root@localhost named]# cd /abc
[root@localhost abc]# ls
apr-1.6.2.tar.gz       cronolog-1.6.2-14.el7.x86_64.rpm  mysql-5.6.26.tar.gz
apr-util-1.6.0.tar.gz  Discuz_X2.5_SC_UTF8.zip           php-5.6.11.tar.bz2
awstats-7.6.tar.gz     httpd-2.4.29.tar.bz2
3.1.2 配置AWStats
  • 解压AWStats软件包到/opt目录中
[root@localhost abc]# tar zxvf awstats-7.6.tar.gz -C /opt
awstats-7.6/
awstats-7.6/tools/
awstats-7.6/tools/awstats_buildstaticpages.pl
.............省略部分内容
  • 将/opt下的awstats-7.6 移动到/usr/local中并重命名为awstats
[root@localhost abc]# cd /opt
[root@localhost opt]# ls
awstats-7.6  rh
[root@localhost opt]# mv awstats-7.6/ /usr/local/awstats
[root@localhost opt]# cd /usr/local/
[root@localhost local]# ls
awstats  bin  etc  games  include  lib  lib64  libexec  sbin  share  src 

此处需注意,如果移动awstats-7.6到/usr/local/中未改名,稍后更新日志分析的时候可能会报错。

  • 进入awstats进行配置

进行配置AWStats服务前一定要先开启httpd服务,否则会配置失败

[root@localhost local]# cd awstats/
[root@localhost awstats]# cd tools
[root@localhost tools]# ls
awstats_buildstaticpages.pl  dolibarr            maillogconvert.pl   xslt
awstats_configure.pl         geoip_generator.pl  nginx
awstats_exportlib.pl         httpd_conf          urlaliasbuilder.pl
awstats_updateall.pl         logresolvemerge.pl  webmin
[root@localhost tools]# ./awstats_configure.pl 
[root@localhost tools]# ./awstats_configure.pl 
..........................省略部分内容
-----> Check for web server install

Enter full config file path of your Web server.
Example: /etc/httpd/httpd.conf
Example: /usr/local/apache2/conf/httpd.conf
Example: c:\Program files\apache group\apache\conf\httpd.conf
Config file path ('none' to skip web server setup):
> /etc/httpd/conf/httpd.conf	 ####输入apache配置文件路径

..........................省略部分内容
-----> Need to create a new config file ?
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ? y    ###提示“是否建立新的httpd的配置文件”这里我们选择yes

-----> Define config file name to create
What is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Your web site, virtual server or profile name:
> www.ceshi.com    ##输入域名

-----> Define config file path
In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directory path to store config file(s) (Enter for default):
>      ###提示“是否选择默认的配置文件目录”此处直接回车

-----> Create config file '/etc/awstats/awstats.www.ceshi.com.conf'
 Config file /etc/awstats/awstats.www.ceshi.com.conf created.

-----> Restart Web server with '/sbin/service httpd restart'
Redirecting to /bin/systemctl restart httpd.service

..........................省略部分内容
A SIMPLE config file has been created: /etc/awstats/awstats.www.ceshi.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.ceshi.com' with command:
> perl awstats.pl -update -config=www.ceshi.com
You can also read your statistics for 'www.ceshi.com' with URL:
> http://localhost/awstats/awstats.pl?config=www.ceshi.com
#####复制以上的路径
Press ENTER to finish...
####完成配置
[root@localhost tools]# 
  • 修改apache配置文件
[root@localhost tools]# cd /etc/httpd/conf
[root@localhost conf]# ls
httpd.conf  magic
[root@localhost conf]# vim httpd.conf 
##输入G跳转到尾行
Alias /awstatsclasses "/usr/local/awstats/wwwroot/classes/"
Alias /awstatscss "/usr/local/awstats/wwwroot/css/"
Alias /awstatsicons "/usr/local/awstats/wwwroot/icon/"
ScriptAlias /awstats/ "/usr/local/awstats/wwwroot/cgi-bin/"
#######配置完AWStats后,会自动将以上AWStats的控制写入到apache配置文件中,
#
# This is to permit URL access to scripts/files in AWStats directory.
#
<Directory "/usr/local/awstats/wwwroot">
    Options None
    AllowOverride None
   #Order allow,deny     ###注释掉此权限配置
   #Allow from all       ###注释掉此权限配置
    Require all granted   ##添加允许所有人访问
</Directory>

--------》wq(保存)
  • 修改AWStats配置文件
[root@localhost conf]# cd /etc/awstats/
[root@localhost awstats]# ls
awstats.www.ceshi.com.conf
[root@localhost awstats]# vim awstats.www.ceshi.com.conf 
#输入/LogFile找到监控的日志文件对象
LogFile="/var/log/httpd/access_log"  ##修改监控的日志文件,指向apache的访问日志文件
DirData="/var/lib/awstats"   ###查看数据目录
------------》wq(保存)
  • 进入/var/lib创建awstats目录给AWStats存放数据
[root@localhost awstats]# cd /var/lib
[root@localhost lib]# mkdir awstats
  • 重启httpd服务
[root@localhost lib]# systemctl restart httpd
3.1.2 使用win10虚拟机进行测试

在浏览器中输入我们之前在配置AWStats时复制的路径:http://localhost/awstats/awstats.pl?config=www.ceshi.com (注意要将local改为www.ceshi.com)

:http://www.ceshi.com/awstats/awstats.pl?config=www.ceshi.com

访问页面如下
在这里插入图片描述

3.1.3 更新日志分析数据
[root@localhost lib]# cd /usr/local/awstats/
[root@localhost awstats]# ls
docs  README.md  tools  wwwroot
[root@localhost awstats]# cd tools
[root@localhost tools]# ls
awstats_buildstaticpages.pl  dolibarr            maillogconvert.pl   xslt
awstats_configure.pl         geoip_generator.pl  nginx
awstats_exportlib.pl         httpd_conf          urlaliasbuilder.pl
awstats_updateall.pl         logresolvemerge.pl  webmin
[root@localhost tools]# ./awstats_updateall.pl now   ##执行日志数据分析更新(刷新)
Running '"/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -update -config=www.ceshi.com -configdir="/etc/awstats"' to update config www.ceshi.com
Create/Update database for config "/etc/awstats/awstats.www.ceshi.com.conf" by AWStats version 7.6 (build 20161204)
From data in log file "/var/log/httpd/access_log"...
Phase 1 : First bypass old records, searching new record...
Searching new records from beginning of log file...
Phase 2 : Now process new records (Flush history on disk after 20000 hosts)...
Jumped lines in file: 0
Parsed lines in file: 98
 Found 0 dropped records,
 Found 0 comments,
 Found 0 blank records,
 Found 1 corrupted records,
 Found 0 old records,
 Found 97 new qualified records.
  • 返回win10虚拟机刷新网页

更新后数据如下
在这里插入图片描述

3.2 设置周期性计划任务

将日志数据分析周期性计划任务

[root@localhost tools]# crontab -e
*/2 * * * * /usr/local/awstats/tools/awstats_updateall.pl now
###每隔2分钟更新日志数据分析
-----》wq
[root@localhost tools]# systemctl start crond  ##开启周期性计划任务
3.3 优化网页网址
[root@localhost tools]# cd /var/www/html/
您在 /var/spool/mail/root 中有邮件
[root@localhost html]# ls
[root@localhost html]# vim aws.html
<html>
<head>
   <meta http-equiv=refresh content="0;url=http://www.ceshi.com/awstats/awstats.pl?config=www.ceshi.com">
 </head>
 <body></body>
</html>
---------->保存
  • 返回win10,重新打开浏览器,输入http://www.ceshi.com/aws.html

显示页面如下
在这里插入图片描述

总结

需注意:在本篇博客中也有过强调,开始配置AWStats日志分析服务的时候,最好更改文件包命名,以防之后更新数据时出现erorr。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值