日志分析:用AWStats分析Nginx的访问日志,它可以统计您站点的如


Awstats是一个免费非常简洁而且强大有个性的统计工具。它可以统计您站点的如下信息:
一:访问量,访问次数,页面浏览量,点击数,数据流量等
二:精确到每月、每日、每小时的数据
三:访问者国家
四:访问者ip
五:Robots/Spiders的统计
六:纺客持续时间
七:对不同Files type 的统计信息
八:Pages-URL的统计
九:访客操作系统浏览器等信息
十:其它信息(搜索关键字等等)

本文主要撰写如何用AWStats分析Nginx的访问日志,具体过程如下:

网站环境:lnmp [Linux+Nginx+PHP FastCGI模式+mysql (CentOS-5.6)]
准备工作:下载awstats最新软件包,perl环境准备好,perl –version 查看!

1. nginx日志分割,创建日志分割脚本,用crontab定时执行此脚本。
# vim /wslogs/zhiliyou.com/logcron.sh

#!/bin/bash
log_dir="/wslogs/linuxboy.net/log_dir"
time=`date +%Y%m%d`
mv ${log_dir}/linuxboy_www_access.log /wslogs/linuxboy_www_access_$time.log
/usr/local/webserver/nginx/sbin/nginx -s reload

2. 配置awstats分析的日志文件
# mkdir /etc/awstats  ///存放awstats的配置文件

将下载的软件包放在/usr/local/下,解压
# tar zxvf awstats-7.0.tar.gz
# mv awstats-7.0/ awstats
# cp awstats/wwwroot/cgi-bin/awstats.model.conf /etc/awstats  ///复制该文件到/etc/awstats目录下
# awstats/tools/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: http://www.mysite.com/
Example: demo
Your web site, virtual server or profile name:
#> 统计网站的域名
-回车-
-----> 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=www.moabc.net
#回头把该命令填入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.http://www.moabc.net.conf/
#新配置文件所在的路径
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.moabc.net' with command:
> perl awstats.pl -update -config=www.moabc.net
You can also build static report pages for 'www.moabc.net' with command:
> perl awstats.pl -output=pagetype -config=www.moabc.net

Press ENTER to finish...

3. 修改日志路径
# vim /etc/awstats/生成的配置文件

LogFile=”/wslogs/linuxboy_www_access_%YYYY-0%MM-0%dd-0.log”
DirData="/var/lib/awstats"        ///统计的数据文件存放位置
DirIcons="/tools/icon"       ///指定图标目录,图片目录一般在解压的安装包里

# cp –R awstats/wwwroot/icon /wsdata/linuxboy/toos/    ///复制图标素材到指定目录
# mkdir /var/lib/awstats      ///创建存放awstats数据目录

4.分析日志,执行一下日志分割脚本
# /wslogs/logcron.sh
# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.linuxboy.net
执行过后,会在/var/lib/awstats下生成.TXT的数据文件!

5. 生成html静态页面方便查看
#/usr/local/awstats/tools/awstats_buildstaticpages.pl \
-update -config=www.linuxboy.net -lang=cn -dir=/wsdata/linuxboy/tools/awstats \
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

上述命令参数解释:
* /usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats    静态页面生成工具
* -update -config=www.linuxboy.net   更新配置项
* -lang=cn   语言为中文
* -dir=/wsdata/www/tools/awstats    统计结果输出目录
* -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats   日志更新程序路径。

6. 给tools/awstats/目录设置访问权限
# mkdir /usr/local/webserver/nginx/httpwd
# htpasswd -c /usr/local/webserver/nginx/httpwd/awstats webadmin
New password: (此处输入你的密码)
Re-type new password: (再次输入你的密码)
Adding password for user

Nginx.conf配置文件配置:

location ~ ^/tools/awstats/ {
        access_log off;
        error_log off;
        charset gb2312;
        auth_basic "Linuxde-AWStats login";
        auth_basic_user_file /usr/local/webserver/nginx/httpwd/awstats;
}

7. 配置完成,浏览器输入以下格式的地址:
http://www.linuxboy.net/tools/awstats/awstats.www.linuxboy.net.html

相关内容