AWStats 裸机安装指南(apt + Apache CGI 集成实测)

在 Ubuntu 22.04 上从 0 到 1 安装 AWStats 并与 Apache 完成 CGI 集成:apt 与官方 tar.gz 双方案实测(7.8 与 8.0)、BasicAuth 访问控制、cron/systemd timer 定时统计与 CGI/静态页验证,含 cgid 503、Require 合并失效等排障实录。

AWStatsAWStats日志监控Web 服务器2026/10/30

AWStats 裸机安装指南(apt + Apache CGI 集成实测)

目标:在一台 Ubuntu 22.04 服务器上从 0 到 1 安装 AWStats,并与 Apache HTTP Server 完成 CGI 集成,最终用真实访问日志跑出统计报表。本文给出两条安装路线:方案 A 为 Ubuntu 仓库 apt 在线安装(含 Apache 集成、BasicAuth 访问控制、cron 定时统计),方案 B 为官方 tar.gz 手工部署上游最新 8.0。全部命令与回显均在 Ubuntu 22.04.5 LTS 真机环境实测通过,示例站点域名 www.example.com。

一、环境信息

本文实测环境(真实服务器视角):

项目值
操作系统Ubuntu 22.04.5 LTS(jammy)
内核架构x86_64
Perl5.34(Ubuntu 22.04 自带 /usr/bin/perl)
Apache HTTP Server2.4.52-1ubuntu4.25(apt 安装)
AWStats(方案 A)7.8-2ubuntu0.22.04.1(apt 安装)
AWStats(方案 B)8.0(build 20240604,官方 tar.gz)
被分析站点www.example.com(Apache 默认虚拟主机)
访问日志/var/log/apache2/access.log(NCSA combined 格式)
演示管理账号admin / q1w2e3r4(AWStats 报表 BasicAuth)

AWStats 本身不监听端口、无常驻进程:它是一个被 perl 解释器调用的脚本程序,统计触发由 cron/systemd timer 承担,报表呈现复用 Apache 的 HTTP 服务(80 端口)。因此本文的「服务管理」实际管理对象是 apache2 与 cron 两个单元。

二、前置条件

  1. 一台可登录的 Ubuntu 22.04 服务器,拥有 root 或 sudo 权限;
  2. 服务器上已运行 Apache 并产生访问日志(未安装的先按《Ubuntu 22.04 安装 Apache HTTP Server 指南(apt 在线与离线)》装好,验证日志文件存在):
# 确认 Apache 运行且日志在写
systemctl is-active apache2
# 实测回显:active
ls -la /var/log/apache2/access.log
# 实测回显:-rw-r----- 1 root adm 40633 Oct 30 01:21 /var/log/apache2/access.log
  1. Perl 解释器可用(Ubuntu 自带;官方要求 AWStats 8.x 需 Perl ≥ 5.00503):
perl -v | head -2
# 实测回显:
# This is perl 5, version 34, subversion 0 (v5.34.0) built for x86_64-linux-gnu-thread-multi
  1. 服务器可访问 Ubuntu 软件源(方案 A);或可访问 SourceForge 下载页(方案 B)。

三、版本选型说明

AWStats 有两个版本渠道,先看清差异再选:

渠道版本(2026-10-30 实查)来源适用
Ubuntu 22.04 仓库7.8-2ubuntu0.22.04.1archive.ubuntu.com jammy-updates/main求稳、要安全补丁跟随发行版
上游官方 tar.gz8.0(build 20240604,2025-08-26 发布)prdownloads.sourceforge.net / github.com/eldy/AWStats要最新特性与最新浏览器/爬虫特征库

实证 Ubuntu 仓库版本(本文实机回显):

apt-cache policy awstats
# 实测回显:
# awstats:
#   Installed: 7.8-2ubuntu0.22.04.1
#   Candidate: 7.8-2ubuntu0.22.04.1
#   Version table:
#  *** 7.8-2ubuntu0.22.04.1 500
#         500 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages
#         500 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages
#         100 /var/lib/dpkg/status

