awstats訪問日誌統計安裝

 CentOS下基於Awstats日誌的站點統計系統的構建
前  言

  通過我們構建的HTTP服務器將Web發佈後,隨着不斷被訪問,Apache會記下每個訪客的基本信息。CentOS的情況下,這些信息通常保存在位於/var/log/httpd/下的相應日誌文件中。但Apache單調的日誌,除了提供給我們一些最基本的服務器被訪信息、以及出錯信息之外,我們很難單純的通過日誌文件得到更多、更系統的關於時間、規律性、地點方面的信息。這也使得很難通過單純的日誌去系統的分析流量。

  但通過Awstats,我們可以把Apache的日誌作爲一種有效資源,獲得更加系統化的信息。

  實質上,Awstats就是一套分析系統,它將日誌文件統計學式的進行分析與統計,然後提供一個能夠反映規律性的界面給我們。Awstats並不是僅能應用於分析Apache的日誌,在郵件服務器等等方面,它也被廣泛的應用。但在這一節中,只介紹在Web服務器中運用Awstats建立統計系統的方法。

下載並安裝 Awstats

   首先下載並安裝Awstats。

[root@sample ~]# wget http://nchc.dl.sourceforge.net/s ... ts-6.5-1.noarch.rpm  ← 下載Awstats的rpm包

--15:34:59-- http://nchc.dl.sourceforge.net/s ... ts-6.5-1.noarch.rpm
=> `awstats-6.5-1.noarch.rpm'
Resolving nchc.dl.sourceforge.net... 211.79.61.10
Connecting to nchc.dl.sourceforge.net|211.79.61.10|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1,095,131 (1.0M) [text/plain]

100%[====================================>] 1,095,131 155.28K/s ETA 00:00

15:35:06 (158.94 KB/s) - `awstats-6.5-1.noarch.rpm' saved [1095131/1095131]

[root@sample ~]# rpm -ivh awstats-6.5-1.noarch.rpm ← 安裝Awstats

Preparing... ########################################### [100%]
1:awstats ########################################### [100%]

----- AWStats 6.5 - Laurent Destailleur -----
AWStats files have been installed in /usr/local/awstats
If first install, follow instructions in documentation
(/usr/local/awstats/docs/index.html) to setup AWStats in 3 steps:
Step 1 : Install and Setup with awstats_configure.pl (or manually)
Step 2 : Build/Update Statistics with awstats.pl
Step 3 : Read Statistics

  然後刪除安裝後的遺留源文件。

[root@sample ~]# rm -f awstats-6.5-1.noarch.rpm  ← 刪除安裝後的遺留源文件

配置 Awstats

   接下來對Awstats進行初始配置。

[root@sample ~]# /usr/local/awstats/tools/awstats_configure.pl  ← 運行初始配置腳本

----- AWStats awstats_configure 1.0 (build 1.6) (c) Laurent Destailleur -----
This tool will help you to configure AWStats to analyze statistics for
one web server. You can try to use it to let it do all that is possible
in AWStats setup, however following the step by step manual setup
documentation (docs/index.html) is often a better idea. Above all if:
- You are not an administrator user,
- You want to analyze downloaded log files without web server,
- You want to analyze mail or ftp log files instead of web log files,
- You need to analyze load balanced servers log files,
- You want to 'understand' all possible ways to use AWStats...
Read the AWStats documentation (docs/index.html).

-----> Running OS detected: Linux, BSD or Unix
Warning: AWStats standard directory on Linux OS is '/usr/local/awstats'.
If you want to use standard directory, you should first move all content
of AWStats distribution from current directory:
/root
to standard directory:
/usr/local/awstats
And then, run configure.pl from this location.
Do you want to continue setup from this NON standard directory [yN] ? y  ← 選擇y,同意安裝到默認目錄下

-----> Check for web server install

Enter full config file path of your Web server.
Example: /etc/httpd/httpd.conf
Example: /usr/local/apache2/conf/httpd.conf
Example: c:/Program files/apache group/apache/conf/httpd.conf
Config file path ('none' to skip web server setup):
> /etc/httpd/conf/httpd.conf  ← 指定Apache配置文件所在位置

