AWStats 是 SourceForge 上迭代極快的開源日誌分析工具,底層以 Perl 撰寫。相比同類老牌的 Webalizer,它的長處相當明顯。
它能輸出的指標包括:訪問量、訪問次數、頁面瀏覽量、點擊數、流量;月/日/時三級粒度;訪客國家與 IP;Robots/Spiders 統計;停留時長;檔案類型分佈;Pages-URL 排行;作業系統與瀏覽器;以及搜尋關鍵字等衍生資訊。與 Webalizer、analog 的横向比較可參考官網 https://awstats.sourceforge.net/#COMPARISON 。
AWStats 分兩步走:先解析日誌,把統計結果寫入一個純文字構成的 AWStats 資料庫;再輸出報表。輸出有兩種形態——由 CGI 程式即時讀庫呈現,或透過後台腳本匯出成靜態頁面。
https://nchc.dl.sourceforge.net/project/awstats/AWStats/7.0/awstats-7.0.tar.gz ll awstats-7.0.tar.gz -rw-r--r-- 1 root root 1254327 5月 23 05:06 awstats-7.0.tar.gz tar xf awstats-7.0.tar.gz mv awstats-7.0 /usr/local/awstats cd /usr/local/awstats ls docs README.TXT tools wwwroot
執行自帶的配置腳本,它會自動改寫 Apache 主設定檔並生成站點配置文件:
perl tools/awstats_configure.pl
腳本會先偵測作業系統與 Web 伺服器,接著要求輸入 Apache 主設定檔路徑(本例為 /etc/httpd/conf/httpd.conf),然後自動寫入 Alias 與 ScriptAlias 指向 AWStats 的 classes、css、icon、cgi-bin 目錄,並更新 awstats.model.conf 模板。
接著回答 y 建立新的 profile,輸入站點名稱(本例 www.sxkeji.com.cn),配置檔存放目錄保留預設 /etc/awstats,於是產生 /etc/awstats/awstats.www.sxkeji.com.cn.conf。腳本最後提示重啟 httpd,並說明 configure.pl 尚不支援自動寫入 cron,需手動加入:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.sxkeji.com.cn # 多個 profile 時可用一條指令搞定 /usr/local/awstats/tools/awstats_updateall.pl now
此時也能透過 https://localhost/awstats/awstats.pl?config=www.sxkeji.com.cn 預覽報表。
mkdir /var/lib/awstats vim /etc/awstats/awstats.www.sxkeji.com.cn.conf cat /etc/awstats/awstats.www.sxkeji.com.cn.conf | grep -vnE '^$|^#' | less
需要重點確認的幾行:
51:LogFile="/var/log/httpd/access_log" # Apache 日誌位置 63:LogType=W 122:LogFormat=1 # 沿用 httpd 預設格式 134:LogSeparator=" " 153:SiteDomain="www.sxkeji.com.cn" 168:HostAliases="sxkeji.com.cn www.sxkeji.com.cn 127.0.0.1 localhost" 188:DNSLookup=2 203:DirData="/var/lib/awstats" # 統計資料庫目錄
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl --update --config=www.sxkeji.com.cn
該指令會自動讀取 /etc/awstats/awstats.www.sxkeji.com.cn.conf,分兩階段處理:先跳過舊記錄、定位新記錄,再逐行解析並寫入歷史資料。輸出中會列出跳過行數、解析行數,以及 dropped/comments/blank/corrupted/old/new qualified 各類計數,方便判斷日誌格式是否匹配。日常維運把這條指令掛進 crontab 即可實現定期更新。