最近一直折腾在Linode新租的VPS,以CentOS+Lnmp+Awstats的组合做为网站的基础平台。CentOS和Lnmp安装很顺利,参考了网上的一些资料2小时搞定,但在调试Awstats的时候遇到很多问题,在解决这些疑难杂症的时候也学到了不少关于CentOS的知识。
这里主要是转载一篇文章《使用awstats自动分析Nginx日志》,网上关于Awstats分析Nginx日志生成网站流量统计的文章很多,但大多都是转自IBM官网的《使用 awstats 分析 Nginx 的访问日志》,这篇文档是08年的,之后也没有修订过,里面好多细节没有说清楚,对于初学Linux的新手很难实现。
Awstats的介绍和功能我就直接忽略了,请看下面Awstats分析Nginx日志生成网站流量统计的详细操作步骤。
日志切割
本文主要介绍通过让 awstats 对日志统计的结果生成静态页面,然后通过 Nginx 输出以达到统计 Nginx 访问日志的效果,其中还包括如何让 Nginx 自动切割日志文件。对于nginx的日志,我的做法是按天切割。然后存入日期形式的目录中并压缩。
需要注意的是,nginx的日志应该遵循以下格式,才可以被awstats识别,如定义日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
使用日志格式
access_log /home/www/logs/access.log main;
本文不讲如何安装nginx,稍后我将发布我的lnmp一键安装包(linux nginx mysql php)。全编译+优化自动化安装,使用php-fpm运行php的fastcgi进程。
我写了一个定时切割日志的脚本。每天0:00开始执行,切割昨天的日志(交由awstats分析),压缩前天的日志(压缩日志可减小存储空间,为防止awstats没有分析完就被压缩,所以只压缩前天的日志)。如果你的nginx和log文件放的路径和我的不一样,请对应修改。
vim cut_log.sh
输入以下内容
#!/bin/bash # This script run at 00:00 # cut yesterday log and gzip the day before yesterday log files. # yesterday logs to awstats # The Nginx logs path logs_path="/home/www/logs/" date_dir=${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/$(date -d "yesterday" +"%d")/ gzip_date_dir=${logs_path}$(date -d "-2 day" +"%Y")/$(date -d "-2 day" +"%m")/$(date -d "-2 day" +"%d")/ mkdir -p $date_dir mv ${logs_path}*access.log $date_dir /usr/local/nginx/sbin/nginx -s reopen /usr/bin/gzip ${gzip_date_dir}*.log
然后让它每天0时起开始进行,执行crontab -e加入以下代码再按:wq保存退出,这里我将此脚本放在/root/下,切记要给它可执行权限(chmod +x cut_log.sh).
00 00 * * * /bin/bash /root/cut_log.sh
这样就可以每天凌里自动切割昨天的日志到以日期为目录结构的目录中。可以留存以后查询。留着昨天的日志交给下面的awstats来分析,压缩前天的日志(前天的已经被分析过了)。
安装和配置awstats
下载最新的 awstats,我使用的是迄今为止最新的7.0版本
安装到/usr/local下,这个路径是习惯。大部分人保持的良好习惯。
wget http://awstats.sourceforge.net/files/awstats-7.0.tar.gz tar -zxvf awstats-7.0.tar.gz mv awstats-7.0 /usr/local/awstats
修改权限,wget下载下来的包中权限是非root的,赋予过权限之后,.pl的文件也就可以运行了。
chown -R root:root /usr/local/awstats chmod -R =rwX /usr/local/awstats chmod +x /usr/local/awstats/tools/*.pl chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl
然后执行 tools 目录中的 awstats_configure.pl 配置向导,创建一个新的统计
运行(注意这里要在当前目录运行。否则会有一些关于标准目录的提示。)
cd /usr/local/awstats/tools ./awstats_configure.pl
将会有如下一些提示:
-----> 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 #这里添none并回车,因为我们没有使用apache
回车之后下一个选项
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: >akii.org #这里输入你要分析的域名,或是随便一个你易记的配置名并回车
接下来要定义你的配置文件存放的路径,可用默认
-----> 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
回车后的提示
-----> Create config file '/etc/awstats/awstats.akii.org.conf' Config file /etc/awstats/awstats.akii.org.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=akii.org 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.akii.org.conf You should have a look inside to check and change manually main parameters. You can then manually update your statistics for 'yuyuanchun.com' with command: > perl awstats.pl -update -config=akii.org You can also build static report pages for 'akii.org' with command: > perl awstats.pl -output=pagetype -config=akii.org Press ENTER to finish... #回车完成配置文件的创建
完成配置文件的创建后,我们还要修改一下。因为我们是按天切割的日志,切割完成后交由awstats去分析。并不是让awstats去分时正在时时增长的也就是正在被写入的日志,这样的好处是不至于遗漏数据,并且分析已经切割完成的日志,更不用担心会有冲突。坏处是我一天切割一次日志,你要等第二天才能看昨天的一些详细数据。
修改/etc/awstats/awstats.akii.org.conf,执行:
vi /etc/awstats/awstats.akii.org.conf
找到
LogFile="/var/log/httpd/mylog.log"
修改为:
LogFile="/home/www/logs/%YYYY-24/%MM-24/%DD-24/akii.org_access.log"
如果你的日志路径和我的不一样,请修改成对应的日志文件名。以上的完整路径是切割后保存的nginx日志文件。其中%YYYY-24/%MM-24/%DD-24表示年月日都减去24小时,也就是昨天的日志目录。修改完成后按:wq保存退出。
接下来可以测试一下awstats分析日志了(前提是你已经有了切割过的日志,没有的话可以先退行一下切割日志的脚本/root/cut_log.sh)
首先,还要创建一个awstats用于记录数据的目录
mkdir -p /var/lib/awstats
然后运行awstats的wwwroot目录中的awatsts.pl来测试一下
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=akii.org
你如果看到类似下面的提示就说明配置文件都正确了。
Create/Update database for config "/etc/awstats/awstats.akii.org.conf" by AWStats version 7.0 (build 1.964) From data in log file "/home/www/logs/2010/07/24/akii.org_access.log"... Phase 1 : First bypass old records, searching new record... Direct access after last parsed record (after line 43260) Jumped lines in file: 43260 Found 43260 already parsed records. Parsed lines in file: 0 Found 0 dropped records, Found 0 comments, Found 0 blank records, Found 0 corrupted records, Found 0 old records, Found 0 new qualified records
统计分析完成后,结果还在 Awstats 的数据库中。在 Apache 上,可以直接打开 Perl 程序的网页查看统计。 但本文开始时已经提到,Nginx 对 Perl 支持并不好,所以我们要换个方法,利用 awstats 的工具将统计的结果生成静态文件,具体的步骤如下:
* 首先在 webroot 目录下创建一个文件夹。例:/home/www/awstats
* 写一个脚本,定期执行让 Awstats 把静态页面生成到该目录中
先生成存放awstats生成的静态文件的目录,我这里用的是/home/www/awstats
mkdir -p /home/www/awstats
我们来写一个脚本
vim /root/awstats.sh
然后输入以下内容
#!/bin/bash mkdir -p /home/www/awstats/akii.org /usr/local/awstats/tools/awstats_buildstaticpages.pl -update \ -config=akii.org -lang=cn -dir=/home/www/awstats/akii.org \ -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
上述命令的具体意思如下:
* /usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats 静态页面生成工具
* -update -config=akii.org 更新配置项
* -lang=cn 语言为中文
* -dir=/home/www/awstats 统计结果输出目录
* -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路径。
然后在你的nginx的配置文件中,在你想要安置awstats或默认的ip或域名的server段中,加入关于awstats和icon的两个目录配置。
如一个完整案例:
server { listen 80; server_name localhost; root /home/www; index index.html; location ~ ^/awstats/ { # awstats 静态页面目录 root /home/www/awstats; autoindex on; #可以目录浏览你的多个域名的目录用于分析 index index.html; access_log off; } location ~ ^/icon/ { # 图标目录 root /usr/local/awstats/wwwroot; index index.html; access_log off; } }
接下来可以测试一下脚本是否可以正确执行
还是别忘了给它可执行权限
chmod +x /root/awstats.sh /root/awstats.sh
如果你看到它生成了一堆网页,那就说明成功了。
输出信息部分例如
Launch update process : "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=akii.org -update -configdir= ...... Build keywords page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=akii.org -staticlinks -lang=cn -output=keywords Build errors404 page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=akii.org -staticlinks -lang=cn -output=errors404 20 files built. Main HTML page is 'awstats.akii.org.html'.
然后可以把它加入自动运行了。
配置awstats脚本自动运行
crontab -e
加入
00 1 * * * /root/awstats.sh
然后保存退出。
这样就可以每天在凌晨自动分割日志,并且开始自动用awstats分析nginx的日志了。
“Awstats分析Nginx日志生成网站流量统计”上的10条回复
没研究过这方面的
这个比统计网站统计的信息更完整,cpanel下就包含了这个统计。
谷歌的流量统计不错的啊,我一般都是用那个的。
技术贴啊 呵呵看不懂啊 点点广告支持你了啊
真实在 😉
链接已加:)。你的博客,我喜欢。
非常不错!
准备国庆试验!
希望能成功!
http://www.lifesand.com
哈哈。。。
学会咯 很开心
太多代码了,看不懂呢!