AWStats

来自 ArchWiki

来自 AWStats - 用于高级统计的免费日志文件分析器

AWStats 是一个免费的、功能强大的工具,可以图形化地生成高级的 Web、流媒体、ftp 或邮件服务器统计信息。此日志分析器可以作为 CGI 或从命令行工作,并在几个图形化网页中显示日志中包含的所有可能信息。它使用部分信息文件来能够经常和快速地处理大型日志文件。它可以分析来自所有主要服务器工具的日志文件,例如 Apache 日志文件(NCSA combined/XLF/ELF 日志格式或 common/CLF 日志格式)、WebStar、IIS(W3C 日志格式)以及许多其他 Web、代理、wap、流媒体服务器、邮件服务器和一些 ftp 服务器。

安装

安装 awstats 软件包。当 Apache HTTP 服务器 被用作 Web 服务器时,也需要 mod_perlAUR 软件包。

配置

为 Apache 启用 mod_perl

要在 Apache 中启用 mod_perl,您应该将以下行添加到 Apache 配置文件 (/etc/httpd/conf/httpd.conf)

LoadModule perl_module modules/mod_perl.so

配置 Apache 以记录 AWStats 的日志

默认情况下,AWStats 要求 Apache 以“combined”格式记录访问日志。除非您想要不同的行为,否则您应该将您的访问日志格式设置为“combined”。为此,您的 Apache 配置应如下所示

<VirtualHost *:80>
    ServerAdmin zxc@returnfalse.net
    DocumentRoot "/srv/http/xxx"
    ServerName www.returnfalse.net
    ErrorLog "/var/log/httpd/returnfalse-error_log"
    CustomLog "/var/log/httpd/returnfalse-access_log" combined
</VirtualHost>

这里重要的一行是

CustomLog "/var/log/httpd/returnfalse-access_log" combined
注意: 在这一点上,如果 apache 已经开始以不同的格式记录访问日志,AWStats 会对此发出抱怨,因为它无法读取。因此,如果您现在要更改 Apache 的日志格式,您可能应该删除旧的日志文件,以免混淆 AWStats。

在 Apache 配置中包含 AWStats 配置

注意: AUR 软件包附带的 Apache HTTPD Web 服务器配置似乎有点过时,并且不包含 Perl 脚本处理程序的使用!

如果您设置了日志格式,那么下一步是在 Apache 中包含 AWStats 配置文件。AUR 中的软件包有一个默认的配置文件,如果您在 Apache 配置文件 (/etc/httpd/conf/httpd.conf) 中启用以下行,它就可以正常工作

<IfModule !mpm_prefork_module>
    LoadModule cgid_module modules/mod_cgid.so
</IfModule>
<IfModule mpm_prefork_module>
    LoadModule cgi_module modules/mod_cgi.so
</IfModule>

但是,如果您想创建自己的配置,可以使用此配置(使用 mod_perl 而不是 cgid_module 或 cgi_module)

LoadModule perl_module modules/mod_perl.so

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

<Directory "/usr/share/webapps/awstats">
    AddHandler perl-script .pl
    AddHandler perl-script .cgi
    PerlResponseHandler ModPerl::Registry
    PerlOptions +ParseHeaders
    
    Options None
    AllowOverride None
    Require all granted
</Directory>

DirectoryIndex index.html index.htm awstats.pl

将此文件(在 AUR 案例中,路径是 /etc/httpd/conf/extra/httpd-awstats.conf)包含到 Apache 的主配置中

Include conf/extra/httpd-awstats.conf

现在,如果您已正确完成所有步骤,则应该可以在 https://127.0.0.1/awstats/awstats.pl 上看到 AWStats 正在运行,在 重启 Apache 的 httpd.service 单元后。

最后一件事,也是实际目的,使 AWStats 读取日志并将它们转换为统计数据。

AWStats 配置

该软件包附带一个脚本,用于更新 AWStats 上显示的统计数据。此脚本读取 /etc/awstats 中的 AWStats 配置文件,并更新这些配置文件中定义的站点的统计数据。您可以使用 AWStats 的配置工具,而不是创建这些配置文件。运行

# perl /usr/share/awstats/tools/awstats_configure.pl

并按照说明进行操作。如果您成功创建了配置文件,则需要手动修改一件事。使用您喜欢的文本编辑器打开由 awstats_configure.pl 创建的配置文件。然后找到定义 LogFile 变量的行,并将其设置为 Apache 记录访问日志的路径(您之前设置为以“combined”格式记录)

