原文搬運: https://blog.ccnrz.info/2016/03/31/2016-03-31-it-awstats-application-built-with/
AWStats 是一套免費的日誌分析工具,用來分析 web, streaming, ftp or mail server 日誌。
工具運行在 apache 環境下。
文章中將會提到建置分析 apache 日誌環境與幾個常見問題,下列操作均以 sudo
方式進行。
將 AWStats 安裝至 Ubuntu
安裝 awstats 套件
apt-get install awstats
設定 AWStats
首先建立一份 www.abc.com
的設定檔
cp /etc/awstats/awstats.conf /etc/awstats/awstats.www.abc.com.conf
修改設定檔
vim /etc/awstats/awstats.www.abc.com.conf
進行如下設定
LogFile="/var/log/apache2/access.log"
SiteDomain="www.abc.com"
HostAliases="localhost 127.0.0.1 www.abc.com"
LogFile
變數為指定要分析 apache log 檔位置
如果要分析的不是 apache log, 還須設定 LogType
以及 LogFormat
這兩個變數
產生統計資料
/usr/lib/cgi-bin/awstats.pl -config=www.abc.com -update
如果是分析現有的 LOG,第一次會需要比較長的時間。(之前測試 73MB 的 log 跑了大概一小時多)
後續透過 crontab
排程進行,會比較快。
0 */3 * * * /usr/lib/cgi-bin/awstats.pl -config=yourdomain.ext -update > /dev/null
設定 apache 以檢視 awstats 的統計結果
啟用 apache 的 cgi
模組
a2enmod cgi
修改 apache web site 的設定檔
vim /etc/apache2/sites-available/000-default.conf
於 conf 文件末行新增下列內容
Alias /awstatsclasses "/usr/share/awstats/lib/"
Alias /awstats-icon "/usr/share/awstats/icon/"
Alias /awstatscss "/usr/share/doc/awstats/examples/css"
ScriptAlias /awstats/ /usr/lib/cgi-bin/
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
重新載入 apache 設定檔
service apache2 reload
檢視分析結果
開啟瀏覽器輸入下列網址
http://www.abc.com/awstats/awstats.pl?config=www.abc.com
網址中 config
的參數為指定要看 www.abc.com
設定檔產出的分析結果
常見問題
如果我有很多個日誌檔,要如何餵給 AWStats 呢?
AWstats 有一個日誌合併工具 /usr/share/awstats/tools/logresolvemerge.pl
比如說我在 /home/xxx/apache_log
下有 access.log
access.log.1.bz2
access.log.2.bz2
三個日誌
那麼可以合併成 merged-access.log
/usr/share/awstats/tools/logresolvemerge.pl /home/xxx/apache_log/access.log* > merged-access.log
/var/lib/awstats 目錄肥大 (2018/3/29 新增)
有時會遇到 /var/lib/awstats 目錄下 awstats122017.www.abc.com.tmp.58900 大量 tmp 檔案的情況,可以手動進行刪除。
ls /var/lib/awstats | grep -P "^awstats\d{6}\.([\w|\.])+\.tmp.\d+$" | xargs -d"\n" rm