阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

日志分析工具Awstats实战之Nginx篇

158次阅读
没有评论

共计 31883 个字符,预计需要花费 80 分钟才能阅读完成。

前言:
Awstats 是在 SourceForge 上发展很快的一个基于 Perl 的 WEB 日志分析工具,一个充分的日志分析让 Awstats 显示您下列资料:
访问次数、独特访客人数,
访问时间和上次访问,
使用者认证、最近认证的访问,
每周的高峰时间 (页数, 点击率, 每小时和一周的千字节),
域名 / 国家的主机访客 (页数, 点击率, 字节,269 域名 / 国家检测, geoip 检测),
主机名单, 最近访问和未解析的 IP 地址名单
大多数看过的进出页面,
档案类型,
网站压缩统计表 (mod_gzip 或者 mod_deflate),
使用的操作系统 (每个操作系统的页数, 点击率 , 字节, 35 OS detected),
使用的浏览器,
机器人访问 (检测 319 个机器人),
蠕虫攻击 (5 个蠕虫家族),
搜索引擎,利用关键词检索找到你的地址,
HTTP 协议错误 (最近查阅没有找到的页面),
其他基于 URL 的个性报导, 链接参数, 涉及综合行销领域目的.
贵网站被加入 ” 最喜爱的书签 ”. 次数.
屏幕大小 (需要在索引页补充一些 HTML 标签).
浏览器的支持比例: Java, Flash, RealG2 reader, Quicktime reader, WMA reader, PDF reader.
负载平衡服务器比率集群报告.

Awstats 的运行是需要 PERL 环境的支持,从 awstats 的文档来看,它对 Apache HTTP Server 的支持是非常完美的,而当我们把 Web 服务器换成 Nginx 后,要运行 awstats 变得很麻烦。首先 Nginx 本身对 Perl 的支持是比较弱的,甚至官方也不建议使用;另外在日志格式上有需要修改后才能运行。
使用 awstats 可以分析 apache 日志,同样也可以分析 nginx 日志。本文将详细介绍自动定时切割 nginx 的访问日志,并使用 awstats 来定时分析 nginx 日志及实现统计结果可供安全便捷的查阅。

环境:
CentOS 6.4 x86_64
ip:192.168.1.113
域名:www.linuxidc.com(server 和 client 都通过 hosts 文件解析)
nginx-1.2.9 编译安装,路径 /usr/local/nginx,服务开启状态
日志记录格式为 nginx 默认的,切勿更改,否则会造成 awstats 无法分析日志。
log_format  main  ‘$remote_addr – $remote_user [$time_local] “$request” ‘
                  ‘$status $body_bytes_sent “$http_referer” ‘
                  ‘”$http_user_agent” “$http_x_forwarded_for”‘;
awstats-7.2.tar.gz

一、日志自动切割
对于 nginx 的日志切割,由于没有像 apache 一样去用 cronolog 工具,这里我们就写一个脚本,让它可以在每天 00:01 自动执行,切割昨天的日志(交由 awstats 分析),压缩前天的日志(压缩日志可减小存储空间,为防止 awstats 没有分析完就被压缩,所以只压缩前天的日志)。

vim /server/scripts/cut_nginx_log.sh

输入以下内容:

#!/bin/sh
yesterday=`date -d “yesterday” +”%Y%m%d”`
before_yesterday=`date -d “-2 day” +”%Y%m%d”`
Nginx_Dir=”/usr/local/nginx”
Nginx_logs=”/app/logs”
Log_Name=”www_access”
cd /tmp
[-d $Nginx_Logs] && cd $Nginx_logs || exit 1
[-f $Log_Name.log] && /bin/mv $Log_Name.log ${Log_Name}_${yesterday}.log || exit 1
if [$? -eq 0 -a -f $Nginx_Dir/logs/nginx.pid]
    then
      kill -USR1 `cat $Nginx_Dir/logs/nginx.pid`
fi
[-f  ${Log_Name}_${before_yesterday}.log ] && /usr/bin/gzip ${Log_Name}_${before_yesterday}.log|| exit 1

执行 crontab - e 将该脚本加入定时任务中

1 0 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1

这样每天凌晨 00:01 就能自动实现日志的切割,压缩等功能了

因为本次实验下的 nginx 此时已经有日志了,另外为了后文 awstats 能对切割过的日志进行分析,所以这里我们要运行一下此脚本,来将现有日志进行切割生成昨天的日志方便后文操作。

/bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1

二、Awstats 的安装与配置

1. 部署 awstats
首先我们要下载 awstats 软件包,并将其放在常规目录(/usr/local)下

wget http://awstats.sourceforge.net/files/awstats-7.2.tar.gz
tar zxf awstats-7.2.tar.gz
mv awstats-7.2 /usr/local/awstats

由于 wget 下载下来的包中权限是非 root 的,所以这里要修改权限,否则稍后 *.pl 将无法运行