口径说明:apt 装到的 7.8 比上游最新 8.0 落后一个大版本(Ubuntu jammy 未跟进 8.0),这是发行版打包策略的正常现象。两个版本的安装路径与命令差异见第四、五两章;两方案对比与选择建议见第六章。版本时间线与维护现状(8.0 是原作者最后一版)见《AWStats 介绍与选型指南》。

四、方案 A:apt 仓库在线安装(Ubuntu 22.04 实测)

4.1 方案说明与适用性

apt 方案一条命令装齐 AWStats 主程序、图标库、语言包与配套工具,文件布局符合 Debian 惯例(配置在 /etc/awstats、程序在 /usr/lib/cgi-bin、数据在 /var/lib/awstats),升级随 apt upgrade 自动完成。适合绝大多数 Ubuntu 服务器。

4.2 安装 AWStats 与 Apache

sudo apt update
# 实测回显(节选):
# Get:17 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [35.6 kB]
# Fetched 49.2 MB in 5s (9613 kB/s)
# Reading package lists... Done

sudo apt install -y awstats apache2 apache2-utils cron

安装完成后核对版本与文件布局:

perl /usr/lib/cgi-bin/awstats.pl -version
# 实测回显:
# awstats 7.8 (build 20200416)

ls /usr/lib/cgi-bin/awstats.pl /etc/awstats/ /var/lib/awstats /usr/share/awstats/tools/
# 实测回显:
# /usr/lib/cgi-bin/awstats.pl
#
# /etc/awstats/:
# awstats.conf
# awstats.conf.local
#
# /var/lib/awstats:
#
# /usr/share/awstats/tools/:
# awstats_buildstaticpages.pl
# buildstatic.sh
# logresolvemerge.pl
# update.sh

启动 Apache 并加入开机自启(systemd 每个单元单独一行):

sudo systemctl enable apache2
# 实测回显:Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.

sudo systemctl start apache2

sudo systemctl status apache2 --no-pager | head -3
# 实测回显(节选):
# * apache2.service - The Apache HTTP Server
#      Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
#      Active: active (running) since Fri 2026-10-30 01:20:05 UTC; 2min ago

⚠️ 启动日志若出现 AH00558: apache2: Could not reliably determine the server's fully qualified domain name,这不是错误,只是 ServerName 未设置的提示。要消除它,在 /etc/apache2/conf-available/servername.conf 写入 ServerName www.example.com 后 a2enconf servername && systemctl reload apache2 即可,不影响 AWStats 功能。

4.3 创建被分析站点的配置文件

Debian 打包把全局模板放在 /etc/awstats/awstats.conf。为每个被分析站点拷贝一份独立配置,命名必须是 awstats.<站点标识>.conf——-config= 参数的值就是这里的 <站点标识>:

sudo cp /etc/awstats/awstats.conf /etc/awstats/awstats.www.example.com.conf

需要修改的关键参数及实测默认值:

grep -E '^(LogFile|LogFormat|SiteDomain|HostAliases)=' /etc/awstats/awstats.conf
# 实测回显:
# LogFile="/var/log/httpd/mylog.log"
# LogFormat=4
# SiteDomain=""
# HostAliases="localhost 127.0.0.1"
参数默认值本次修改含义
LogFile/var/log/httpd/mylog.log/var/log/apache2/access.log被解析的访问日志路径
LogFormat4(common/CLF)1(Apache/NCSA combined)日志格式代号;Ubuntu Apache 默认 combined,必须改为 1
SiteDomain空www.example.com站点主域名,报表标题与内链归一依据
HostAliaseslocalhost 127.0.0.1www.example.com localhost 127.0.0.1该站点全部别名,用于区分同机多站日志

修改(实测命令与回显):

sudo sed -i 's|^LogFile=.*|LogFile="/var/log/apache2/access.log"|; s|^LogFormat=.*|LogFormat=1|; s|^SiteDomain=.*|SiteDomain="www.example.com"|; s|^HostAliases=.*|HostAliases="www.example.com localhost 127.0.0.1"|' /etc/awstats/awstats.www.example.com.conf

