AWStats是一款免费的功能强大的工具,它以图形方式生成高级web、流、FTP或邮件服务器统计信息。可以以CGI或从命令行方式运行,并在几个图形网页中显示日志包含的所有可能信息。
本文主要介绍centos6.9下安装、配置awstats,并统计nginx访问日志
[root@web data]# vim /usr/local/nginx/conf/nginx.conf
log_format access ‘$remote_addr -$remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent"$http_referer" ‘
‘"$http_user_agent" $http_x_forwarded_for‘;
access_log log/access.log access;
如果使用了网站使用了CDN或者代理, 则格式应该有所调整,以便第一个参数为真实客户端的地址:
[root@web data]# vim /usr/local/nginx/conf/nginx.conf
log_format access ‘$http_x_forwarded_for -$remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent"$http_referer" ‘
‘"$http_user_agent" $remote_addr‘;
access_log log/access.log access;
nginx的日志不能像apache一样去用cronolog工具进行切割,这里我们就写一个脚本,让它可以在每天00:00自动执行,切割昨天的日志(交由awstats分析),并压缩前天的日志(压缩日志可减小存储空间)。
[root@web data]# vim /data/scripts/cut_nginx_log.sh
#!/bin/bash
# Settings
logs_path="/data/wwwlogs"
back_path="/data/backup/wwwlogs"
log_file=access.log
date_dir=${back_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/$(date -d "yesterday" +"%d")/
gzip_date_dir=${back_path}/$(date -d "-2 day" +"%Y")/$(date -d "-2 day" +"%m")/$(date -d "-2 day" +"%d")/
# Settings End
mkdir -p $date_dir
[ -d $logs_path ] && cd $logs_path || exit 1
[ -f $log_file ] && /bin/mv $log_file $date_dir ||exit 1
[ $? -eq 0 ] && /usr/local/nginx/sbin/nginx -s reopen
[ -f ${gzip_date_dir}*.log ] && /usr/bin/gzip ${gzip_date_dir}*.log || exit 1
执行crontab -e 配置00:00进行切割即可
cd /usr/local/src
wget http://prdownloads.sourceforge.net/awstats/awstats-7.6.tar.gz
tar xvf awstats-7.6.tar.gz
mv awstats-7.6 /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
生成的配置文件,默认存储在/etc/awstats下, 如下为生产步骤:
[root@web local]# cd /usr/local/awstats/tools
[root@web tools]# ./awstats_configure.pl
......
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 #新建配置文件
-----> 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:
> log.test.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):
> #直接回车
-----> Create config file ‘/etc/awstats/awstats.log.test.com.conf‘
Config file /etc/awstats/awstats.log.test.com.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=log.test.com
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...
以下直接回车即可
默认会在/etc/awstats目录下生成一个/etc/awstats/awstats.test.com.conf的配置文件
vim /etc/awstats/awstats.test.com.conf
LogFile="/data/backup/wwwlogs/%YYYY-24/%MM-24/%DD-24/zhibo.log"
...
DirData="/data/awstats"
LogFile是对应上面Nginx日志切割所生成的目录存放位置,注意awstats的年月日格式,-24表示昨天的日志。此处年月日都需要-24, 否则跨年或跨月时会报错。
DirData指数据文件保存目录, 这个文件需要真实存在,否则会报错
分析的执行顺序是:
Nginx 产生日志 –> 日志切割 –> Nginx 继续产生日志 –> 另存切割日志 –> 交由Awstats统计 –> 生成结果
创建记录数据的目录:mkdir -p /data/awstats
生成静态文件/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=log.test.com -lang=cn -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -dir=/data/awstats/
参数说明:
/usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats静态页面生成工具
-update -config=log.test.com 更新配置项
-lang=cn 语言为中文
-dir=/data/awstats 统计结果输出目录
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路径
vim /usr/local/nginx/conf/vhost/awstats.conf
#cat /usr/local/nginx/conf/vhosts/awstats.conf
server {
listen 8080;
server_name logs.yourdomain;
access_log /home/logs/awstats_access.log;
root /data/awstats; #html存放目录
index index.html;
location /awstats/classes/ {
alias /usr/local/awstats/wwwroot/classes/;
}
location /awstats/css/ {
alias /usr/local/awstats/wwwroot/css/;
}
location /awstats/icon/ {
alias /usr/local/awstats/wwwroot/icon/;
}
location /awstats-icon/ {
alias /usr/local/awstats/wwwroot/icon/;
}
location /awstats/js/ {
alias /usr/local/awstats/wwwroot/js/;
}
location /awstatsicons/ {
alias /usr/local/awstats/wwwroot/icon/;
}
location ~ ^/icon/
{
root /usr/local/awstats/wwwroot;
index index.html;
access_log off;
error_log off;
}
}
vim /data/scripts/awstats.sh
#!/bin/bash
back_path="/data/backup/wwwlogs"
log_file=access.log
date_dir=${back_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/$(date -d "yesterday" +"%d")/
if [ -f &data_dir$log_file ]
then
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=log.c.com -lang=cn -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -dir=/data/awstats/
else
exit 1
在网站访问低谷,创建定时任务
crontab -e
0 5 * * * cd /data/scripts && sh awstats.sh >/dev/null 2>&1
如果你已经切割了日志,现在就可以通过http://ogs.yourdomain:8080 来访问静态页面了
原文地址:http://blog.51cto.com/songky/2118088