注意: 请将 DirIcons 更改为 DirIcons="/awstatsicons" 以显示图片和图标!
# LogFile=/var/log/httpd/returnfalse-access_log

现在您可以运行脚本来测试结果,例如,如果您有一个 /etc/awstats/awstats.apache.conf,则运行

# /usr/share/awstats/tools/awstats_buildstaticpages.pl config=apache -update -awstatsprog=/usr/share/webapps/awstats/cgi-bin/awstats.pl -dir=/srv/http/awstats
警告: 使用这些设置,任何人都可以访问 AWStats。设置身份验证将有助于保持这些统计数据的私密性。

Nginx

如果您的 Web 服务器软件是 nginx,请按照以下步骤操作

1. 如上所述安装 awstats。有必要使用以下命令获取用户 http 和组 http 拥有的文件夹和文件

# chown -R http:http /usr/share/webapps/awstats/

2. 使用 awstats 配置工具生成站点配置文件,如上所述。确保正确设置了以下行

LogFile="/var/log/nginx/access.log"
LogFormat=1

3. 为了使 awstats 的 Perl 脚本在 nginx 上工作,使用以下代码创建 /etc/nginx/cgi-bin.php

<?php
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("pipe", "w")   // stderr is a file to write to
);
$newenv = $_SERVER;
$newenv["SCRIPT_FILENAME"] = $_SERVER["X_SCRIPT_FILENAME"];
$newenv["SCRIPT_NAME"] = $_SERVER["X_SCRIPT_NAME"];
if (is_executable($_SERVER["X_SCRIPT_FILENAME"])) {
   $process = proc_open($_SERVER["X_SCRIPT_FILENAME"], $descriptorspec, $pipes, NULL, $newenv);
   if (is_resource($process)) {
       fclose($pipes[0]);
       $head = fgets($pipes[1]);
       while (strcmp($head, "\n")) {
           header($head);
           $head = fgets($pipes[1]);
       }
       fpassthru($pipes[1]);
       fclose($pipes[1]);
       fclose($pipes[2]);
       $return_value = proc_close($process);
   } else {
       header("Status: 500 Internal Server Error");
       echo("Internal Server Error");
   }
} else {
   header("Status: 404 Page Not Found");
   echo("Page Not Found");
}
?> 

4. 将这些指令添加到域 nginx 配置文件

location ^~ /awstatsicons {
   alias /usr/share/webapps/awstats/icon/;
   access_log off;
}
location ^~ /awstatscss {
   alias /usr/share/webapps/awstats/examples/css/;
   access_log off;
}
location ^~ /awstatsclasses {
   alias /usr/share/webapps/awstats/examples/classes/;
   access_log off;
}
location ~ ^/cgi-bin/.*\.(cgi|pl|py|rb) {
   gzip off;
   fastcgi_pass  unix:/var/run/php-fpm/php-fpm.sock;
   fastcgi_index cgi-bin.php;
   fastcgi_param SCRIPT_FILENAME    /etc/nginx/cgi-bin.php;
   fastcgi_param SCRIPT_NAME        /cgi-bin/cgi-bin.php;
   fastcgi_param X_SCRIPT_FILENAME  /usr/share/webapps/awstats$fastcgi_script_name;
   fastcgi_param X_SCRIPT_NAME      $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 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_param REMOTE_USER        $remote_user;
}

5. 您可以在 “http://your_domain.com/cgi-bin/awstats.pl?config=your_domain.com” 访问您站点的 awstats 页面。可选地,您可能希望将此重写规则添加到 nginx 站点配置文件

location ~ ^/awstats {
   rewrite ^ http://your_domain.com/cgi-bin/awstats.pl?config=your_domain.com;
}

这样,您只需在浏览器的地址栏中键入 “http://your_domain.com/awstats” 即可访问您的 awstats 页面。

Caddy

如果您的 Web 服务器软件是 caddy,请安装 fcgiwrap 以使 FastCGI 工作,然后根据您的配置使用以下模板更改您的 Caddyfile。

1. 如上所述安装 awstats。有必要使用以下命令获取用户 http 和组 http 拥有的文件夹和文件

# chown -R http:http /usr/share/webapps/awstats/