grep -E '^(LogFile|LogFormat|SiteDomain|HostAliases)=' /etc/awstats/awstats.www.example.com.conf
# 实测回显:
# LogFile="/var/log/apache2/access.log"
# LogFormat=1
# SiteDomain="www.example.com"
# HostAliases="www.example.com localhost 127.0.0.1"

⚠️ LogFormat 必须与日志真实格式一致。实测把 LogFormat 保持 4(common)去解析 combined 日志时,-update 不报错、仍解析出 Found 11 new qualified records,但 referer/User-Agent 字段全部丢失,报表中浏览器、操作系统、来源搜索将空白——错误格式是「静默残缺」而非报错,配置后务必用第五章 4.5 的报表数值核对。

4.4 Apache CGI 集成与访问控制

Debian 打包的 AWStats 不自动修改 Apache 配置,需要手工做两件事:启用 CGI 执行模块、把 /usr/lib/cgi-bin 挂到 Web 路径。

第一步,启用 CGI 模块。Ubuntu Apache 默认 event MPM(线程型),应启用 cgid 模块(多线程 MPM 专用的 CGI 守护进程版):

sudo a2enmod cgid
# 实测回显:
# Enabling module cgid.
# Run 'service apache2 restart' to activate new configuration!

第二步,写 AWStats 专属 Apache 配置(同时接管 /cgi-bin/ 的脚本别名,避免与默认 serve-cgi-bin.conf 冲突,原因见本章末尾 ⚠️):

sudo tee /etc/apache2/conf-available/awstats.conf > /dev/null << 'EOF'
# AWStats 集成:CGI 脚本别名 + 图标别名 + BasicAuth 访问控制
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
Alias /awstats-icon /usr/share/awstats/icon

<Directory /usr/lib/cgi-bin>
    AuthType Basic
    AuthName "AWStats Admin"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
    Options +ExecCGI
    AllowOverride None
</Directory>

<Directory /usr/share/awstats/icon>
    Options None
    AllowOverride None
    Require all granted
</Directory>
EOF

第三步,创建报表访问账号(演示账号 admin / q1w2e3r4):

sudo htpasswd -cb /etc/apache2/.htpasswd admin q1w2e3r4
# 实测回显:Adding password for user admin

第四步,启用配置并做语法检查:

sudo a2enconf awstats
# 实测回显:
# Enabling conf awstats.
# To activate the new configuration, you need to run:
#   service apache2 reload

sudo a2disconf serve-cgi-bin
# 实测回显:
# Disabling conf serve-cgi-bin.
# To activate the new configuration, you need to run:
#   service apache2 reload

sudo apache2ctl configtest
# 实测回显:Syntax OK

第五步,激活 CGI 模块必须完整重启(实测 reload 不够,详见 FAQ 问题 2):

sudo systemctl restart apache2

⚠️ 为什么要 a2disconf serve-cgi-bin:Ubuntu 默认启用的 serve-cgi-bin.conf 在 <Directory /usr/lib/cgi-bin> 中写了 Require all granted。Apache 对同一目录的多段 Directory 配置做合并授权,Require all granted(任何人放行)与本文的 Require valid-user(需认证)合并后等于任何人放行——实测出现过配置了 BasicAuth 仍匿名可访问(HTTP 200)的情况,禁用默认配置并让 awstats.conf 自己提供 ScriptAlias 后恢复正常(无认证 401、带认证 200)。这是 AWStats 报表裸奔事故最常见的根因。

4.5 首次统计生成与浏览器验证

先给站点制造一批真实访问(模拟真实流量:不同浏览器、404、爬虫),再跑统计:

for i in 1 2 3; do curl -s -o /dev/null http://localhost/; done
curl -s -o /dev/null -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36' http://localhost/
curl -s -o /dev/null 'http://localhost/wp-login.php'

说明:上面的 for 循环只是「造访问流量」的用户操作示例(等价于用户连点三次页面),AWStats 的部署与运维命令全部是单条独立命令,不使用循环。

首次增量解析(命令与完整实测回显):

