AWStats 是一套免费且中文化相当完整的流量分析工具,可以获取 Apache、FTP、Mail 等产生的记录文件再进行分析并产生清楚详细的图表。 以下是 CentOS 安装及配置 AWStats 的步骤:

☛ 安装 AWStats 流量分析套件

使用 yum 命令安装 AWStats 流量分析包,如下所示:

[root@localhost ~]# yum install awstats

☛ 设定 AWStats 套件

AWStats 安装完成后,会在 /etc/awstats/ 产生 awstats.localhost.localdomain.conf 的设定文件,假设你的主机网址名称是 www.x-family.idv.tw,那文件名就应该是awstats.www.x-family.idv.tw.conf ,将 awstats.localhost.localdomain.conf 复制成一个 awstats.www.x-family.idv.tw.conf 的文件,然后以 gedit 修改 awstats.www.www.x-family.idv.tw.conf 档案内的以下列设定,如下所示:

[root@localhost ~]# cd /etc/awstats
[root@localhost awstats]#cp awstats.localhost.localdomain.conf awstats.www.x-family.idv.tw.conf
[root@localhost awstats]#gedit awstats.www.x-family.idv.tw.conf

按序更改以下参数设置 :

LogFile="/var/log/httpd/access_log"
LogType=W
LogFormat=1
SiteDomain="www.x-family.idv.tw"
DirCgi="/awstats/"
DirIcons="/awstatsicons"
AllowToUpdateStatsFormBrowser=0
Lang="auto"

☛ 修改 Apache 的 AWStats 的配置文件内容

使用 gedit 编辑器修改 Apache 的 awstats 配置文件 /etc/httpd/conf.d/awstats.conf ,修改相关 awstats 执行目录及可连接读取分析文件的 IP 位置,如下所示:

Alias /awstatsclasses "/usr/share/awstats/wwwroot/classes/"
Alias /awstatscss "/usr/share/awstats/wwwroot/css/"
Alias /awstatsicons "/usr/share/awstats/wwwroot/icon/"
ScriptAlias /awstats/ "/usr/share/awstats/wwwroot/cgi-bin/"

<Directory "/usr/share/awstats/wwwroot">
     Options None
     AllowOverride None
     Order allow,deny
     Allow from ALL
</Directory>

设定完成后重启 Apache service 重新读取配置文件。

[root@localhost ~]# systemctl restart httpd

☛ 测试 AWStats 分析报告

使用下列的操作指令将报告输出至 /var/www/html/ 目录下,然后使用浏览器查看,如下所示:

[root@localhost ~]# cd /usr/share/awstats/wwwroot/cgi-bin/
[root@localhost ~]# perl awstats.pl -config=www.x-family.idv.tw -update -output > web.html
[root@localhost ~]# Chmod 755 /usr/share/awstats/wwwroot/cgi-bin/

使用浏览器输入网址 ( 如:http://www.x-family.idv.tw/ 或 http://www.x-family.idv.tw/cgi-bin/awstats.pl?config=www.x-family.idv.tw ),确认是否可以连接到 AWStats 分析网页?

如果浏览器出现 The requested URL /awstats.pl was not found on this server. 消息时,则将 /usr/share/awstats/wwwroot/ 目录下的 cgi-bin 复制一份至 /var/www/ 目录下即可排除这一情况,操作如下:

[root@localhost ~]# mkdir /var/www/cgi-bin
[root@localhost ~]# cp /usr/share/awstats/wwwroot/cgi-bin/*.* /var/www/cgi-bin/
[root@localhost ~]# chmod 755 /var/www/cgi-bin

这是因为 Apache 的配置文件默认 cgi-bin 的目录位置在 /var/www/cgi-bin 所致。

☛ 设定更新排程

设置每小时更新一次 AWStats,使用 gedit 在 /usr/sbin/ 下新增一个 awstats.sh 文件, 输入以下内容 :

perl /var/www/cgi-bin/awstats.pl -config=www.x-family.idv.tw -update

存档后,再以 gedit 修改 /etc/crontab ,加入以下的内容,如下所示:

0 * * * * root /usr/sbin/awstats.sh