-----> Check and complete web server config file '/etc/httpd/conf/httpd.conf'
Add 'Alias /awstatsclasses "/root/wwwroot/classes/"'
Add 'Alias /awstatscss "/root/wwwroot/css/"'
Add 'Alias /awstatsicons "/root/wwwroot/icon/"'
Add 'ScriptAlias /awstats/ "/root/wwwroot/cgi-bin/"'
Add '<Directory>' directive
AWStats directives added to Apache config file.

-----> Update model config file '/etc/awstats/awstats.model.conf'
File awstats.model.conf updated.

-----> Need to create a new config file ?
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ? y  ← 選擇y,同意創建一個新的對象配置文件

-----> Define config file name to create
What is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Your web site, virtual server or profile name:
> www.fuancn.cn   ← 爲統計對象創建代號(推薦以網站URL作爲代號)#p#分頁標題#e#

-----> Define config file path
In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directory path to store config file(s) (Enter for default):
>  ← 直接按回車,接受將Awstats的配置文件置於/etc/awstats下

-----> Create config file '/etc/awstats/awstats.www.fuancn.cn.conf'
Config file /etc/awstats/awstats.www.fuancn.cn.conf created.

-----> Restart Web server with '/sbin/service httpd restart'  ← HTTP服務重新啓動

Stopping httpd:               [ OK ]
Starting httpd:               [ OK ]

-----> Add update process inside a scheduler
Sorry, configure.pl does not support automatic add to cron yet.
You can do it manually by adding the following command to your cron:
/root/wwwroot/cgi-bin/awstats.pl -update -config=www.fuancn.cn
Or if you have several config files and prefer having only one command:
/root/tools/awstats_updateall.pl now
Press ENTER to continue...  ← 按回車繼續進行配置

A SIMPLE config file has been created: /etc/awstats/awstats.www.fuancn.cn.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.fuancn.cn' with command:
> perl awstats.pl -update -config=www.fuancn.cn
You can also read your statistics for 'www.fuancn.cn' with URL:
> http://localhost/awstats/awstats.pl?config=www.fuancn.cn
Press ENTER to finish...  ← 按回車結束初始配置

  然後對剛剛創建的對象配置文件進行配置。

[root@sample ~]# vi /etc/awstats/awstats.www.fuancn.cn.conf  ← 修改對象配置文件(文件名與上一步初始配置中設置的域名對應)

LogFile="/var/log/httpd/mylog.log"  ← 找到這一行,修改日至文件所在位置(指定Apache的日誌文件)
 ↓
LogFile="/var/log/httpd/access_log"   ← 變爲此狀態

DirData="/var/lib/awstats"  ← 找到這一行,更改Awstats數據的保存位置
 ↓
DirData="."  ← 變爲此狀態,讓數據與awstats.pl腳本在同目錄下

Lang="auto"  ← 找到這一行,將auto改爲cn
 ↓
Lang="cn"   ← 變爲此狀態,讓語言默認爲中文

SkipHosts=""  ← 找到這一行,在""之間添加無效訪問規則
 ↓
SkipHosts="127.0.0.1 REGEX[^192/.168/.]"   ← 變爲此狀態,本地及內部的訪問不做分析統計

LevelForWormsDetection=0  ← 找到這一行,將0改爲2
 ↓
LevelForWormsDetection=2  ← 變爲此狀態,將來自Worm的訪問也不做分析統計

  接下來,再對httpd.conf中面向Awstats的部分進行必要設置。

[root@sample ~]# vi /etc/httpd/conf/httpd.conf  ← 編輯Apache的配置文件

#
# Directives to allow use of AWStats as a CGI
#
----------------------------------------------------
Alias /awstatsclasses "/root/wwwroot/classes/"
Alias /awstatscss "/root/wwwroot/css/"
Alias /awstatsicons "/root/wwwroot/icon/"
ScriptAlias /awstats/ "/root/wwwroot/cgi-bin/"
----------------------------------------------------

