Awstats的安裝基本配置
1. AWSTATS 簡介
AWStats,全名為 Advanced Web Statistics,是一套免費功能強大並支援多國語言的工具程式,專門用來分析Web.Streaming.FTP或Mail伺服器的統計資料,並可產生統計圖表,顯示用戶端連線至網站存取網頁的紀錄資訊。
它可以分析許多主要伺服器的日誌檔,如 Apache 的日誌檔 (NCSA combined/XLF/ELF log format or common/CLF log format)、WebStar、IIS (W3C log format) 以及許多其它的 Web、Proxy、Wap、Streaming 伺服器、Mail 伺服器及部份 FTP 伺服器。
AWStats 與其它統計分析軟體(Analog、Webalizer、HitBox…)相較之下,可以產生更多樣的紀錄,並做更詳細的分析。你可以到這裏檢視比較著名的分析工具 (AWStats、Analog、Webalizer…)的功能及差異性:功能比較表。
AWStats 是免費的 GPL (GNU General Public License) 授權軟體,你可以檢視 license chart 看哪些是可以或不能做的。
AWStats 是以 Perl 透過指令列的方式執行,但也可以從瀏覽器透過 CGI 的方式來做即時更新的動作,因為請確定係統中已安裝 Perl 程式或套件,並啟動 CGI 的執行權限。
2. 安裝 AWSTATS
下載
首先需先下載 AWStats 軟體,請到官方網站查看最新版本,或直接進入下載頁麵。 目前我使用的是 6.7 版,使用Windows的話可以下載zip或exe檔;使用Linux的話可以下載 rpm 或 tar.gz 檔。
安裝
目前使用的環境是 CentOS 5(RHEL),所以直接下載 rpm 檔安裝即可:
可以到如下的地址下載
https://awstats.sourceforge.net/files/awstats-6.8.tar.gz
# rpm -ivh https://jaist.dl.sourceforge.net/sourceforge/awstats/awstats-6.7-1.noarch.rpm
或下載rpm檔後執行
# rpm -ivh awstats-6.7-1.noarch.rpm
安裝後檢查
安裝後 AWStats 的相關資料位置如下(以rpm版本安裝):
.主目錄:/usr/local/awstats
.網站設定檔目錄:/etc/awstats
.網站設定檔範例:/etc/awstats/awstats.model.conf
.其它路徑說明:
/usr/local/awstats/wwwroot 預設awstats網站根目錄
/usr/local/awstats/tools awstats 工具
/usr/local/awstats/docs 說明文件,索引頁為 index.html
/usr/local/awstats/wwwroot/cgi-bin awstats.pl 主程式所在目錄
/usr/local/awstats/wwwroot/cgi-bin/lang 語係檔目錄
…………. (其它請參閱說明文件)
若你使用的是 Windows 係統,請先安裝 Perl (安裝後可能需要重新開機),再安裝 AWStats 程式。
3. 使用 AWSTATS 提供的工具設定
若你是初次設定 awstats,建議可以使用它本身所提供的工具,您可以下以下的指令:
#chown apache:apache wwwroot/ -R
最好改變一下這個文件夾的權限
# cd /usr/local/awstats/tools
# perl awstats_configure.pl
接下來就會問您一些問題,如 Apache 主目錄、設定檔、網域(主機)名稱……等,目的是為了幫您修改 apache 的設定資訊以及基本的 awstat.conf 檔。
若不執行這個步驟也是可以,就手動來編輯 awstats for apache 的設定資訊。
詳細介紹見本文後半部分
4. APACHE 中的 AWSTATS 設定
使用工具產生的設定
若您使用上述的方式,在您的 httpd.conf 設定檔的文件尾會被加上這樣的資訊:
#
# Directives to add to your Apache conf file to allow use of AWStats as a CGI.
# Note that path “/usr/local/awstats/” must reflect your AWStats Installation path.
#
Alias /awstatsclasses “/usr/local/awstats/wwwroot/classes/”
Alias /awstatscss “/usr/local/awstats/wwwroot/css/”
Alias /awstatsicons “/usr/local/awstats/wwwroot/icon/”
ScriptAlias /awstats/ “/usr/local/awstats/wwwroot/cgi-bin/”
#
# This is to permit URL access to scripts/files in AWStats directory.
#
<Directory “/usr/local/awstats/wwwroot”>
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>
手動設定
若您不想使用工具,那麼可以手動將上麵的設定值加入 httpd.conf 檔中,或者是直接編輯一個 awstats.conf 檔放入 /etc/httpd/conf.d/ 目錄下:
# vi /etc/httpd/conf.d/awstats.conf
內容為
Alias /awstatsclasses “/usr/local/awstats/wwwroot/classes/”
Alias /awstatscss “/usr/local/awstats/wwwroot/css/”
Alias /awstatsicons “/usr/local/awstats/wwwroot/icon/”
ScriptAlias /awstats/ “/usr/local/awstats/wwwroot/cgi-bin/”
#
# This is to permit URL access to scripts/files in AWStats directory.
#
<Directory “/usr/local/awstats/wwwroot”>
Options None
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>
驗證機製
不管如何,建議 AllowOverride 設定成 AuthConfig,針對統計資料目錄做驗證的動作,避免網站細部日誌被有心人士利用。另外必需在欲保護的網頁目錄下(即/usr/local/awstats/wwwroot)建立 .htaccess 檔案,並設定適當權限:
# cd /usr/local/awstats/wwwroot
# vi .htaccess
內容為
AuthName “Private Web Site”
Authtype Basic
AuthUserFile /etc/httpd/conf/.htpasswd
require valid-user
設定適當權限
# chown apache:apache .htaccess
# chmod 700 .htaccess
建立密碼檔
# htpasswd -c /etc/httpd/conf/.htpasswd yourname
輸入2次密碼即可
關於Apache詳細驗證機製的流程與作法,請參考相關教學文章。
日誌設定
另外,在 httpd.conf 中有關日誌部份的設定建議設定為:
將
CustomLog /yourlogpath/yourlogfile common
更改為
CustomLog /yourlogpath/yourlogfile combined
虛擬站點日誌設定
或者是你有架設虛擬站點,可以針對不同的站台設定不同的日誌記錄檔,未來也可以分析各個虛擬站點的細部統計:
# 虛擬站台(Name-based)範例
NameVirtualHost 192.168.1.100
# === domain1.com.cn ===
<VirtualHost 192.168.1.100>
ServerAdmin webadm@domain1.com.cn
DocumentRoot /var/www/domain1
ServerName www.domain1.com.cn
ErrorLog logs/domain1.com.cn-error_log
CustomLog logs/domain1.com.cn-access_log combined
</VirtualHost>
# === domain2.com.cn ===
<VirtualHost 192.168.1.100>
ServerAdmin webadm@domain2.com.cn
DocumentRoot /var/www/domain2
ServerName www.domain2.com.cn
ErrorLog logs/domain2.com.cn-error_log
CustomLog logs/domain2.com.cn-access_log combined
</VirtualHost>
# === domain3.com.cn ===
<VirtualHost 192.168.1.100>
ServerAdmin webadm@domain3.com.cn
DocumentRoot /var/www/domain3
ServerName www.domain3.com.cn
ErrorLog logs/domain3.com.cn-error_log
CustomLog logs/domain3.com.cn-access_log combined
</VirtualHost>
這樣可以提供更詳細的日誌記錄,日誌內容如:
xx.xxx.xx.xx – - [dd/mmm/yyyy:hh:mm:ss +0000] “GET / HTTP/1.1″ 200 1234 “https://www.php-oa.com/from.html” “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)”
重新啟動 Apache
更改完 apache 的設定檔後,記後重新啟動讓設定值生效:
# /etc/init.d/httpd restart
5. 建立網站的 AWSTATS 設定檔
當您使用 perl awstats_configure.pl 指令來建立起 awstats.mysite.conf 設定檔後,接下來我們就要來修改設定檔中部份的設定內容,因為設定資料繁多,在這裏隻要針對重點部份設定即可。
複製/修改設定檔
# cd /etc/awstats
# cp awstats.model.conf awstats.domain1.conf
# vi awstats.domain1.conf
找到底下這幾行,並且修改內容:
# 日誌檔位置
LogFile=”/var/log/httpd/domain1.com-access_log”
# 日誌檔類型:W – WEB,S – Streaming,M – Mail,F – FTP
LogType=W
# 日誌格式 缺省值是1:Apache日誌,2是IIS日誌
LogFormat=1
# 主機名稱或網域
SiteDomain=”www.domain1.com”
# 主機別名
HostAliases=”www.domain1.com domain1.com localhost 127.0.0.1 \
REGEX[domain1\.com\.cn$] ”
# 存放 awstats 分析完成的資料庫檔案存放目錄
# 這邊修改成 “.” 表示跟 awstats.pl 相同目錄,即 /usr/local/awstats/wwwroot/cgi-bin
DirData=”.”
# 執行 awstats 的目錄:更改成之前在 httpd.conf 或 awstats.conf 中設定 ailas 的路徑
DirCgi=”/awstats”
# 圖示目錄:更改成之前在 httpd.conf 或 awstats.conf 中設定 ailas 的路徑
DirIcons=”/awstatsicons”
# 設定語係:預設為 GB2312,可依需求修改為 UTF8 (請參考下麵更改語係檔設定)
Lang=”cn”
若您希望更細部的設定,請參考安裝後 awstats 所提供文件,或參考設定檔內容說明。
6.Awstats的中文問題(補丁)
GB2312 碼轉UTF8 碼
中文部份預設為GB2312碼,但由於現在許多網站都使用UTF-8的編碼方式,這會造成部份統計網頁內容會有亂碼出現,可以用以下方式來將原語係檔轉換成UTF-8的編碼使用。
解決方法不詳細寫了,下一個下麵的補丁,就能解決問題
中文等的補丁在:https://www.chedong.com/tech/lib.tgz
#cd /usr/local/awstats/wwwroot/cgi-bin
#wget https://www.chedong.com/tech/lib.tgz
#cp -r /usr/local/awstats/wwwroot/cgi-bin/lib /usr/local/awstats/wwwroot/cgi-bin/lib.bak
# tar zxvf lib.tgz
7. 測試設定檔
產生統計報表檔
接下來就要測試設定檔是否設定正確,以便順利產生相關檔案:
# cd /usr/local/awstats/wwwroot/cgi-bin
# perl awstats.pl -config=domain1 -update
這裏的 -config=domain1 指的就是 awstats.domain1.conf 的設定檔
Update for config “/etc/awstats/awstats.domain1.conf”
With data in log file “/var/log/httpd/domain1.com.-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: 225730
Found 122 dropped records,
Found 87 corrupted records,
Found 0 old records,
Found 225521 new qualified records.
# ls -l
awstats012007.domain1.txt (這就是awstats建立的domain1 2007年01月的統計資料檔)
awredir.pl
awstats.pl
如果有看到以上的資料,表示您的awstats設定檔正確,而且awstats可以正確分析您apache所產生的log 檔。
AWStats的運行模式是這樣的:
1.分析日誌:運行後將這樣的日誌統計結果歸檔到一個AWStats的數據庫(純文本)裏;
2.然後是輸出:分兩種形式
?一種是通過cgi程序讀取統計結果數據庫輸出;
?一種是運行後台腳本將輸出導出成靜態文件;
動態:
https://localhost/cgi-bin/awstats/awstats.pl?config=www.php-oa.com
添加定時分析指令
crontab -e
* */1 * * * (cd /var/www/awstats/cgi-bin/; ./awstats.pl -update -config=www.php-oa.com)
例:靜態生成
若要建立個別站點的統計資料,並且匯出個別的網頁(html)檔,可以編輯一個script檔:
# vi /usr/local/awstats/wwwroot/cgi-bin/upawstats.sh
內容為
#!/bin/bash
cd /usr/local/awstats/wwwroot/cgi-bin
perl awstats.pl -config=domain1 -output > domain1.html
perl awstats.pl -config=domain2 -output > domain2.html
perl awstats.pl -config=domain3 -output > domain3.html
如果還有其它的網站,可依此類推;另外您也可以參考awstats原廠文件的做法。
記得順便將權限修改為700:
# chmod 700 /usr/local/awstats/wwwroot/cgi-bin/upawstats.sh
權限設定完之後,先將這個script執行一次,若無任何問題,接下來就是將它放入排程工作即可。
檢視統計圖表
剛才上麵執行過的script所產生的檔案,我們就可以透過瀏覽器看查看統計資料了,您可以連到伺服器上的相關網址查看,例如“https://你的IP/awstats/domain1.html”:
統計資料很多,也可以選擇指定的月份報表,這部份就不多做介紹了。
8. 設定排程
設定排程讓 awstats 定時分析 log 即可:
# crontab -e 或 vi /etc/crontab
內容為
0 3,15 * * * /usr/local/awstats/wwwroot/cgi-bin/upawstats.sh
上麵安排在淩晨3點及下午3點執行更新,如果不會設定排程,請參考排程相關教學文章。
9. 結語
有了這樣的日誌記錄分析統計軟體,看網站日誌真的方便多了,但是日誌還是要保留一段時間,畢竟若有惡意攻擊事件,還是得分析LOG檔內容才行。
最後更新:2017-01-04 22:34:31