awstats 结合 nginx日志进行网站统计分析

1. 安装FCGI
    # wget http://search.cpan.org/CPAN/authors/id/B/BO/BOBTFISH/FCGI-0.70.tar.gz  
    # tar zxvf FCGI-0.70.tar.gz  
    # cd FCGI-0.70  
    # perl Makefile.PL  
    # make  
    # make install  

2. 安装 IO 和 IO::ALL模块


    # wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz  
    # tar zxvf IO-1.25.tar.gz  
    # cd IO-1.25  
    # perl Makefile.PL  
    # make  
    # make install  
    # wget http://search.cpan.org/CPAN/authors/id/I/IN/INGY/IO-All-0.41.tar.gz  
    # tar zxvf IO-All-0.41.tar.gz  
    # cd IO-All  
    # perl Makefile.PL  
    # make  
    # make install  

3. 准备Perl脚本

    脚本见附件:perl-fcgi.pl
    把这个脚本放在一个好管理的地方并授予执行权限,如下:
    # chmod 755 /usr/local/nginx/perl-fcgi.pl

4. cgi启动/停止脚本 (其中,nobody为nginx的运行用户,需要改为与nginx运行一致的用户)

    脚本见附件:start_perl_cgi.sh
    注意事项:
      1)不能用 root 用户执行(会提示). 要用与 Nginx 相同身份的用户执行。否则可能会在     Nginx Log 中提示 Permision Denied
      2)修改里面的dir路径以及存放perl-fcgi.pl的路径
    # vi /usr/local/nginx/start_perl_cgi.sh
    # chmod 755 /usr/local/nginx/start_perl_cgi.sh
    注意:上面脚本中的nobody要替换为与nginx相同的用户与组, 才能保证正常执行
    # 启动脚本
    # /usr/local/nginx/start_perl_cgi.sh start
    正常情况下在/usr/local/nginx/logs 下生成 perl-fcgi.sock 这个文件,如果没有生成,那就要检查下上面的步聚了.

5. 配置nginx.conf文件

   将附件nginx.conf中的配置代码拷贝到你的nginx.conf中,并重启nginx(注意后续需要修改路径为awstats的cgi-bin路径)

6. perl探针测试

   将附件perlinfo.cgi拷贝到第5步nginx.conf配置中对应的目录下,然后授权perlinfo.cgi可执行,通过浏览器访问,如果出现一些表格,则表示配置成功;

7. 安装awstats (注意选一个你好管理的目录进行安装)

   # wget http://prdownloads.sourceforge.net/awstats/awstats-7.0.tar.gz
   安装时,会有一些交互:
   1) Config file path ('none' to skip web server setup): 填写none 回车,跳过
   2) Do you want me to build a new AWStats config/profilefile (required if first install) [y/N] ? #y 创建一个新的统计配置
   3) Your web site, virtual server or profile name:
       #> www.mibuo.com    #可填你的任意的统计网站的域名
  
   后面的步骤都用回车直至完成;

8. 安装完毕后,修改统计站点配置文件(awstats安装默认会在/etc/awstats/下生成配置文件,以awstats.安装时你填写的域名.conf命名)

   #vi /etc/awstats/awstats.www.mibuo.com.conf
   1) 修改:LogFile="/var/log/httpd/mylog.log" 修改为你要分析的nginx日志存放的地方;
   比如我的:LogFile="/app/nginx/logs/access_%YYYY%MM%DD.log"  日志格式为:access_20111020.log,可按日切割存放;
   2) 修改:LogFormat = 1 (默认)修改为:LogFormat = "%host - %time1 %methodurl %code %bytesd %refererquot %uaquot"
       注意:=号后面的参数必须与日志记录一一对应的,可参考LogFormat上面注释的参数说明,比如我的日志:
192.168.**.** - [18/Oct/2011:19:44:29 +0800] "POST /user/login HTTP/1.1" 500 18392 "http://192.168.***.***/user/login" "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1" -

     那么对应的第一个参数%host对应192.168.**.**,第二个参数横杠"-"对应横杠"-",第三个参数%time1对应[18/Oct/2011:19:44:29 +0800],依次类推(可参考LogFormat上面注释的参数说明)

9. 执行awstats日志分析脚本(这个要配一个定时器,可每天更新一次,这样就可以按天、按月来查看统计数据了)

/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=www.mibuo.com -lang=cn -dir=/data/admin_web/awstats -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

脚本解释:
   -config=www.mibuo.com (后面为你统计分析的域名)
   -lang=cn(cn代表中文)
   -dir=/data/admin_web/awstats (后面为你存放awstats生成页面的目录)
   -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl(后面为awstats.pl脚本存放地方,默认为awstats安装目录下的webroot/cgi-bin下)

10.启动nginx,启动start_perl_cgi.sh(start_perl_cgi.sh start)脚本,执行日志分析更新(第九步),访问http://192.168.**.***/awstats.pl?config=www.mibuo.com即可看到统计分析的界面(界面见附件中的awstats.png);大功告成


 原文转载:
http://it.5yun.com.cn/html/y2015/m07/126.html

猜你喜欢

转载自senon.iteye.com/blog/1202835