找到以上水平線間的語句羣,對相應路徑進行正確修改。變爲下面水平線間狀態:

----------------------------------------------------
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 "/root/wwwroot">  ← 找到這一行,對相應路徑進行正確修改
 ↓
<Directory "/usr/local/awstats/wwwroot">  ← 變爲此狀態

  然後,重新啓動HTTP服務,使新的設置生效。

[root@sample ~]# /etc/rc.d/init.d/httpd restart  ← 重新啓動HTTP服務,使設置生效

Stopping httpd:               [ OK ]
Starting httpd:               [ OK ]

對日誌進行分析統計

  配置完畢後,我們可以通過各種適用於自己的方式,定義統計的方式以及輸出的結果。在這裏,介紹最簡單,也是最廣泛的統計方法,如下:

[root@sample ~]# vi /root/awstats.sh  ← 建立統計用腳本如下:

#!/bin/bash
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.fuancn.cn
(以上紫色字體部分的域名請根據實際情況設置)

[root@sample ~]# chmod 700 /root/awstats.sh   ← 賦予腳本可被執行的屬性

[root@sample ~]# /root/awstats.sh   ← 運行腳本,開始進行統計(如日誌量比較大的話,要花一段時間)

Update for config "/etc/awstats/awstats.www.fuancn.cn.conf"
With data in log file "/var/log/httpd/access_log"...
Phase 1 : First bypass old records, searching new record...#p#分頁標題#e#
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: 55
Found 52 dropped records,
Found 0 corrupted records,
Found 0 old records,
Found 3 new qualified records.

然後在客戶端的瀏覽器上訪問 http://服務器IP地址(或你的域名)/awstats/awstats.pl?config=www.fuancn.cn 即可看到詳細的分析統計資料,如下圖:

 

定製統計分析規則及相應外觀設置

[1]完成以上配置,我們已經可以通過客戶端的瀏覽器,掌握Web服務器的訪問量、來路等等詳細的信息,但由於Awstats的統計是基於Apache日誌的,我們必須通過awstats.pl對當前最新的日誌文件進行統計才能得到最新的統計結果。也就是說,在我們運行自行創建的awstats.sh腳本之前,當前在客戶端能夠看到的統計分析結果,只是上一次運行awstats.sh腳本時統計的結果。想得到最新的統計結果,必須再次運行awstats.sh。在這裏,我們將其設置爲每天定時自動運行,以便於每天都能查看到最新的信息。如下:

[root@sample ~]# crontab -e  ← 對定期任務執行時間表進行編輯,添加如下一行:

00 00 * * * /root/awstats.sh  ← 添加此行到其中,讓統計分析在每天0點更新

(以上操作方法與vi編輯器相同)

[2] 在一些情況,可能並不希望外界看到日誌統計分析的結果。對此,我們可以爲Apache添加面向於Awstats的訪問限制,如下:
[root@sample ~]# vi /etc/httpd/conf.d/awstats.conf  ← 創建面向於Awstats的配置文件

<Files "awstats.pl">
AllowOverride None
Order deny,allow
Deny from all
Allow from 192.168.0  ← 只允許局域網內訪問
</Files>


[root@sample ~]# /etc/rc.d/init.d/httpd restart  ← 重新啓動HTTP服務,使設置生效

Stopping httpd:               [ OK ]
Starting httpd:               [ OK ]


[3]用Firefox瀏覽分析結果時,字體會顯得很難看(因爲Firefox和IE識別11px字體不一樣),所以用Firefox瀏覽器的情況,可以通過修改awstats.pl輸出css的相關字體尺寸進行修改,而達到用Firefox瀏覽時也和IE一樣能夠正確顯示出適當尺寸的字體。方法就是直接編輯"/usr/local/awstats/wwwroot/cgi-bin/awstats.pl",將文件中所有“11px”的地方都替換爲“12px。

 

訪問路徑    http://192.168.1.254/awstats/awstats.pl?config=auth.biokee.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章