当前位置:首页 > Nginx
Nginx 结合 Awstats 实现网站日志分析
来源:靑龍一笑的博客  作者:靑龍一笑  发布时间:2015-02-12 15:19:12  点击量:1233  评论:0

    Awstats 是一个基于 Perl 的 Web 日志分析工具,Awstats 的运行需要 Perl 环境的支持。
    首先,确认服务器是否安装 Perl:

[root@RicenOS ~]# perl -version

    进入到 awstats-7.4.tar.gz 文件所在目录,并解压:

[root@RicenOS ~]# cd /data/softwares/
[root@RicenOS softwares]# tar xvf awstats-7.4.tar.gz

    将解压后的目录移到 /data/awstats 目录,官方建议安装在 /usr/local/awstats 目录:

[root@RicenOS softwares]# mv awstats-7.4 /data/awstats

    修改 /data/awstats 目录的所有者和所属组:

[root@RicenOS softwares]# ls -ld /data/awstats/
drwxr-xr-x 5 sshd 1000 4096 01-04 22:26 /data/awstats/
[root@RicenOS softwares]# chown -R root:root /data/awstats/

    进入到 /data/awstats/tools 目录下,执行 awstats_configure.pl 文件:

[root@RicenOS softwares]# cd /data/awstats/tools/
[root@RicenOS tools]# ./awstats_configure.pl


    修改 Awstats 的配置文件:

[root@RicenOS tools]# vi /data/awstats/conf/awstats.www.ricensoftwares.com.cn.conf

    1)指定 Nginx 日志文件的路径:LogFile="/data/nginx/logs/www.ricensoftwares.com.cn.log"
    如果 Nginx 有做日志切割,则:LogFile="/data/nginx/logs/www.ricensoftwares.com.cn_%YYYY-24%MM-24%dd-24.log"
    2)修改 Awstats 的日志格式:LogFormat="%time1 %host %methodurl %code %bytesd %refererquot %uaquot"
    3)设置网站域名(手工配置的时候):SiteDomain=www.ricensoftwares.com.cn
    4)设置网站别名(手工配置的时候):HostAliases="ricensoftwares.com.cn www.ricensoftwares.com.cn 127.0.0.1 localhost"
    5)修改数据目录:DirData="/data/awstats/data"
    6)修改语言为中文:Lang="cn"
    创建 Awstats 的数据目录:

[root@RicenOS tools]# mkdir -p /data/awstats/data

    修改 awstats.pl 文件:

[root@RicenOS tools]# vi /data/awstats/wwwroot/cgi-bin/awstats.pl

    找到下面这段:

my @PossibleConfigDir = (
       "$DIR",
       "/etc/awstats",
       "/usr/local/etc/awstats", "/etc",
       "/etc/opt/awstats"
); 

    修改为:

my @PossibleConfigDir = (
       "$DIR",
       "/data/awstats/conf"
);

    创建存放 Awstats 静态页面的目录:

[root@RicenOS tools]# mkdir -p /data/websites/awstats

    修改 awstats_buildstaticpages.pl 文件:

[root@RicenOS tools]# vi /data/awstats/tools/awstats_buildstaticpages.pl

    找到下面这段:

else { @PossibleConfigDir=("$AwstatsDir","$DIR","/etc/awstats","/usr/local/etc/awstats","/etc","/etc/opt/awstats"); }

    修改为:

else { @PossibleConfigDir=("$AwstatsDir","$DIR","/data/awstats/conf"); }

    修改 awstats_updateall.pl 文件:

[root@RicenOS tools]# vi /data/awstats/tools/awstats_updateall.pl

    找到“my $DIRCONFIG = "/etc/awstats";”,修改为“my $DIRCONFIG = "/data/awstats/conf";”。
    找到下面这段:

elsif (-s "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl") {
    $Awstats="/usr/local/awstats/wwwroot/cgi-bin/awstats.pl";
    $AwstatsFound=1;
}

    修改为:

elsif (-s "/data/awstats/wwwroot/cgi-bin/awstats.pl") {
    $Awstats="/data/awstats/wwwroot/cgi-bin/awstats.pl";
    $AwstatsFound=1;
}

    修改 Nginx 的日志格式:

[root@RicenOS ~]# vi /data/nginx/conf/nginx.conf
log_format main '[$time_local] $remote_addr "$request" $status '
              '$body_bytes_sent "$http_referer" "$http_user_agent"'

    修改 Nginx 站点的配置文件:

[root@RicenOS tools]# vi /data/nginx/conf/vhosts/www.ricensoftwares.com.cn.conf

    1)按指定格式生成站点日志:access_log  logs/www.ricensoftwares.com.cn.log  main;
    2)代理 Awstats 访问地址:

location /awstats/ {
    index index.html;
    alias /data/websites/awstats/;
    access_log off;
    error_log off;
    charset gb2312;
}
location /icon/ {
    index index.html;
    alias /data/awstats/wwwroot/icon/;
    access_log off;
    error_log off;
    charset gb2312;
}

    重启 Nginx 服务:

[root@RicenOS tools]# service nginx restart

    为指定的站点生成数据:

[root@RicenOS tools]# /data/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.ricensoftwares.com.cn

    为所有网站生成数据:

[root@RicenOS tools]# /data/awstats/tools/awstats_updateall.pl now

    生成静态页面:

[root@RicenOS tools]# /data/awstats/tools/awstats_buildstaticpages.pl -update
> -config=www.ricensoftwares.com.cn -lang=cn -dir=/data/websites/awstats
> -awstatsprog=/data/awstats/wwwroot/cgi-bin/awstats.pl

    测试访问地址:http://www.ricensoftwares.com.cn/awstats/awstats.www.ricensoftwares.com.cn.html
    测试效果:

版权所有 © 2005-2023 靑龍一笑的博客  Powered by C.S.Ricen
Copyright © 2005-2023 by www.ricensoftwares.com.cn  All Rights Reserved.

欢迎光临本站,这里是靑龍一笑的博客。

因资金匮乏,本站已迁到国外的免费空间,可能导致本站的访问速度较慢,由此给您带来的不便,敬请谅解。

您可以通过下方的“支持本站建设”链接,给本站提供资金支持。

Free Web Hosting