chown -R root.root /usr/local/awstats
chmod +x /usr/local/awstats/tools/*.pl
chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl

接下来我们要执行 awstats/tools 下的 awstats_configure.pl 配置向导,用来生成 awstats 的配置文件,awstats 配置文件的命名规则是 awstats.website.conf。

cd /usr/local/awstats/tools/
./awstats_configure.pl

此时会出现如下提示
—– AWStats awstats_configure 1.0 (build 1.9) (c) Laurent Destailleur —–
This tool will help you to configure AWStats to analyze statistics for
one web server. You can try to use it to let it do all that is possible
in AWStats setup, however following the step by step manual setup
documentation (docs/index.html) is often a better idea. Above all if:
– You are not an administrator user,
– You want to analyze downloaded log files without web server,
– You want to analyze mail or ftp log files instead of web log files,
– You need to analyze load balanced servers log files,
– You want to ‘understand’ all possible ways to use AWStats…
Read the AWStats documentation (docs/index.html).
—–> Running OS detected: Linux, BSD or Unix
—–> 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):
> none      #这里让填写网页服务器的配置文件路径,因为我们用的不是 apache,所以这里要填 none
Your web server config file(s) could not be found.
You will need to setup your web server manually to declare AWStats
script as a CGI, if you want to build reports dynamically.
See AWStats setup documentation (file docs/index.html)
—–> Update model config file ‘/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf’
  File awstats.model.conf updated.
—–> 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        #询问是否创建一个新的配置文件,这里填 y
—–> 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.linuxidc.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):
>              #这里要填写你配置文件存放路径,我们使用它默认的路径 /etc/awstats,所以直接回车即可
—–> Create config file ‘/etc/awstats/awstats.www.linuxidc.com.conf’
 Config file /etc/awstats/awstats.www.linuxidc.com.conf created.
—–> Add update process inside a scheduler
Sorry, configure.pl does not support automatic add to cron yet.
You can do it manually by adding the following command to your cron:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.linuxidc.com
Or if you have several config files and prefer having only one command:
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue…  #提示不能自动加入 crontab 定时任务,需要稍后自己添加,我们按回车继续即可
A SIMPLE config file has been created: /etc/awstats/awstats.www.linuxidc.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for ‘www.linuxidc.com’ with command:
> perl awstats.pl -update -config=www.linuxidc.com
You can also build static report pages for ‘www.linuxidc.com’ with command:
> perl awstats.pl -output=pagetype -config=www.linuxidc.com
Press ENTER to finish… #提示配置文件创建完成和如何更新配置及建立静态报告页,这里我们回车即可结束这个配置向导

2、修改 awstats 配置文件
完成配置文件的创建之后,我们还需要对 /etc/awstats/awstats.www.linuxidc.com.conf 里的一些参数进行修改。
sed -i ‘s#LogFile=”/var/log/httpd/mylog.log”#LogFile=”/app/logs/www_access_%YYYY-24%MM-24%DD-24.log”#g’ /etc/awstats/awstats.www.linuxidc.com.conf

这里更改的目的是指定 awstats 需要分析的 nginx 的日志文件路径。这里的路径大家要按自己的日志路径来填。
sed -i ‘s#DirData=”/var/lib/awstats”#DirData=”/usr/local/awstats/data”#g’ /etc/awstats/awstats.www.linuxidc.com.conf

这里更改的目的是指定 awstats 的数据库配置文件(即 awstats 的数据库 (纯文本))。
由于,此处没有 /usr/local/awstats/data 目录,所以我们要创建出来

mkdir /usr/local/awstats/data

以上的两个替换操作进行完之后一定要用命令查看替换是否成功,以便及早发现纰漏。

grep “LogFile=” /etc/awstats/awstats.www.linuxidc.com.conf
grep “DirData=” /etc/awstats/awstats.www.linuxidc.com.conf

查询替换结果正确之后,即可进行下面的步骤。

3、创建 awstats 统计结果存放目录
现在我们要创建 awstats 统计结果的数据库存放目录
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.linuxidc.com

如果屏幕输出类似下面的提示就说明配置文件都正确无误了
Create/Update database for config “/etc/awstats/awstats.www.linuxidc.com.conf” by AWStats version 7.2 (build 1.992)
From data in log file “/app/logs/www_access.log”…
Phase 1 : First bypass old records, searching new record…
Searching new records from beginning of log file…
Jumped lines in file: 0
Parsed lines in file: 0
 Found 0 dropped records,
 Found 0 comments,
 Found 0 blank records,
 Found 0 corrupted records,
 Found 0 old records,
 Found 0 new qualified records.

注释:awstats.pl 会到 /etc/awstats 目录下搜索,根据读取到的配置文件运行程序,去读取 -config 中的参数,把 www.linuxidc.com 扩展成 awstats.www.linuxidc.com.conf
分析日志:运行后将这样的日志统计结果归档到一个 awstats 的数据库(纯文本)里;
然后是输出:分两种形式
1、一种是通过 cgi 程序读取统计结果数据库输出;
2、一种是运行后台脚本将输出导出成静态文件;
统计分析完成后,结果还在 Awstats 的数据库中。在 Apache 上,可以直接打开 Perl 程序的网页查看统计。但 Nginx 对 Perl 支持并不好,所以要换个方法,利用 awstats 的工具将统计的结果生成静态文件,这里方便我们还是用脚本来实现
vim /server/scripts/awstats.sh

输入以下内容
#!/bin/sh
Awstats_Dir=”/usr/local/awstats”
[-d /www/awstats]||mkdir /www/awstats
$Awstats_Dir/wwwroot/cgi-bin/awstats.pl -update -config=www.linuxidc.com
$Awstats_Dir/tools/awstats_buildstaticpages.pl -update -config=www.linuxidc.com -awstatsprog=$Awstats_Dir/wwwroot/cgi-bin/awstats.pl -lang=cn -dir=/www/awstats

脚本内容讲解:
/usr/local/awstats/tools/awstats_buildstaticpages.pl    Awstats 静态页面生成工具
-update -config=www.linuxidc.com  更新配置项
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl  Awstats 日志更新程序路径
-lang=cn    语言为中文
-dir= /www/awstats  统计结果输出目录

awstats_buildstaticpages.pl 会根据 -config 的参数去读取里面的 DirData 路径下的数据库配置文件,然后生成静态的 html 文件,生成的文件重定向到 /www/awstats 目录下。

三、配置 nginx 实现安全访问
接下来我们要配置 nginx 使其能安全的访问到分析的数据
vim /usr/local/nginx/conf/nginx.conf

在 server{}内添加如下内容:
server {
        listen      80;
        server_name  www.linuxidc.com;
        location / {
            root  /www/linuxidc;
            index  index.html index.htm;
            access_log /app/logs/www_access.log commonlog;
}
location ~ ^/awstats/ {
            root  /www/;
            index  awstats.www.linuxidc.com.html;          #根据自己的网站域名进行更改首页文件
            autoindex on;
            access_log off;
            charset gb2312;
            auth_basic “Restricted”;                                          #有些网站不愿意公开网站流量信息,所以加个认证
            auth_basic_user_file /usr/local/nginx/htpasswd.pass;  #该文件由 apache 的加密认证工具 htpasswd 创建
}
location ~ ^/icon/ {
            root  /usr/local/awststs/wwwroot;
            index  index.html;
            access_log off;
            charset gb2312;
}
    }

由于 nginx 没有好的加密认证工具,需要借助 apache 的 htpasswd 来实现加密认证功能
htpasswd -c -m /usr/local/nginx/htpasswd.pass linuxidcadmin      #用户名为 linuxidcadmin

配置完毕之后,检查 nginx 语法,然后优雅重启之后,用游览器访问 www.linuxidc.com/awstats,输入账号密码之后即可查看统计信息了。
至此,awstats 已经可以实现对 Nginx 的日志统计及静态化的安全访问功能了。
四、配置 awstats 自动运行
为了让整个日志的统计过程可以实现自动化,将 awstats.sh 脚本加入 crontab 定时任务中去,此时结合上面的定时切割任务,我们的 crontab 里面会有多出来两条定时任务
1 0 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1
0 1 * * * /bin/sh /server/scripts/awstats.sh >/dev/null 2>&1

到此,我们整个日志访问工具 awstats 在 nginx 上的配置就完成了。当然此篇是基于静态页面的显示的,后面会再写一篇博文日志分析工具 Awstats 实战之 Nginx 篇 - 分析结果动态化出来和大家一起交流学习的,见 http://www.linuxidc.com/Linux/2013-10/92150p2.htm。

推荐阅读:

CentOS 6.3 下 AWStats+GeoIP 实现查看网站访问归属地 http://www.linuxidc.com/Linux/2013-06/85984.htm

Nginx 日志分析 AWStats + JAWStats 安装配置 http://www.linuxidc.com/Linux/2013-06/85567.htm

经典日志分析工具 -AWStats http://www.linuxidc.com/Linux/2012-12/77080.htm

AWStats 日志系统配置文件和错误归纳 http://www.linuxidc.com/Linux/2012-12/75657.htm

使用 AWStats 分析网站日志 - 强大的日志分析工具 http://www.linuxidc.com/Linux/2012-11/74431.htm

AWStats 的详细介绍:请点这里
AWStats 的下载地址:请点这里

上一篇博文“分析工具 Awstats 实战之 Nginx 篇 - 分析结果静态化 http://www.linuxidc.com/Linux/2013-10/92150.htm”介绍了如何将 awstats 的日志分析信息用静态页面来进行显示,不过显示效果肯定没有动态的好啦。本篇博文将带大家一起来部署动态的分析结果查阅。

环境:
CentOS 6.4
ip:192.168.1.113
域名:www.linuxidc.com(server 和 client 都通过 hosts 文件解析)
nginx-1.2.9 编译安装,路径 /usr/local/nginx,服务开启状态
日志记录格式为 nginx 默认的,切勿更改,否则会造成 awstats 无法分析日志。
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
                ‘$status $body_bytes_sent “$http_referer” ‘
                ‘”$http_user_agent” “$http_x_forwarded_for”‘;
awstats-7.2.tar.gz CPAN-2.00.tar.gz FCGI-0.74.tar.gz FCGI-ProcManager-0.24.tar.gz
必须有 perl-devel,不然无法编译 FCGI。

一、日志自动切割

对于 nginx 的日志切割,由于没有像 apache 一样去用 cronolog 工具,这里我们就写一个脚本,让它可以在每天 00:01 自动执行,切割昨天的日志(交由 awstats 分析),压缩前天的日志(压缩日志可减小存储空间,为防止 awstats 没有分析完就被压缩,所以只压缩前天的日志)。
vim /server/scripts/cut_nginx_log.sh

输入以下内容:

#!/bin/sh
yesterday=`date -d “yesterday” +”%Y%m%d”`
before_yesterday=`date -d “-2 day” +”%Y%m%d”`
Nginx_Dir=”/usr/local/nginx”
Nginx_logs=”/app/logs”
Log_Name=”www_access”
cd /tmp
[-d $Nginx_Logs] && cd $Nginx_logs || exit 1
[-f $Log_Name.log] && /bin/mv $Log_Name.log ${Log_Name}_${yesterday}.log || exit 1
if [$? -eq 0 -a -f $Nginx_Dir/logs/nginx.pid]
    then
      kill -USR1 `cat $Nginx_Dir/logs/nginx.pid`
fi
[-f  ${Log_Name}_${before_yesterday}.log ] && /usr/bin/gzip ${Log_Name}_${before_yesterday}.log|| exit 1

执行 crontab - e 将该脚本加入定时任务中

1 0 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1

这样每天凌晨 00:01 就能自动实现日志的切割,压缩等功能了。

因为本次实验下的 nginx 此时已经有日志了,另外为了后文 awstats 能对切割过的日志进行分析,所以这里我们要运行一下此脚本,来将现有日志进行切割生成昨天的日志方便后文操作。

/bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1

二、配置 FCGI

1、安装 CPAN
wget http://search.cpan.org/CPAN/authors/id/A/AN/ANDK/CPAN-2.00.tar.gz
tar zxf CPAN-2.00.tar.gz
cd CPAN-2.00
perl Makefile.PL
make && make install

2、安装 FCGI 和 FCGI::ProcManager

wget http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/FCGI-0.74.tar.gz
tar zxf FCGI-0.74.tar.gz
cd FCGI-0.74
第一种安装方法:perl -MCPAN -e ‘install FCGI’
第二种安装方法:perl Makefile.PL
                make&&make install
wget http://search.cpan.org/CPAN/authors/id/B/BO/BOBTFISH/FCGI-ProcManager-0.24.tar.gz
tar zxf FCGI-ProcManager-0.24.tar.gz
cd FCGI-ProcManager-0.24
第一种安装方法:perl -MCPAN -e ‘install FCGI::ProcManager’
第二种安装方法:perl Makefile.PL
                make&&make install

在执行第一种安装方法的时候,一定是全程自动滚动下来提示 OK 的。如果出现提示你输入 yes 之类的,你需要按提示操作完之后,再运行第二次直到全程自动滚动下来提示 OK 才为完成安装。或者你就用第二种方法来执行安装。
3、创建 FCGI 启动文件

vi /usr/local/nginx/sbin/fcgi      #此处按个人习惯命名
#!/usr/bin/perl
use FCGI;
#perl -MCPAN -e ‘install FCGI’
use Socket;
use POSIX qw(setsid);
#use Fcntl;
require ‘syscall.ph’;
&daemonize;
#this keeps the program alive or something after exec’ing perl scripts
END() {} BEGIN() {}
*CORE::GLOBAL::exit = sub {die “fakeexit\nrc=”.shift().”\n”; };
eval q{exit};
if ($@) {
        exit unless $@ =~ /^fakeexit/;
};
&main;
sub daemonize() {
    chdir ‘/’                or die “Can’t chdir to /: $!”;
    defined(my $pid = fork)  or die “Can’t fork: $!”;
    exit if $pid;
    setsid                    or die “Can’t start a new session: $!”;
    umask 0;
}
sub main {
        #$socket = FCGI::OpenSocket(“127.0.0.1:8999”, 10);
        $socket = FCGI::OpenSocket(“/usr/local/nginx/fastcgi_temp/perl_cgi-dispatch.sock”, 10);
#use UNIX sockets – user running this script must have w access to the ‘nginx’ folder!!
        $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket);
        if ($request) {request_loop()};
            FCGI::CloseSocket($socket);
}
sub request_loop {
        while($request->Accept() >= 0 ) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
          #processing any STDIN input from WebServer (for CGI-POST actions)
          $stdin_passthrough =”;
          $req_len = 0 + $req_params{‘CONTENT_LENGTH’};
          if (($req_params{‘REQUEST_METHOD’} eq ‘POST’) && ($req_len != 0) ){
                my $bytes_read = 0;
                while ($bytes_read < $req_len) {
                        my $data = ”;
                        my $bytes = read(STDIN, $data, ($req_len – $bytes_read));
                        last if ($bytes == 0 || !defined($bytes));
                        $stdin_passthrough .= $data;
                        $bytes_read += $bytes;
                }
            }
            #running the cgi app
            if ((-x $req_params{SCRIPT_FILENAME}) && #can I execute this?
                (-s $req_params{SCRIPT_FILENAME}) && #Is this file empty?
                (-r $req_params{SCRIPT_FILENAME})    #can I read this file?
            ){
                pipe(CHILD_RD, PARENT_WR);
                my $pid = open(KID_TO_READ, “-|”);
                unless(defined($pid)) {
                        print(“Content-type: text/plain\r\n\r\n”);
                        print “Error: CGI app returned no output – Executing $req_params
{SCRIPT_FILENAME} failed !\n”;
                        next;
                }
                if ($pid > 0) {
                        close(CHILD_RD);
                        print PARENT_WR $stdin_passthrough;
                        close(PARENT_WR);
                        while(my $s = <KID_TO_READ>) {print $s;}
                        close KID_TO_READ;
                        waitpid($pid, 0);
                } else {
                        foreach $key (keys %req_params){
                          $ENV{$key} = $req_params{$key};
                        }
                        # cd to the script’s local directory
                        if ($req_params{SCRIPT_FILENAME} =~ /^(.*)\/[^\/]+$/) {
                                chdir $1;
                        }
                        close(PARENT_WR);
                        close(STDIN);
                        #fcntl(CHILD_RD, F_DUPFD, 0);
                        syscall(&SYS_dup2, fileno(CHILD_RD), 0);
                        #open(STDIN, “<&CHILD_RD”);
                        exec($req_params{SCRIPT_FILENAME});
                        die(“exec failed”);
                }
            }
            else {
                print(“Content-type: text/plain\r\n\r\n”);
                print “Error: No such CGI app – $req_params{SCRIPT_FILENAME} may not exist or is
not executable by this process.\n”;
            }
        }
}

创建完成后,需要赋予 fcgi 执行权限:
chmod 755 /usr/local/nginx/sbin/fcgi

启动 FPM(FastCGI 进程管理器)
perl /usr/local/nginx/sbin/fcgi >/dev/null 2>$1

在这里,Nginx 需要对 fcgi 生成的 /usr/local/nginx/fastcgi_temp/perl_cgi-dispatch.sock 有读写权限,否则会报 502 错误。
三、Awstats 的安装与配置
1、部署 awstats
首先我们要下载 awstats 软件包,并将其放在常规目录(/usr/local)下
wget http://awstats.sourceforge.net/files/awstats-7.2.tar.gz
tar zxf awstats-7.2.tar.gz
mv awstats-7.2 /usr/local/awstats

由于 wget 下载下来的包中权限是非 root 的,所以这里要修改权限,否则稍后 *.pl 将无法运行
chown -R root.root /usr/local/awstats
chmod +x /usr/local/awstats/tools/*.pl
chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl

接下来我们要执行 awstats/tools 下的 awstats_configure.pl 配置向导,用来生成 awstats 的配置文件,awstats 配置文件的命名规则是 awstats.website.conf
12 cd /usr/local/awstats/tools/
./awstats_configure.pl

此时会出现如下提示

—– AWStats awstats_configure 1.0 (build 1.9) (c) Laurent Destailleur —–
This tool will help you to configure AWStats to analyze statistics for
one web server. You can try to use it to let it do all that is possible
in AWStats setup, however following the step by step manual setup
documentation (docs/index.html) is often a better idea. Above all if:
– You are not an administrator user,
– You want to analyze downloaded log files without web server,
– You want to analyze mail or ftp log files instead of web log files,
– You need to analyze load balanced servers log files,
– You want to ‘understand’ all possible ways to use AWStats…
Read the AWStats documentation (docs/index.html).
—–> Running OS detected: Linux, BSD or Unix
—–> 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):
> none      #这里让填写网页服务器的配置文件路径,因为我们用的不是 apache,所以这里要填 none
Your web server config file(s) could not be found.
You will need to setup your web server manually to declare AWStats
script as a CGI, if you want to build reports dynamically.
See AWStats setup documentation (file docs/index.html)
—–> Update model config file ‘/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf’
  File awstats.model.conf updated.
—–> 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        #询问是否创建一个新的配置文件,这里填 y
—–> 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.linuxidc.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):
>              #这里要填写你配置文件存放路径,我们使用它默认的路径 /etc/awstats,所以直接回车即可
—–> Create config file ‘/etc/awstats/awstats.www.linuxidc.com.conf’
 Config file /etc/awstats/awstats.www.linuxidc.com.conf created.
—–> Add update process inside a scheduler
Sorry, configure.pl does not support automatic add to cron yet.
You can do it manually by adding the following command to your cron:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.linuxidc.com
Or if you have several config files and prefer having only one command:
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue…  #提示不能自动加入 crontab 定时任务,需要稍后自己添加,我们按回车继续即可
A SIMPLE config file has been created: /etc/awstats/awstats.www.linuxidc.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for ‘www.linuxidc.com’ with command:
> perl awstats.pl -update -config=www.linuxidc.com
You can also build static report pages for ‘www.linuxidc.com’ with command:
> perl awstats.pl -output=pagetype -config=www.linuxidc.com
Press ENTER to finish… #提示配置文件创建完成和如何更新配置及建立静态报告页,这里我们回车即可结束这个配置向导

2、修改 awstats 配置文件
完成配置文件的创建之后,我们还需要对 /etc/awstats/awstats.www.linuxidc.com.conf 里的一些参数进行修改。

sed -i ‘s#LogFile=”/var/log/httpd/mylog.log”#LogFile=”/app/logs/www_access_%YYYY-24%MM-24%DD-24.log”#g’ /etc/awstats/awstats.www.linuxidc.com.conf

这里更改的目的是指定 awstats 需要分析的 nginx 的日志文件路径。这里的路径大家要按自己的日志路径来填。

sed -i ‘s#DirData=”/var/lib/awstats”#DirData=”/usr/local/awstats/data”#g’ /etc/awstats/awstats.www.linuxidc.com.conf

这里更改的目的是指定 awstats 的数据库配置文件(即 awstats 的数据库 (纯文本))。
由于,此处没有 /usr/local/awstats/data 目录,所以我们要创建出来
1 mkdir /usr/local/awstats/data

以上的两个替换操作进行完之后一定要用命令查看替换是否成功,以便及早发现纰漏。

grep “LogFile=” /etc/awstats/awstats.www.linuxidc.com.conf
grep “DirData=” /etc/awstats/awstats.www.linuxidc.com.conf

查询替换结果正确之后,即可进行下面的步骤。
3、生成 awstats 统计信息数据库

现在我们需要用 awstats 来生成对日志的统计分析信息出来了。由于我们用的是支持 perl 的 FCGI 动态化访问页面,所以此处我们只需要直接更新数据库即可。FCGI 程序会自动将数据库以动态页面的形式展现出来,无须再手动生成静态页面了。本处我们用脚本来完成。

vim /server/scripts/awstats_up.sh
#!/bin/sh
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.linuxidc.com >/dev/null 2>&1

该脚本里面用下面的命令也是可以的。

/usr/local/awstats/tools/awstats_updateall.pl now

运行该脚本生成分析结果

/bin/sh /server/scripts/awstats_up.sh

四、配置日志分析页面的来访 ip 的地址位置显示

这里我们用国内最准确的 ip 数据库——QQ 纯真库我在博客附件里面放了这个工具,大家下载之后通过 CRT 用 lrzsz 工具传上去,具体步骤这里不写了。
附件里面有三个文件 qqhostinfo.pm,qqwry.pl 和 QQWry.Dat,我们将这三个文件统统都放到 /usr/local/awstats/wwwroot/cgi-bin/plugins 中。
接下来,我们修改 qqwry.pl 文件,将./QQWry.Dat 修改为 ${DIR}/plugins/QQWry
vim /usr/local/awstats/wwwroot/cgi-bin/plugins/qqwry.pl
 #my $ipfile=”./QQWry.Dat”;

修改为

my $ipfile=”${DIR}/plugins/QQWry.Dat “;

然后编辑 awstats 的配置文件 /etc/awstats/awstats.www.linuxidc.com.conf(根据你前面配置的站点信息生成的文件),将 LoadPlugin=”hostinfo” 替换为 LoadPlugin=”qqhostinfo” 即可。
sed -i ‘s#\#LoadPlugin=”hostinfo”#LoadPlugin=”qqhostinfo”#g’ /etc/awstats/awstats.www.linuxidc.com.conf

切记,在这些替换完之后一定要查看替换是否成功,以便及早发生纰漏。

grep “LoadPlugin=\”qqhostinfo\”” /etc/awstats/awstats.www.linuxidc.com.conf

如果检查无误,那么我们的 ip 地址位置显示就配置好了,在后面的日志分析中,我们就可以清楚的看到来访 ip 的地理位置信息了。

五、配置 nginx
接下来我们要配置 nginx 使其能安全的访问到分析的数据

vim /usr/local/nginx/conf/nginx.conf

在 server{}内添加如下内容:

server {
          listen 80;
          server_name www.linuxidc.com;
            location / {
            root  /www/linuxidc;
            index  index.html index.htm;
            access_log  /app/logs/www_access.log  main;
    }
            location ~* ^/cgi-bin/.*\.pl$ {
            root /usr/local/awstats/wwwroot;
              fastcgi_pass unix:/usr/local/nginx/fastcgi_temp/perl_cgi-dispatch.sock;
              fastcgi_index index.pl;
              include  fastcgi_params;
              charset gb2312;
              auth_basic “Restricted”;      #有些网站不愿意公开网站流量信息,所以加个认证
              auth_basic_user_file /usr/local/nginx/htpasswd.pass;  #该文件由 apache 的加密认证工具 htpasswd 创建
          }
      location ~ ^/icon/ {
        root  /usr/local/awstats/wwwroot/;
        index index.html;
        access_log off;
        error_log off;
        charset gb2312;
      }
}

 

为了保持 nginx.conf 主配置文件更加整洁干净,所以我们将 fastcgi_param 的一系列参数添加到 /usr/local/nginx/conf/fastcgi_params 文件的最顶部,然后在 nginx.conf 里面调用这个文件即可。

vi /usr/local/nginx/conf/fastcgi_params
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              fastcgi_param QUERY_STRING    $query_string;
              fastcgi_param REQUEST_METHOD  $request_method;
              fastcgi_param CONTENT_TYPE    $content_type;
              fastcgi_param CONTENT_LENGTH  $content_length;
              fastcgi_param GATEWAY_INTERFACE CGI/1.1;
              fastcgi_param SERVER_SOFTWARE    nginx;
              fastcgi_param SCRIPT_NAME        $fastcgi_script_name;
              fastcgi_param REQUEST_URI        $request_uri;
              fastcgi_param DOCUMENT_URI      $document_uri;
              fastcgi_param DOCUMENT_ROOT      $document_root;
              fastcgi_param SERVER_PROTOCOL    $server_protocol;
              fastcgi_param REMOTE_ADDR        $remote_addr;
              fastcgi_param REMOTE_PORT        $remote_port;
              fastcgi_param SERVER_ADDR        $server_addr;
              fastcgi_param SERVER_PORT        $server_port;
              fastcgi_param SERVER_NAME        $server_name;
              fastcgi_read_timeout 60;

针对上面的加密,由于 nginx 没有好的加密认证工具,需要借助 apache 的 htpasswd 来实现加密认证功能

htpasswd -c -m /usr/local/nginx/htpasswd.pass linuxidcadmin      #用户名为 linuxidcadmin

配置完毕之后,检查 nginx 语法,然后优雅重启之后,用游览器访问 http://www.linuxidc.com/cgi-bin/awstats.pl?config=www.linuxidc.com,输入账号密码之后即可查看统计信息了。

日志分析工具 Awstats 实战之 Nginx 篇

日志分析工具 Awstats 实战之 Nginx 篇

至此,awstats 已经可以实现对 Nginx 的日志统计,动态化安全访问及来访 ip 的地址位置显示等功能了。

五、配置 awstats 自动运行
为了让整个日志的统计过程可以实现自动化,将 awstats.sh 脚本加入 crontab 定时任务中去,此时结合上面的定时切割任务��我们的 crontab 里面会有多出来两条定时任务

1 0 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1
0 1 * * * /bin/sh /server/scripts/awstats_up.sh >/dev/null 2>&1

到此,我们整个日志访问工具 awstats 在 nginx 上对日志分析之后的静态化和动态化访问都已经实现了。后面的话,我会再写一篇 awstats 对 apache 服务器的日志分析部署文档。大家如果有什么问题的话,可以联系我进行交流探讨,咱们共同学习,共同进步!

推荐阅读

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

前言:
Awstats 是在 SourceForge 上发展很快的一个基于 Perl 的 WEB 日志分析工具,一个充分的日志分析让 Awstats 显示您下列资料:
访问次数、独特访客人数,
访问时间和上次访问,
使用者认证、最近认证的访问,
每周的高峰时间 (页数, 点击率, 每小时和一周的千字节),
域名 / 国家的主机访客 (页数, 点击率, 字节,269 域名 / 国家检测, geoip 检测),
主机名单, 最近访问和未解析的 IP 地址名单
大多数看过的进出页面,
档案类型,
网站压缩统计表 (mod_gzip 或者 mod_deflate),
使用的操作系统 (每个操作系统的页数, 点击率 , 字节, 35 OS detected),
使用的浏览器,
机器人访问 (检测 319 个机器人),
蠕虫攻击 (5 个蠕虫家族),
搜索引擎,利用关键词检索找到你的地址,
HTTP 协议错误 (最近查阅没有找到的页面),
其他基于 URL 的个性报导, 链接参数, 涉及综合行销领域目的.
贵网站被加入 ” 最喜爱的书签 ”. 次数.
屏幕大小 (需要在索引页补充一些 HTML 标签).
浏览器的支持比例: Java, Flash, RealG2 reader, Quicktime reader, WMA reader, PDF reader.
负载平衡服务器比率集群报告.

Awstats 的运行是需要 PERL 环境的支持,从 awstats 的文档来看,它对 Apache HTTP Server 的支持是非常完美的,而当我们把 Web 服务器换成 Nginx 后,要运行 awstats 变得很麻烦。首先 Nginx 本身对 Perl 的支持是比较弱的,甚至官方也不建议使用;另外在日志格式上有需要修改后才能运行。
使用 awstats 可以分析 apache 日志,同样也可以分析 nginx 日志。本文将详细介绍自动定时切割 nginx 的访问日志,并使用 awstats 来定时分析 nginx 日志及实现统计结果可供安全便捷的查阅。

环境:
CentOS 6.4 x86_64
ip:192.168.1.113
域名:www.linuxidc.com(server 和 client 都通过 hosts 文件解析)
nginx-1.2.9 编译安装,路径 /usr/local/nginx,服务开启状态
日志记录格式为 nginx 默认的,切勿更改,否则会造成 awstats 无法分析日志。
log_format  main  ‘$remote_addr – $remote_user [$time_local] “$request” ‘
                  ‘$status $body_bytes_sent “$http_referer” ‘
                  ‘”$http_user_agent” “$http_x_forwarded_for”‘;
awstats-7.2.tar.gz

一、日志自动切割
对于 nginx 的日志切割,由于没有像 apache 一样去用 cronolog 工具,这里我们就写一个脚本,让它可以在每天 00:01 自动执行,切割昨天的日志(交由 awstats 分析),压缩前天的日志(压缩日志可减小存储空间,为防止 awstats 没有分析完就被压缩,所以只压缩前天的日志)。

vim /server/scripts/cut_nginx_log.sh

输入以下内容:

#!/bin/sh
yesterday=`date -d “yesterday” +”%Y%m%d”`
before_yesterday=`date -d “-2 day” +”%Y%m%d”`
Nginx_Dir=”/usr/local/nginx”
Nginx_logs=”/app/logs”
Log_Name=”www_access”
cd /tmp
[-d $Nginx_Logs] && cd $Nginx_logs || exit 1
[-f $Log_Name.log] && /bin/mv $Log_Name.log ${Log_Name}_${yesterday}.log || exit 1
if [$? -eq 0 -a -f $Nginx_Dir/logs/nginx.pid]
    then
      kill -USR1 `cat $Nginx_Dir/logs/nginx.pid`
fi
[-f  ${Log_Name}_${before_yesterday}.log ] && /usr/bin/gzip ${Log_Name}_${before_yesterday}.log|| exit 1

执行 crontab - e 将该脚本加入定时任务中

1 0 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1

这样每天凌晨 00:01 就能自动实现日志的切割,压缩等功能了

因为本次实验下的 nginx 此时已经有日志了,另外为了后文 awstats 能对切割过的日志进行分析,所以这里我们要运行一下此脚本,来将现有日志进行切割生成昨天的日志方便后文操作。

/bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1

二、Awstats 的安装与配置

1. 部署 awstats
首先我们要下载 awstats 软件包,并将其放在常规目录(/usr/local)下

wget http://awstats.sourceforge.net/files/awstats-7.2.tar.gz
tar zxf awstats-7.2.tar.gz
mv awstats-7.2 /usr/local/awstats

由于 wget 下载下来的包中权限是非 root 的,所以这里要修改权限,否则稍后 *.pl 将无法运行

chown -R root.root /usr/local/awstats
chmod +x /usr/local/awstats/tools/*.pl
chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl

接下来我们要执行 awstats/tools 下的 awstats_configure.pl 配置向导,用来生成 awstats 的配置文件,awstats 配置文件的命名规则是 awstats.website.conf。

cd /usr/local/awstats/tools/
./awstats_configure.pl

此时会出现如下提示
—– AWStats awstats_configure 1.0 (build 1.9) (c) Laurent Destailleur —–
This tool will help you to configure AWStats to analyze statistics for
one web server. You can try to use it to let it do all that is possible
in AWStats setup, however following the step by step manual setup
documentation (docs/index.html) is often a better idea. Above all if:
– You are not an administrator user,
– You want to analyze downloaded log files without web server,
– You want to analyze mail or ftp log files instead of web log files,
– You need to analyze load balanced servers log files,
– You want to ‘understand’ all possible ways to use AWStats…
Read the AWStats documentation (docs/index.html).
—–> Running OS detected: Linux, BSD or Unix
—–> 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):
> none      #这里让填写网页服务器的配置文件路径,因为我们用的不是 apache,所以这里要填 none
Your web server config file(s) could not be found.
You will need to setup your web server manually to declare AWStats
script as a CGI, if you want to build reports dynamically.
See AWStats setup documentation (file docs/index.html)
—–> Update model config file ‘/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf’
  File awstats.model.conf updated.
—–> 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        #询问是否创建一个新的配置文件,这里填 y
—–> 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.linuxidc.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):
>              #这里要填写你配置文件存放路径,我们使用它默认的路径 /etc/awstats,所以直接回车即可
—–> Create config file ‘/etc/awstats/awstats.www.linuxidc.com.conf’
 Config file /etc/awstats/awstats.www.linuxidc.com.conf created.
—–> Add update process inside a scheduler
Sorry, configure.pl does not support automatic add to cron yet.
You can do it manually by adding the following command to your cron:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.linuxidc.com
Or if you have several config files and prefer having only one command:
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue…  #提示不能自动加入 crontab 定时任务,需要稍后自己添加,我们按回车继续即可
A SIMPLE config file has been created: /etc/awstats/awstats.www.linuxidc.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for ‘www.linuxidc.com’ with command:
> perl awstats.pl -update -config=www.linuxidc.com
You can also build static report pages for ‘www.linuxidc.com’ with command:
> perl awstats.pl -output=pagetype -config=www.linuxidc.com
Press ENTER to finish… #提示配置文件创建完成和如何更新配置及建立静态报告页,这里我们回车即可结束这个配置向导

2、修改 awstats 配置文件
完成配置文件的创建之后,我们还需要对 /etc/awstats/awstats.www.linuxidc.com.conf 里的一些参数进行修改。
sed -i ‘s#LogFile=”/var/log/httpd/mylog.log”#LogFile=”/app/logs/www_access_%YYYY-24%MM-24%DD-24.log”#g’ /etc/awstats/awstats.www.linuxidc.com.conf

这里更改的目的是指定 awstats 需要分析的 nginx 的日志文件路径。这里的路径大家要按自己的日志路径来填。
sed -i ‘s#DirData=”/var/lib/awstats”#DirData=”/usr/local/awstats/data”#g’ /etc/awstats/awstats.www.linuxidc.com.conf

这里更改的目的是指定 awstats 的数据库配置文件(即 awstats 的数据库 (纯文本))。
由于,此处没有 /usr/local/awstats/data 目录,所以我们要创建出来

mkdir /usr/local/awstats/data

以上的两个替换操作进行完之后一定要用命令查看替换是否成功,以便及早发现纰漏。

grep “LogFile=” /etc/awstats/awstats.www.linuxidc.com.conf
grep “DirData=” /etc/awstats/awstats.www.linuxidc.com.conf

查询替换结果正确之后,即可进行下面的步骤。

3、创建 awstats 统计结果存放目录
现在我们要创建 awstats 统计结果的数据库存放目录
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.linuxidc.com

如果屏幕输出类似下面的提示就说明配置文件都正确无误了
Create/Update database for config “/etc/awstats/awstats.www.linuxidc.com.conf” by AWStats version 7.2 (build 1.992)
From data in log file “/app/logs/www_access.log”…
Phase 1 : First bypass old records, searching new record…
Searching new records from beginning of log file…
Jumped lines in file: 0
Parsed lines in file: 0
 Found 0 dropped records,
 Found 0 comments,
 Found 0 blank records,
 Found 0 corrupted records,
 Found 0 old records,
 Found 0 new qualified records.

注释:awstats.pl 会到 /etc/awstats 目录下搜索,根据读取到的配置文件运行程序,去读取 -config 中的参数,把 www.linuxidc.com 扩展成 awstats.www.linuxidc.com.conf
分析日志:运行后将这样的日志统计结果归档到一个 awstats 的数据库(纯文本)里;
然后是输出:分两种形式
1、一种是通过 cgi 程序读取统计结果数据库输出;
2、一种是运行后台脚本将输出导出成静态文件;
统计分析完成后,结果还在 Awstats 的数据库中。在 Apache 上,可以直接打开 Perl 程序的网页查看统计。但 Nginx 对 Perl 支持并不好,所以要换个方法,利用 awstats 的工具将统计的结果生成静态文件,这里方便我们还是用脚本来实现
vim /server/scripts/awstats.sh

输入以下内容
#!/bin/sh
Awstats_Dir=”/usr/local/awstats”
[-d /www/awstats]||mkdir /www/awstats
$Awstats_Dir/wwwroot/cgi-bin/awstats.pl -update -config=www.linuxidc.com
$Awstats_Dir/tools/awstats_buildstaticpages.pl -update -config=www.linuxidc.com -awstatsprog=$Awstats_Dir/wwwroot/cgi-bin/awstats.pl -lang=cn -dir=/www/awstats

脚本内容讲解:
/usr/local/awstats/tools/awstats_buildstaticpages.pl    Awstats 静态页面生成工具
-update -config=www.linuxidc.com  更新配置项
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl  Awstats 日志更新程序路径
-lang=cn    语言为中文
-dir= /www/awstats  统计结果输出目录

awstats_buildstaticpages.pl 会根据 -config 的参数去读取里面的 DirData 路径下的数据库配置文件,然后生成静态的 html 文件,生成的文件重定向到 /www/awstats 目录下。

三、配置 nginx 实现安全访问
接下来我们要配置 nginx 使其能安全的访问到分析的数据
vim /usr/local/nginx/conf/nginx.conf

在 server{}内添加如下内容:
server {
        listen      80;
        server_name  www.linuxidc.com;
        location / {
            root  /www/linuxidc;
            index  index.html index.htm;
            access_log /app/logs/www_access.log commonlog;
}
location ~ ^/awstats/ {
            root  /www/;
            index  awstats.www.linuxidc.com.html;          #根据自己的网站域名进行更改首页文件
            autoindex on;
            access_log off;
            charset gb2312;
            auth_basic “Restricted”;                                          #有些网站不愿意公开网站流量信息,所以加个认证
            auth_basic_user_file /usr/local/nginx/htpasswd.pass;  #该文件由 apache 的加密认证工具 htpasswd 创建
}
location ~ ^/icon/ {
            root  /usr/local/awststs/wwwroot;
            index  index.html;
            access_log off;
            charset gb2312;
}
    }

由于 nginx 没有好的加密认证工具,需要借助 apache 的 htpasswd 来实现加密认证功能
htpasswd -c -m /usr/local/nginx/htpasswd.pass linuxidcadmin      #用户名为 linuxidcadmin

配置完毕之后,检查 nginx 语法,然后优雅重启之后,用游览器访问 www.linuxidc.com/awstats,输入账号密码之后即可查看统计信息了。
至此,awstats 已经可以实现对 Nginx 的日志统计及静态化的安全访问功能了。
四、配置 awstats 自动运行
为了让整个日志的统计过程可以实现自动化,将 awstats.sh 脚本加入 crontab 定时任务中去,此时结合上面的定时切割任务,我们的 crontab 里面会有多出来两条定时任务
1 0 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1
0 1 * * * /bin/sh /server/scripts/awstats.sh >/dev/null 2>&1

到此,我们整个日志访问工具 awstats 在 nginx 上的配置就完成了。当然此篇是基于静态页面的显示的,后面会再写一篇博文日志分析工具 Awstats 实战之 Nginx 篇 - 分析结果动态化出来和大家一起交流学习的,见 http://www.linuxidc.com/Linux/2013-10/92150p2.htm。

推荐阅读:

CentOS 6.3 下 AWStats+GeoIP 实现查看网站访问归属地 http://www.linuxidc.com/Linux/2013-06/85984.htm

Nginx 日志分析 AWStats + JAWStats 安装配置 http://www.linuxidc.com/Linux/2013-06/85567.htm

经典日志分析工具 -AWStats http://www.linuxidc.com/Linux/2012-12/77080.htm

AWStats 日志系统配置文件和错误归纳 http://www.linuxidc.com/Linux/2012-12/75657.htm

使用 AWStats 分析网站日志 - 强大的日志分析工具 http://www.linuxidc.com/Linux/2012-11/74431.htm

AWStats 的详细介绍:请点这里
AWStats 的下载地址:请点这里

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计31883字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中