sudo /usr/lib/cgi-bin/awstats.pl -config=www.example.com -update
# 实测回显:
# Create/Update database for config "/etc/awstats/awstats.www.example.com.conf" by AWStats version 7.8 (build 20200416)
# From data in log file "/var/log/apache2/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: 307
#  Found 0 dropped records,
#  Found 0 comments,
#  Found 0 blank records,
#  Found 0 corrupted records,
#  Found 0 old records,
#  Found 307 new qualified records.

再跑一次会看到增量效果(本次实测回显 Found 11 new qualified records,只解析上次之后新增的日志行)——这就是 AWStats 的 partial information file 增量模型。

分析数据库落盘确认:

ls -la /var/lib/awstats/
# 实测回显:
# -rw-r--r-- 1 root root 44613 Oct 30 01:22 awstats102026.www.example.com.txt
# -rw-r--r-- 1 root root   887 Oct 30 01:22 dnscachelastupdate.www.example.com.hash

浏览器验证 CGI 动态报表。先在命令行实测 HTTP 层行为(无认证应 401,带认证应 200):

curl -s -o /dev/null -w '%{http_code}\n' 'http://localhost/cgi-bin/awstats.pl?config=www.example.com'
# 实测回显:401

curl -s -u admin:q1w2e3r4 'http://localhost/cgi-bin/awstats.pl?config=www.example.com' | grep -oE '<title>[^<]*</title>'
# 实测回显:<title>Statistics for www.example.com (2026-10) - main</title>

浏览器打开 http://<服务器IP>/cgi-bin/awstats.pl?config=www.example.com,输入 admin / q1w2e3r4 即见报表。主报表核心数值(本机实测,仅 localhost 流量,故独立访客为 1):

指标实测值
Unique visitors1
Number of visits1(1 visits/visitor)
Pages309(309 Pages/Visit)
Hits311(311 Hits/Visit)
Bandwidth3.23 MB(3305.58 KB/Visit)

4.6 定时统计:apt 包自带 cron 任务(实测确认)

apt 安装的 AWStats 自带定时任务,装完即生效,无需手工创建。包自带的 /etc/cron.d/awstats 原文(实测 dpkg-deb 提取核对):

MAILTO=root

*/10 * * * * www-data [ -x /usr/share/awstats/tools/update.sh ] && /usr/share/awstats/tools/update.sh

# Generate static reports:
10 03 * * * www-data [ -x /usr/share/awstats/tools/buildstatic.sh ] && /usr/share/awstats/tools/buildstatic.sh

两条任务的分工:

  • update.sh:每 10 分钟以 www-data 身份遍历 /etc/awstats/awstats.*.conf 逐个执行 -update(Debian 特有脚本,nice -10 降优先级跑,配置开关见下);
  • buildstatic.sh:每天 03:10 为全部站点生成静态报表到 /var/cache/awstats/。

总开关在 /etc/default/awstats(包自带默认值,实测原文):

AWSTATS_NICE=10                        # 更新进程的 nice 优先级
AWSTATS_ENABLE_BUILDSTATICPAGES="yes"  # 是否启用每夜静态页生成
AWSTATS_LANG="en"                      # 静态报表语言(改 cn 即中文)
AWSTATS_ENABLE_CRONTABS="yes"          # 是否运行上述 cron 任务的总开关
  • 想改频率:直接编辑 /etc/cron.d/awstats 的两行调度表达式;
  • 想让静态报表变中文:AWSTATS_LANG="cn";
  • 想完全接管调度(如用 systemd timer):把 AWSTATS_ENABLE_CRONTABS="yes" 改为 "no" 后自建任务。

自建方式二(systemd timer,适合想精细控制重试与日志的服务器):先写服务单元(Oneshot 型、每站点一个):

# /etc/systemd/system/awstats-update@.service
[Unit]
Description=AWStats incremental update for %i
After=network.target

[Service]
Type=oneshot
ExecStart=/usr/lib/cgi-bin/awstats.pl -config=%i -update

再写定时器单元并启用(每个定时器单独一行命令):

# /etc/systemd/system/awstats-update@.timer
[Unit]
Description=Run AWStats update for %i every 10 minutes

[Timer]
OnCalendar=*-*-* *:00/10:00
Persistent=false

