WIKI使用導(dǎo)航
站長(zhǎng)百科導(dǎo)航
站長(zhǎng)專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢
- 虛擬主機(jī)
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計(jì)算
- 微博營(yíng)銷
- 虛擬主機(jī)管理系統(tǒng)
- 開放平臺(tái)
- WIKI程序與應(yīng)用
- 美國(guó)十大主機(jī)
CentOS/基于日志的站點(diǎn)統(tǒng)計(jì)系統(tǒng)的構(gòu)建
通過我們構(gòu)建的HTTP服務(wù)器將Web發(fā)布后,隨著不斷被訪問,Apache會(huì)記下每個(gè)訪客的基本信息。CentOS的情況下,這些信息通常保存在位于/var/log/httpd/下的相應(yīng)日志文件中。但Apache單調(diào)的日志,除了提供給我們一些最基本的服務(wù)器被訪信息、以及出錯(cuò)信息之外,我們很難單純的通過日志文件得到更多、更系統(tǒng)的關(guān)于時(shí)間、規(guī)律性、地點(diǎn)方面的信息。這也使得很難通過單純的日志去系統(tǒng)的分析流量。
但通過Awstats,我們可以把Apache的日志作為一種有效資源,獲得更加系統(tǒng)化的信息。
實(shí)質(zhì)上,Awstats就是一套分析系統(tǒng),它將日志文件統(tǒng)計(jì)學(xué)式的進(jìn)行分析與統(tǒng)計(jì),然后提供一個(gè)能夠反映規(guī)律性的界面給我們。Awstats并不是僅能應(yīng)用于分析Apache的日志,在郵件服務(wù)器等等方面,它也被廣泛的應(yīng)用。但在這一節(jié)中,只介紹在Web服務(wù)器中運(yùn)用Awstats建立統(tǒng)計(jì)系統(tǒng)的方法。
下載并安裝Awstats[ ]
首先下載并安裝Awstats。
[root@sample ~]# wget http://nchc.dl.sourceforge.net/sourceforge/awstats/awstats-6.5-1.noarch.rpm ← 下載Awstats的rpm包 --15:34:59-- http://nchc.dl.sourceforge.net/sourceforge/awstats/awstats-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[ ]
接下來對(duì)Awstats進(jìn)行初始配置。
[root@sample ~]# /usr/local/awstats/tools/awstats_configure.pl ← 運(yùn)行初始配置腳本 ----- 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,同意安裝到默認(rèn)目錄下 -----> 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,同意創(chuàng)建一個(gè)新的對(duì)象配置文件 -----> 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.centospub.com ← 為統(tǒng)計(jì)對(duì)象創(chuàng)建代號(hào)(推薦以網(wǎng)站URL作為代號(hào)) -----> 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.centospub.com.conf' Config file /etc/awstats/awstats.www.centospub.com.conf created. -----> Restart Web server with '/sbin/service httpd restart' ← HTTP服務(wù)重新啟動(dòng) 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.centospub.com Or if you have several config files and prefer having only one command: /root/tools/awstats_updateall.pl now Press ENTER to continue... ← 按回車?yán)^續(xù)進(jìn)行配置 A SIMPLE config file has been created: /etc/awstats/awstats.www.centospub.com.conf You should have a look inside to check and change manually main parameters. You can then manually update your statistics for 'www.centospub.com' with command: > perl awstats.pl -update -config=www.centospub.com You can also read your statistics for 'www.centospub.com' with URL: > http://localhost/awstats/awstats.pl?config=www.centospub.com Press ENTER to finish... ← 按回車結(jié)束初始配置
然后對(duì)剛剛創(chuàng)建的對(duì)象配置文件進(jìn)行配置。
[root@sample ~]# vi /etc/awstats/awstats.www.centospub.com.conf ← 修改對(duì)象配置文件(文件名與上一步初始配置中設(shè)置的域名對(duì)應(yīng)) LogFile="/var/log/httpd/mylog.log" ← 找到這一行,修改日至文件所在位置(指定Apache的日志文件) ↓ LogFile="/var/log/httpd/access_log" ← 變?yōu)榇藸顟B(tài) DirData="/var/lib/awstats" ← 找到這一行,更改Awstats數(shù)據(jù)的保存位置 ↓ DirData="." ← 變?yōu)榇藸顟B(tài),讓數(shù)據(jù)與awstats.pl腳本在同目錄下 Lang="auto" ← 找到這一行,將auto改為cn ↓ Lang="cn" ← 變?yōu)榇藸顟B(tài),讓語(yǔ)言默認(rèn)為中文 SkipHosts="" ← 找到這一行,在""之間添加無效訪問規(guī)則 ↓ SkipHosts="127.0.0.1 REGEX[^192\.168\.]" ← 變?yōu)榇藸顟B(tài),本地及內(nèi)部的訪問不做分析統(tǒng)計(jì) LevelForWormsDetection=0 ← 找到這一行,將0改為2 ↓ LevelForWormsDetection=2 ← 變?yōu)榇藸顟B(tài),將來自Worm的訪問也不做分析統(tǒng)計(jì)
接下來,再對(duì)httpd.conf中面向Awstats的部分進(jìn)行必要設(shè)置。
[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/" ---------------------------------------------------- 找到以上水平線間的語(yǔ)句群,對(duì)相應(yīng)路徑進(jìn)行正確修改。變?yōu)橄旅嫠骄€間狀態(tài): ---------------------------------------------------- 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"> ← 找到這一行,對(duì)相應(yīng)路徑進(jìn)行正確修改 ↓ <Directory "/usr/local/awstats/wwwroot"> ← 變?yōu)榇藸顟B(tài)
然后,重新啟動(dòng)HTTP服務(wù),使新的設(shè)置生效。
[root@sample ~]# /etc/rc.d/init.d/httpd restart ← 重新啟動(dòng)HTTP服務(wù),使設(shè)置生效 Stopping httpd: [ OK ] Starting httpd: [ OK ]
對(duì)日志進(jìn)行分析統(tǒng)計(jì)[ ]
配置完畢后,我們可以通過各種適用于自己的方式,定義統(tǒng)計(jì)的方式以及輸出的結(jié)果。在這里,介紹最簡(jiǎn)單,也是最廣泛的統(tǒng)計(jì)方法,如下:
[root@sample ~]# vi /root/awstats.sh ← 建立統(tǒng)計(jì)用腳本如下: #!/bin/bash /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.centospub.com (以上紫色字體部分的域名請(qǐng)根據(jù)實(shí)際情況設(shè)置) [root@sample ~]# chmod 700 /root/awstats.sh ← 賦予腳本可被執(zhí)行的屬性 [root@sample ~]# /root/awstats.sh ← 運(yùn)行腳本,開始進(jìn)行統(tǒng)計(jì)(如日志量比較大的話,要花一段時(shí)間) Update for config "/etc/awstats/awstats.www.centospub.com.conf" With data in log file "/var/log/httpd/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: 55 Found 52 dropped records, Found 0 corrupted records, Found 0 old records, Found 3 new qualified records.
然后在客戶端的瀏覽器上訪問http://服務(wù)器IP地址(或你的域名)/awstats/awstats.pl?config=www.centospub.com即可看到詳細(xì)的分析統(tǒng)計(jì)資料,如下圖:
定制統(tǒng)計(jì)分析規(guī)則及相應(yīng)外觀設(shè)置[ ]
[1]完成以上配置,我們已經(jīng)可以通過客戶端的瀏覽器,掌握Web服務(wù)器的訪問量、來路等等詳細(xì)的信息,但由于Awstats的統(tǒng)計(jì)是基于Apache日志的, 我們必須通過awstats.pl對(duì)當(dāng)前最新的日志文件進(jìn)行統(tǒng)計(jì)才能得到最新的統(tǒng)計(jì)結(jié)果。
也就是說,在我們運(yùn)行自行創(chuàng)建的awstats.sh腳本之前, 當(dāng)前在客戶端能夠看到的統(tǒng)計(jì)分析結(jié)果,只是上一次運(yùn)行awstats.sh腳本時(shí)統(tǒng)計(jì)的結(jié)果。想得到最新的統(tǒng)計(jì)結(jié)果,必須再次運(yùn)行awstats.sh。 在這里,我們將其設(shè)置為每天定時(shí)自動(dòng)運(yùn)行,以便于每天都能查看到最新的信息。如下:
[root@sample ~]# crontab -e ← 對(duì)定期任務(wù)執(zhí)行時(shí)間表進(jìn)行編輯,添加如下一行: 00 00 * * * /root/awstats.sh ← 添加此行到其中,讓統(tǒng)計(jì)分析在每天0點(diǎn)更新 (以上操作方法與vi編輯器相同)
[2] 在一些情況,可能并不希望外界看到日志統(tǒng)計(jì)分析的結(jié)果。對(duì)此,我們可以為Apache添加面向于Awstats的訪問限制,如下:
[root@sample ~]# vi /etc/httpd/conf.d/awstats.conf ← 創(chuàng)建面向于Awstats的配置文件 <Files "awstats.pl"> AllowOverride None Order deny,allow Deny from all Allow from 192.168.0 ← 只允許局域網(wǎng)內(nèi)訪問 </Files> [root@sample ~]# /etc/rc.d/init.d/httpd restart ← 重新啟動(dòng)HTTP服務(wù),使設(shè)置生效 Stopping httpd: [ OK ] Starting httpd: [ OK ]
[3]用Firefox瀏覽分析結(jié)果時(shí),字體會(huì)顯得很難看(因?yàn)镕irefox和IE識(shí)別11px字體不一樣),所以用Firefox瀏覽器的情況,可以通過修改awstats.pl輸出css的相關(guān)字體尺寸進(jìn)行修改,而達(dá)到用Firefox瀏覽時(shí)也和IE一樣能夠正確顯示出適當(dāng)尺寸的字體。方法就是直接編輯"/usr/local/awstats/wwwroot/cgi-bin/awstats.pl",將文件中所有“11px”的地方都替換為“12px。
參考來源[ ]
http://www.centospub.com/make/awstats.html