awstats统计分析nginx日志

php中文网
发布: 2016-06-07 15:30:30
原创
1625人浏览过

#修改nginx日志格式: log_format awstats '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/live.yofogo.com.access.log awstats; #

#修改nginx日志格式:
  log_format awstats '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
  access_log  logs/live.yofogo.com.access.log  awstats;

#nginx切割日志脚本logcron.sh:
  mv  /opt/nginx/nginx-1.2.1/logs/live.yofogo.com.access.log /opt/nginx/nginx-1.2.1/logs/live.yofogo.com.access_`date +%y%m%d`.log
  killall -s usr1 nginx #使用usr1参数通知nginx进程切换日志文件

#安装awstats
  wget http://prdownloads.sourceforge.net/awstats/awstats-7.0.tar.gz
  tar xzf awstats-7.0.tar.gz
  mv awstats-7.0 /usr/local/awstats
  cd /usr/local/awstats/tools
     chmod +x /usr/local/awstats/tools/*.pl
     chmod +x wwwroot/cgi-bin/*.pl
  mkdir /etc/awstats /var/lib/awstats

#awstats配置
  cd /usr/local/awstats/tools
  perl awstats_configure.pl:
    -----> 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  回车#因为这里用的是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    回车#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:
    #> live.yofogo.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):
    #> 回车

    ----> 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=live.yofogo.com  
        #回头把该命令填入crontab 按指定时间执行
    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.live.yofogo.com.conf  
        #新配置文件所在的路径
    you should have a look inside to check and change manually main parameters.
    you can then manually update your statistics for 'live.yofogo.com' with command:
    > perl awstats.pl -update -config=live.yofogo.com
    you can also build static report pages for 'live.yofogo.com' with command:
    > perl awstats.pl -output=pagetype -config=live.yofogo.com
    press enter to finish...回车

  #编辑awstats配置文件:sudo vim /etc/awstats/awstats.live.yofogo.com.conf
          logfile="/opt/nginx/nginx-1.2.1/logs/live.yofogo.com.access_%yyyy-0%mm-0%dd-0.log" //统计的日志文件的路径
          logformat=1//日志格式,这个一定要跟要分析的日志格式匹配,不然可能分析不出来数据或者分析出来的数据不正确
          logseparator=" "//指定日志的分割符
          sitedomain="www.lme.pw"//要分析的网站域名
          dirdata="/opt/awstats"//分析完的数据文件存在的位置
          lang为lang="cn"
          #对应nginx日志切割所生成的目录存放结构,要注意awstats的年月日格式的跟nginx的写法不同。现在执行统计的顺序是:
      nginx 产生日志 –> 日志切割 –> nginx 继续产生日志 –> 另存切割日志 –> 交由awstats统计 –> 生成结果

#awstats静态文件统计资源配置
  mkdir -p /home/works/yofoto/awstats-web/awstats
  #cp -r /usr/local/awstats/wwwroot/css /home/works/yofoto/awstats-web
  #cp -r /usr/local/awstats/wwwroot/icon /home/works/yofoto/awstats-web
  #nginx映射:
    location ~ ^/awstats/ {# html 静态页面目录
        root   /home/works/yofoto/awstats-web;
        index  index.html;
        access_log off;
        error_log off;
        charset gb2312; #最好把默认编码改成 gb2312避免浏览器因自动编码出现乱码的情况
    }

    location ~ ^/icon/ {# 图标目录
        root   /usr/local/awstats/wwwroot;
        index  index.html;
        access_log off;
        error_log off;
        charset gb2312;
    }
  #更新日志统计数据库及生成静态文件到指定目录
    /usr/local/awstats/tools/awstats_buildstaticpages.pl -update  \
  -config=live.yofogo.com -lang=cn -dir=/home/works/yofoto/awstats-web/awstats  \
  -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

  #上述命令的具体意思如下:
    /usr/local/awstats/tools/awstats_buildstaticpages.pl awstats 静态页面生成工具
    -update -config=live.yofogo.com 更新配置项
    -lang=cn 语言为中文
    -dir=/home/works/yofoto/awstats-web/awstats 统计结果输出目录
    -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl awstats日志更新程序路径

#浏览器查看统计结果 http://youhostname/awstats/awstats.live.yofogo.com.html

#配置awstats自动日志统计-设置crontab计划任务:
  sudo vim /etc/crontab
    #下午15:00进行日志切割
    15 00 * * * /opt/nginx/nginx-1.2.1/sbin/logcron.sh
    #下午15:20进行日志分析
    15 20 * * * /usr/local/awstats/tools/awstats_buildstaticpages.pl -update  \
     -config=live.yofogo.com -lang=cn -dir=/home/works/yofoto/awstats-web/awstats  \
     -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl    
  #crontab /etc/crontab    指定cron所执行的配置档路径

#保护日志统计结果页面

     用htpasswd生成密钥,

     location ~ ^/awstats/ {# html 静态页面目录
         root /home/works/yofoto/awstats-web;
         index index.html;
         access_log off;
         error_log off;
         charset gb2312;
         auth_basic "admin";
         auth_basic_user_file /home/works/yofoto/awstats-web/passwd.pass;
     }

检测配置/nginx/sbin/nginx -t -c /nginx/conf/nginx.conf<br />执行命令killall &ndash;s HUP nginx 让 Nginx重新加载配置即可<br />清除80端口的进程: lsof -i :80|grep -v "PID"|awk '{print "kill -9",$2}'|sh
登录后复制

参考:
  http://www.ibm.com/developerworks/cn/linux/l-cn-awstats-nginx
  http://www.linuxyan.com/web-server/102.html
  http://blog.rebill.info/archives/statistical-analysis-of-user-behavior-system-architecture-design.html

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号