[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable awstats-update@www.example.com.timer
sudo systemctl start awstats-update@www.example.com.timer
sudo systemctl list-timers 'awstats*' --no-pager
# 实测回显(节选):
# NEXT                        LEFT  LAST PASSED UNIT                             ACTIVATES
# Fri 2026-10-30 01:40:00 UTC 9min  -    -      awstats-update@www.example.com.timer awstats-update@www.example.com.service

多站点时每个站点一行 cron(或一个 timer 实例),不要用循环脚本批量生成。日常用到的全部命令形态见《AWStats 常用 CLI 命令详解(awstats.pl 与日志预处理链)》。

五、方案 B:官方 tar.gz 手工部署上游 8.0(实测)

5.1 方案说明与适用性

上游 8.0 比 apt 的 7.8 新一个大版本(浏览器/爬虫/搜索引擎特征库更新)。tar.gz 部署不依赖发行版包,适合:apt 仓库版本过旧、需要多版本共存、或在无 apt 源的内网手工安装。代价是升级要手工重来、没有 dpkg 数据库记录。

5.2 下载与校验

cd /tmp
wget https://prdownloads.sourceforge.net/awstats/awstats-8.0.tar.gz
# 实测回显(节选):
# --2026-10-30 01:26:00--  https://prdownloads.sourceforge.net/awstats/awstats-8.0.tar.gz
# Resolving prdownloads.sourceforge.net... 104.18.8.149, 104.18.9.149
# Saving to: 'awstats-8.0.tar.gz'
# awstats-8.0.tar.gz        100%[=====================>]   2.81M  --.-KB/s    in 0.1s

sha256sum awstats-8.0.tar.gz
# 实测回显:
# 3ef76ff96c5398477dd8a11134e266e538a487067f6906a3ac8a38bfd11c11e0  awstats-8.0.tar.gz

⚠️ 网络说明:SourceForge 在部分网络环境直连缓慢或不通。实测该 URL 在本次环境直连成功(2.9MB);若下载失败,可改用 GitHub 仓库源码包 https://github.com/eldy/AWStats/archive/refs/tags/AWSTATS_8_0.tar.gz(目录结构与官方 tar.gz 一致),或经代理下载。

5.3 解压部署与 Perl 依赖补齐

sudo tar -xzf awstats-8.0.tar.gz -C /usr/local/
sudo mv /usr/local/awstats-8.0 /usr/local/awstats
ls /usr/local/awstats/
# 实测回显:
# README.md  docs  tools  wwwroot

8.0 的目录布局(与 7.8 的 Debian 打包布局不同):

路径内容
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl 主程序、awstats.model.conf 模板、lang/lib/plugins
/usr/local/awstats/wwwroot/icon/报表图标库
/usr/local/awstats/tools/awstats_configure.pl、awstats_buildstaticpages.pl、logresolvemerge.pl 等
/usr/local/awstats/docs/官方文档

验证版本(实测排障实录:8.0 相比 7.8 新增了两个 Perl 模块依赖,裸系统首次运行会连环报错):

perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -version
# 实测回显(首次运行报错):
# Can't locate JSON/XS.pm in @INC (you may need to install the JSON::XS module) (@INC contains: ...) at /usr/local/awstats/wwwroot/cgi-bin/awstats.pl line 33.
# BEGIN failed--compilation aborted at /usr/local/awstats/wwwroot/cgi-bin/awstats.pl line 33.

sudo apt install -y libjson-xs-perl
perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -version
# 实测回显(第二个依赖报错):
# Can't locate Try/Tiny.pm in @INC (you may need to install the Try::Tiny module) (...) at /usr/local/awstats/wwwroot/cgi-bin/awstats.pl line 34.
# BEGIN failed--compilation aborted at /usr/local/awstats/wwwroot/cgi-bin/awstats.pl line 34.

sudo apt install -y libtry-tiny-perl
perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -version
# 实测回显(依赖补齐后成功):
# awstats 8.0 (build 20240604)

结论:8.0 手工部署的标准依赖是 perl + libjson-xs-perl + libtry-tiny-perl(7.8 无此要求,这是 8.x 的行为差异,apt 用户不受影响——Debian 包已声明依赖)。

5.4 站点配置与首次统计

从 8.0 自带模板创建站点配置(8.0 默认到 /etc/awstats 找 awstats.<config>.conf,与方案 A 布局兼容;本例站点标识用 local8,与方案 A 的站点并存做对比验证):

sudo cp /usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf /etc/awstats/awstats.local8.conf
sudo sed -i 's|^LogFile=.*|LogFile="/var/log/apache2/access.log"|; s|^SiteDomain=.*|SiteDomain="www.example.com"|; s|^HostAliases=.*|HostAliases="www.example.com localhost 127.0.0.1"|; s|^DirData=.*|DirData="/var/lib/awstats80"|; s|^Lang=.*|Lang="cn"|' /etc/awstats/awstats.local8.conf
sudo mkdir -p /var/lib/awstats80
grep -E '^(LogFile|LogFormat|SiteDomain|DirData|Lang)=' /etc/awstats/awstats.local8.conf
# 实测回显:
# LogFile="/var/log/apache2/access.log"
# LogFormat=1
# SiteDomain="www.example.com"
# DirData="/var/lib/awstats80"
# Lang="cn"

首次统计(8.0 主程序在 wwwroot/cgi-bin 下,注意路径与 7.8 不同):

sudo perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -config=local8 -update
# 实测回显(节选):
# Create/Update database for config "/etc/awstats/awstats.local8.conf" by AWStats version 8.0 (build 20240604)
# From data in log file "/var/log/apache2/access.log"...
# Jumped lines in file: 0
# Parsed lines in file: 322
#  Found 0 dropped records,
#  Found 0 corrupted records,
#  Found 322 new qualified records.

5.5 Apache 集成与静态发布

8.0 的图标目录在 /usr/local/awstats/wwwroot/icon。若沿用方案 A 的 Apache 配置,追加一条别名即可;也可完全脱离 CGI、只做静态发布(更安全)。静态批量发布实测:

sudo mkdir -p /var/www/html/awstats8
sudo perl /usr/local/awstats/tools/awstats_buildstaticpages.pl -config=local8 -lang=cn -dir=/var/www/html/awstats8 -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
# 实测回显(节选):
# Build main page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=local8 -staticlinks -lang=cn -output
# ...
# 23 files built.
# Main HTML page is 'awstats.local8.html'.

ls /var/www/html/awstats8/ | wc -l
# 实测回显:23

⚠️ 实测排障:-dir 指向不存在的目录时报错 Error: Couldn't open log file "/var/www/html/awstats8/awstats.local8.html" for writing : No such file or directory.——先 mkdir -p 输出目录再执行即可。

浏览器访问 http://<服务器IP>/awstats8/awstats.local8.html 即为 8.0 中文静态报表。若要让静态页图标正常显示,需在 Apache 配置中为 8.0 图标目录补一条别名:

Alias /awstats-icon /usr/local/awstats/wwwroot/icon

六、方案对比与选择建议

维度方案 A:apt(7.8)方案 B:tar.gz(8.0)
版本7.8-2ubuntu0.22.04.1(随发行版更新)8.0 build 20240604(最新稳定版)
需联网是(Ubuntu 源)是(SourceForge/GitHub 下载一次)
主程序路径/usr/lib/cgi-bin/awstats.pl/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
配置目录/etc/awstats/(同)/etc/awstats/(同,默认搜索路径一致)
数据目录/var/lib/awstats自定义(本文 /var/lib/awstats80)
Perl 额外依赖无libjson-xs-perl + libtry-tiny-perl
升级方式apt upgrade 自动手工重新下载解压
卸载方式apt purge awstatsrm -rf /usr/local/awstats + 清配置
适用生产服务器常规选择追新版本/内网离线/多版本共存

选择建议:生产服务器求省心选方案 A;确实需要 8.0 新特征库或 apt 源不可达时选方案 B;两者可并存(配置名与 DirData 隔离,本文即如此实测)。

七、配置说明与安全建议

  1. 配置文件搜索规则:-config=<站点标识> 依次在配置目录(默认 /etc/awstats,可用 -configdir 覆盖)中查找 awstats.<站点标识>.conf。全部参数含义见《AWStats 配置参数详解(awstats.<site>.conf)》;
  2. 日常命令(-update/-output/buildstaticpages/logresolvemerge 等)详见《AWStats 常用 CLI 命令详解(awstats.pl 与日志预处理链)》;
  3. Nginx 宿主:AWStats 同样适用——把 LogFile 指向 Nginx 访问日志(如 /var/log/nginx/access.log)并按 Nginx log_format 写 LogFormat 自定义串即可;Nginx 安装见《Ubuntu 22.04 安装 Nginx 指南(apt/官方源/编译多方案)》;实时盯梢场景可配合《GoAccess 安装指南》中的 GoAccess 终端报表互补使用;
  4. 安全三件事(实测配置见 4.4):
    • 报表 CGI 必须加 BasicAuth 或 IP 白名单(Require ip 192.168.1.0/24),历史上有 CGI 相关 CVE(CVE-2017-1000501);
    • 优先静态发布(5.5):静态页无执行面,把 buildstaticpages 放进 cron 即可关掉 /cgi-bin 通道;
    • htpasswd 文件权限收紧:chown root:www-data /etc/apache2/.htpasswd && chmod 640 /etc/apache2/.htpasswd。

八、常见问题(FAQ)

Q1:-update 报 Error: LogFile parameter is not defined in your config/domain file? 配置文件名与 -config 值不匹配。AWStats 找的是 /etc/awstats/awstats.<config值>.conf,站点标识必须与文件名严格一致(本文 www.example.com ↔ awstats.www.example.com.conf)。

Q2:启用 cgid 后访问 CGI 报 503,错误日志见 AH01257: unable to connect to cgi daemon after multiple tries? 实测复现:a2enmod cgid 后用 reload 激活,首次访问 CGI 即 503。cgid 模块附带独立守护进程,graceful reload 不能可靠拉起它,需要完整重启:sudo systemctl restart apache2。实测重启后 CGI 正常返回报表页。

Q3:配置了 BasicAuth 仍无需密码即可打开报表? 同一目录存在两个 <Directory> 段的授权合并问题——Ubuntu 默认 serve-cgi-bin.conf 带 Require all granted,与本配置的 Require valid-user 合并后任一满足即放行。实测解法:sudo a2disconf serve-cgi-bin,并在 awstats.conf 内自带 ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/,重启后实测无认证 401、带认证 200。

Q4:报表里浏览器、操作系统、来源搜索全是空的? LogFormat 与日志格式不匹配的「静默残缺」(见 4.3 ⚠️)。Ubuntu Apache 默认 combined,配 LogFormat=1;用 tail -1 /var/log/apache2/access.log 对照确认每行是否含 referer 与 User-Agent 两个引号字段。

Q5:同一份日志重复导入会怎样? AWStats 按月度数据文件增量记账,重复喂同一批日志会重复计数(数据文件无幂等去重)。修正方式:删除 /var/lib/awstats/awstatsMMYYYY.<站点标识>.txt 后从全量日志重跑 -update(月度数据文件重建)。

Q6:想一次分析轮转的多份日志(access.log、access.log.1、access.log.2.gz)? 不要逐个跑 -update(顺序错会乱时间线),用官方合并器先合成一条流再喂给主程序,实测命令见《AWStats 常用 CLI 命令详解(awstats.pl 与日志预处理链)》。

九、卸载与清理

方案 A(apt)卸载:

sudo a2disconf awstats
sudo systemctl restart apache2
sudo apt purge -y awstats
sudo rm -rf /var/lib/awstats /etc/cron.d/awstats

方案 B(tar.gz)卸载:

sudo rm -rf /usr/local/awstats /var/lib/awstats80 /var/www/html/awstats8
sudo rm -f /etc/awstats/awstats.local8.conf

若不再需要 BasicAuth 凭据:sudo rm -f /etc/apache2/.htpasswd。

十、参考链接