tile 发表于 2013-7-10 09:46:27

awstats实现按日、按月统计

昨天产品提出要求,需要查看每天的uv数。awstats默认是按月统计的,所以无法查看uv数。下面介绍一下nginx做awstats的web容器的实现方法,有些是网上搜集的,有些是自己加进去的,希望能帮到有需求的朋友。一、Awstats安装配置详细安装步骤这里不再赘述,如有需要请参考博客另一篇文章:awstats+jawstats构建日志自动分析系统1、安装# cd /opt# wget http://sourceforge.net/projects/ ... wstats-7.1.1.tar.gz# tar -zxf awstats-7.1.1.tar.gz# mv awstats-7.1.1 /usr/local/awstats# chown -R root:root /usr/local/awstats# chmod +x /usr/local/awstats/tools/*.pl# chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl# cd /usr/local/awstats/tools# ./awstats_configure.pl
1.输入 none 然后回车2.输入 y 确认创建配置文件3.输入配置文件名称,一般输入域名,这里是以slog.fity.cn为案例4.配置文件使用默认路径 /etc/awstats5.按回车继续6.按回车完成配置文件的创建2、修改awstats配置文件# vi /etc/awstats/awstats.slog.fity.cn.conf#如果有多个站点需要统计,请配置多个配置文件LogFile="/htdoc/logs/2013/05/access_20130526.log"//指定log文件的存储位置
如果你希望每天能定时生成昨天的报表,可以:LogFile="/htdoc/logs/%YYYY-24/%MM-24%/access_%YYYY-24%MM-24%DD-24.log"
注:根据你的日志路径和对应的日志文件名进行修改。对应Nginx日志切割程序所生成的目录存放结构保存的nginx日志文件。要注意Awstats的年月日格式的跟Nginx的写法有所不同,其中%YYYY-24/%MM-24/%DD-24表示年月日都减去24小时,也就是昨天的日志目录。3、生成Awstats统计结果静态页面# mkdir -p /htdoc/awstats/html#创建存放结果的目录
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update-config=slog.fity.cn -lang=cn -dir=/htdoc/awstats/html -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl说明:/usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats 静态页面生成工具-update -config=slog.fity.cn 更新配置项,指定配置文件-lang=cn 语言为中文-dir=指定静态页面的存储位置-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路径
二、Nginx配置及URL地址简化1、配置Nginx从Awstats提供的工具包中我们看到Awstats已经提供了在Nginx环境Awstats的安装配置,Nginx主机范例文件:#ls /usr/local/awstats/tools/nginx/awstats-fcgi.phpawstats-nginx.confREADME.txt
这里我们先把awstats-fcgi.PHP复制一份到/usr/local/awstats/wwwroot/cgi-bin/fcgi.php# cd /usr/local/awstats/tools/nginx/# cp -a awstats-fcgi.php /usr/local/awstats/wwwroot/cgi-bin/fcgi.php
然后创建Nginx虚拟主机配置文件(可以参考Awstats提供的示例文件awstats-nginx.conf)这里未来往事给出一个本次测试环境下Nginx的vhost完整配置:server {listen 80;server_name slog.fity.cn;#access_log /var/log/nginx/localhost.access_log main;#error_log /var/log/nginx/localhost.error_log info;root /htdoc/awstats/html;index index.html;
# Restrict access#auth_basic "Restricted";#auth_basic_user_file /etc/awstats/htpasswd;

location ~ ^/cgi-bin/.*.(cgi|pl|py|rb) {gzip off;includefastcgi_params;fastcgi_pass127.0.0.1:9000;#注意配置fastcgi_pass为你的php-fpm server.fastcgi_indexcgi-bin.php;fastcgi_paramSCRIPT_FILENAME /usr/local/awstats/wwwroot/cgi-bin/fcgi.php;#注意文件路径fastcgi_paramSCRIPT_NAME/cgi-bin/fcgi.php;fastcgi_paramX_SCRIPT_FILENAME /usr/local/awstats/wwwroot$fastcgi_script_name;#注意文件路径fastcgi_paramX_SCRIPT_NAME $fastcgi_script_name;fastcgi_paramREMOTE_USER$remote_user;}

# Static awstats files: HTML files stored in DOCUMENT_ROOT/awstats/location /classes/ {alias /usr/local/awstats/wwwroot/classes/;}
location /css/ {alias /usr/local/awstats/wwwroot/css/;}
location /js/ {alias /usr/local/awstats/wwwroot/js/;}location /icon/ {alias /usr/local/awstats/wwwroot/icon/;}
}
2、常见错误:页面错误信息:File not found. 检查nginx调用的文件是否有读取权限,例如/usr/local/awstats/wwwroot/下的相关文件Nginx是否有权限读取。未来往事遇到该故障是由于Nginx没有读取/usr/local/awstats/wwwroot/cgi-bin/fcgi.php文件的权限。页面错误信息:Page not found. 检查Nginx配置文件中是否有参数错误。备注:为了避免以上两处错误,未来往事建议你把该目录下/usr/local/awstats/wwwroot/使用到的文件复制一份到你Nginx中配置的主机目录中,然后适当的修改下Nginx配置文件中的路径地址即可。3、状态测试至此,我们便可以通过如下url方式访问日志统计页面了:http://slog.fity.cn/cgi-bin/awstats.pl?config=slog.fity.cn4、简化URL地址原始地址:http://slog.fity.cn/cgi-bin/awstats.pl?config=slog.fity.cn
简化后的地址:http://slog.fity.cn/slog.fity.cnrewrite规则:location ~ ^/(+)$ {return 301 $scheme://slog.fity.cn/cgi-bin/awstats.pl?config=$1;}
5、利用密码保护统计数据#htpasswd -c /etc/awstats/awstats.alpha.com.htpasswd username
参考:《HttpAuthBasicModule》# Protect each config with a different credentialif ($args ~ "config=(+)") {set $domain $1;}
auth_basic"Admin";auth_basic_user_file/etc/awstats/awstats.$domain.htpasswd;
gzip off;includefastcgi_params;fastcgi_pass127.0.0.1:9000;#注意配置fastcgi_pass为你的php-fpm server.fastcgi_indexcgi-bin.php;fastcgi_paramSCRIPT_FILENAME /usr/local/awstats/wwwroot/cgi-bin/fcgi.php;#注意文件路径fastcgi_paramSCRIPT_NAME/cgi-bin/fcgi.php;fastcgi_paramX_SCRIPT_FILENAME /usr/local/awstats/wwwroot$fastcgi_script_name;#注意文件路径fastcgi_paramX_SCRIPT_NAME $fastcgi_script_name;fastcgi_paramREMOTE_USER$remote_user;}
6、最终的配置文件如下:server {listen 80;server_name slog.fity.cn;root /htdoc/awstats/html;index index.html;
error_log /var/log/nginx/awstats.error.log;access_log off;log_not_found off;
location ~ ^/(+)$ {return 301 $scheme://slog.fity.cn/<cgi-></cgi->bin/awstats.pl?config=$1;}
location ~ ^/cgi-bin/.*.(cgi|pl|py|rb) {if ($args ~ "config=(+)") {set $domain $1;}
auth_basic"Admin";auth_basic_user_file/etc/awstats/awstats.$domain.htpasswd;
gzip off;includefastcgi_params;fastcgi_pass127.0.0.1:9000;#注意配置fastcgi_pass为你的php-fpm server.fastcgi_indexcgi-bin.php;fastcgi_paramSCRIPT_FILENAME /usr/local/awstats/wwwroot/cgi-bin/fcgi.php;#注意文件路径fastcgi_paramSCRIPT_NAME/cgi-bin/fcgi.php;fastcgi_paramX_SCRIPT_FILENAME /usr/local/awstats/wwwroot$fastcgi_script_name;#注意文件路径fastcgi_paramX_SCRIPT_NAME $fastcgi_script_name;fastcgi_paramREMOTE_USER$remote_user;}
location /classes/ {alias /usr/local/awstats/wwwroot/classes/;}
location /css/ {alias /usr/local/awstats/wwwroot/css/;}
location /js/ {alias /usr/local/awstats/wwwroot/js/;}location /icon/ {alias /usr/local/awstats/wwwroot/icon/;}
}
三、配置Awstats按日、按月归档日志统计结果这里需要用到2个JS文件,见附件!安装/使用:下载完成并解压,可以看到如下三个文件:day-by-day-head.jsday-by-day-end.jsreadme.txt
把day-by-day-head.js、day-by-day-end.js复制到站点/usr/local/awstats/wwwroot/js目录下,如果没有没有js目录可自行建立(当然这里的路径你需要修改为你的nginx虚拟主机路径)修改Awstats配置文件(/etc/awstats/*)添加:HTMLHeadSection="<script language=javascript src="/js/day-by-day-head.js"></script>"HTMLEndSection="<script language=javascript src="/js/day-by-day-end.js"></script>"
更新日志数据时记得加上-databasebreak参数/usr/local/awstats/tools/awstats_buildstaticpages.pl-update-config=slog.fity.cn -databasebreak=day -lang=cn -dir=/htdoc/awstats/html -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl/usr/local/awstats/tools/awstats_buildstaticpages.pl-update-config=slog.fity.cn -databasebreak=month -lang=cn -dir=/htdoc/awstats/html -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
至此,我们便可以通过如下url方式访问按年月日归档的日志统计页面了:http://slog.fity.cn/slog.fity.cn(如果你在Nginx配置文件中没有添加URL重写规则,请通过类似地址访问http://slog.fity.cn/cgi-bin/awstats.pl?config=slog.fity.cn)最终效果如图:

96818 发表于 2013-7-11 10:58:14

看尽天下A片,心中自然无码~

hao1nan 发表于 2013-7-15 17:55:20

我真想亲口管你爷爷叫声:爹!

hege 发表于 2013-7-20 04:42:10

我在马路边丢了一分钱

黄智勇 发表于 2013-7-24 02:46:19

勿以坑小而不灌,勿以坑大而灌之。

期盼死亡的小丑 发表于 2013-7-28 15:00:14

读书读到抽筋处,文思方能如尿崩!

qazxsw1 发表于 2013-8-2 21:57:00

我不在江湖,但江湖中有我的传说。

jaydozhou 发表于 2013-10-10 09:56:38

求附件啊啊啊啊啊

格格 发表于 2018-6-14 18:24:43

1

iyunv.com.cn 发表于 2018-6-15 09:27:55

111
页: [1]
查看完整版本: awstats实现按日、按月统计