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

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