2. 使用 awstats 配置工具生成站点配置文件,如上所述。确保正确设置了以下行

LogFile="/var/log/caddy/site_combined.log"
LogFormat=1

3. 使用以下指令创建您的 Caddyfile /etc/caddy/caddy.conf.d/sitename 配置文件

awstats.hostname.com {
#basicauth /cgi-bin username password
root /usr/share/webapps/awstats
log / /var/log/caddy/site_combined.log "{combined}"
gzip

fastcgi /cgi-bin/ unix:/var/run/fcgiwrap.sock {
env SCRIPT_DIR /usr/share/webapps/awstats/cgi-bin/
env SCRIPT_FILENAME /usr/share/webapps/awstats/cgi-bin/awstats.pl
        }
}
awstats.hostname.com/awstatsclasses {
        root /usr/share/webapps/awstats/classes
}
awstats.hostname.com/awstatscss {
        root /usr/share/webapps/awstats/css
}
awstats.hostname.com/awstatsicons/ {
        root /usr/share/webapps/awstats/icon
}
awstats.hostname.com/js {
	root /usr/share/webapps/awstats/js
}

您现在应该能够通过以下 URL 查看您的站点

https://awstats.hostname.com/cgi-bin/awstats.pl?config=awstats.hostname.com

生成统计数据

本文或章节需要语言、wiki 语法或样式改进。请参阅 Help:Style 以供参考。

原因: 运行 awstats_updateall.pl 的命令需要 用户提示。(在 Talk:AWStats 中讨论)

您可以通过发出以下命令手动生成所有站点的最新统计数据

/usr/share/awstats/tools/awstats_updateall.pl now -awstatsprog=/usr/share/webapps/awstats/cgi-bin/awstats.pl

systemd

此过程可以通过(每小时)systemd 计时器脚本自动完成。您首先需要创建 awstats-aw.serviceawstats-aw.timer 单元

/etc/systemd/system/awstats-aw.service
[Unit]
Description=Update Awstats with all site configurations located in /etc/awstats/

[Service]
Type=oneshot
ExecStart=/usr/share/awstats/tools/awstats_updateall.pl now -awstatsprog=/usr/share/webapps/awstats/cgi-bin/awstats.pl
WorkingDirectory=/usr/share
/etc/systemd/system/awstats-aw.timer
[Unit]
Description=Run the awstats-aw.service every hour to update Awstats

[Timer]
OnCalendar=hourly
Persistent=true

[Install]
WantedBy=timers.target

然后,您可以启动该服务并启用该计时器。

cron

此过程可以通过 cron 自动化。请参阅 AWStats cron 模板:/usr/share/doc/awstats-7.5/cron.hourly 但是,如果您正在使用 logrotate,则必须确保 cronjob 在 logrotate 运行之前启动。否则,统计信息将丢失,因为 loratate 会将访问日志文件名更改为 awstats 无法访问的不同名称。处理此问题的更好方法是使用通常位于 /etc/logrotate.d 中的 Web 服务器特定 logrotate 脚本来触发 awstats 计算。此处提供了一个 nginx logrotate 脚本示例。请注意添加了 prerotate 指令

/var/log/nginx/*log {
 daily
 missingok
 notifempty
 create 640 http log
 compress
 sharedscripts
 prerotate
   # Trigger awstats computation
   /usr/share/awstats/tools/awstats_updateall.pl now -awstatsprog=/usr/share/webapps/awstats/cgi-bin/awstats.pl
 endscript
 postrotate
    test ! -r /run/nginx.pid || kill -USR1 `cat /run/nginx.pid`
 endscript
}

GeoIP (可选)

要添加 geo ip 支持,请使用 cpan 安装 Geo::IP 模块。(有关更多详细信息,请参阅 Perl#CPAN.pm。)或者,安装 perl-geoip。将以下行添加到位于 /etc/awstats/ 的每个 awstats 站点配置文件中

LoadPlugin="geoip GEOIP_STANDARD /usr/share/GeoIP/GeoIP.dat"

如果您安装了 geoip-database-extra,您还可以将这些添加到您的 awstats 站点配置中

LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/share/GeoIP/GeoIPCity.dat"
LoadPlugin="geoip_asn_maxmind GEOIP_STANDARD /usr/share/GeoIP/GeoIPASNum.dat+http://enc.com.au/itools/autnum.php?asn="

参见