awstats的安裝和配置

1、下載awstats軟件

鏈接地址:https://prdownloads.sourceforge.net/awstats/awstats-7.5.tar.gz

wKiom1gikv3jjMtWAAA70zUlU9I782.jpg-wh_50

2、安裝

將下載的軟件直接放到/usr/local/下解壓,改名成awstats


2、生成配置文件

cd /usr/local/awstats/tools
./awstats_configure.pl
----- AWStats awstats_configure 1.0 (build 20140126) (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
-----> 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):
> none (此處如果是apache就寫上apache的配置文件路徑,我用的是nginx所以就none)
Your web server config file(s) could not be found.
You will need to setup your web server manually to declare AWStats
script as a CGI, if you want to build reports dynamically.
See AWStats setup documentation (file docs/index.html)
-----> Update model config file '/usr/local/awstats/wwwroot/cgi-bin/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
-----> 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.xxx.com(此處寫上你要分析的域名)
-----> 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):
> /etc/awstats (此處寫上你要存放awstats的配置文件的路徑,一般都放在/etc/awstats,如果自定義在後面分析的時候會找不到配置文件)
-----> Create config file '/etc/awstats/awstats.www.xxxx.com.conf'
Config file /etc/awstats/awstats.www.xxxx.com.conf created.
-----> 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:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.xxxx.com
Or if you have several config files and prefer having only one command:
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue...
A SIMPLE config file has been created: /etc/awstats/awstats.www.xxxx.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.xxxx.com' with command:
> perl awstats.pl -update -config=www.xxxx.com
You can also build static report pages for 'www.xxxx.com' with command:
> perl awstats.pl -output=pagetype -config=www.xxxx.com
Press ENTER to finish…

完成後,配置文件就生成了。如果有多個域名需要分析,就多執行幾次,每個域名對應一個配置文件

3、日誌格式處理及awstats配置文件的修改

因爲我這nginx的日誌格式是自定義的格式,寫的比較多,所以分析的時候就處理下格式,只取了不部分日誌

取值的字段:$remote_addr  $request $status  $body_bytes_sent  $http_referer  $time_local $http_user_agent

因爲$time_local和$http_user_agent 字符串中間有空格的問題,所以在處理日誌的時候給這兩個字段加了中括號處理

處理日誌的語句如下:

awk -F'|' '{print $3" "$12" "$5" "$14" "$11" ""["$2"]"" ""["$13"]”}’access.log >access_20161101.log

有了日誌格式我們就可以去修改awstats對應域名的日誌格式了

vim /etc/awstats/awstats.www.xxxx.com.conf
LogFormat = "%host %method %url %other %code %bytesd %referer %time1 %uabracket”
LogSeparator=“” #按默認空格分隔

還有需要修改的地方是:

#存放log的路徑,最後%YYYY-24%MM-24%DD-24代表是當前日期的前一天,今天是20161109,那此處轉換後就是20161108,如果要取當天的日誌文件,把-24改成-0就可以了。

LogFile="/data/awstats/logs/www/combine/access_%YYYY-24%MM-24%DD-24.log”

#生成分析完的結果目錄

DirData="/data/awstats/web”

#確認域名,如果是生成的配置文件,只要域名寫對了,這地方就沒什麼問題,如果是從其他配置文件copy過來就需要記得修改下面這兩項

SiteDomain="www.xxxx.com”
HostAliases="xxxx.com www.xxxx.com 127.0.0.1 localhost”

#如果同一個域名有多個nginx的日誌需要合並可以使用awstats自帶的工具/usr/local/awstats/tools/logresolvemerge.pl

使用方法:

logresolvemerge.pl access1.log access2.log accessN.log >access_20161109.log

因我這是讓awstats的CGI來處理分析結果所以需要安裝perl的FCGI和FCI-ProcManager來支持

4、perl支持的FCGI和FCGI-ProcManager的安裝

wKiom1gik2SBG-eSAAE0a5ebpKQ261.jpg-wh_50

    install FCGI::ProcManger

wKioL1gik3PQtvvRAASnH1ZLkUU285.jpg-wh_50


啓動fcgi來支持CGI的解析

從網上下載了fcgi的腳本,nginx調用cgi的時候我用的是ip地址方式連接,所以將文件中$socket = FCGI::OpenSocket( "127.0.0.1:8999", 10 );的註釋去掉了,將 #$socket = FCGI::OpenSocket( "/usr/local/awstats/perl_cgi-dispatch.sock", 10 );給註釋,如果是使用unix套接字來訪來訪問就掉個個就可以。

啓動/usr/local/awstats/fcgi &

查看是否啓動:netstat -ntpl|grep 8999

5、QQ純真庫支持顯示來訪IP的地理位置信息

從網上找得三個文件qqhostinfo.pm qqwry.dat qqwry.pl,將這三個文件存放在/usr/local/awstats/wwwroot/cgi-bin/plugins下,然後修改awstats的域名配置文件中的一行

將LoadPlugin=“hostinfo"
修改成LoadPlugin=“qqhostinfo"

如果本身是註釋的,請將註釋去掉

效果圖:

wKiom1gik4TBQEQRAADFFVG8A4s218.jpg-wh_50

6、nginx配置

新建一個虛擬主機

server{
     listen 80;
     server_name 10.0.0.xxx; #或者是訪問的域名,我這用的ip地址來訪問的
     location / {
          root /data/awstats/web/;
          index index.html index.htm;
          access_log logs/access.log;
     }
     location ~* ^/cgi-bin/.*\.pl$ {
          include awstats.conf; #此處我是直接加載一個配置文件來處理的,具體配置見下面
     }
     location ~ ^/icon/{
          root /usr/local/awstats/wwwroot;
          index index.html;
          charset gb2312;
     }
}

awsstats.conf 存放路徑是:nginx的配置文件目錄conf下,和nginx.conf同級目錄

fastcgi_pass 127.0.0.1:8999;
fastcgi_index awstats.pl;
fastcgi_param SCRIPT_FILENAME /usr/local/awstats/wwwroot/cgi-bin/awstats.pl;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_read_timeout 60;

7、自動處理日誌文件

每天會在凌晨將昨天日誌取過來後處理完畢存放在配置文件設置的目錄中,然後執行

/usr/local/awstats/tools/awstats_updateall.pl now

或者:

/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.
xxxx.com


將這個處理的腳本放入crontab中

wKioL1gik6KB1o6ZAAAv7c9cMg8728.jpg-wh_50


我這的處理腳本如下:僅供參考

wKiom1gik67iDo1HAAO0iMrzuHs208.jpg-wh_50

8、訪問

可以在瀏覽器輸入你在nginx配置的IP或者域名來訪問

http://youdomain/cgi-bin/awstats.pl?config=www.xxx.com,多個不同域名就修改地址欄的config段


wKioL1gik7-gkWy2AAO2O2y-5sI907.jpg-wh_50

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