环境介绍
awstats下载地址:
操作系统:Centos 6.* ( 双核4G、硬盘至少20G)
awstats 版本:awstats-7.6.tar.gz
IP:192.168.1.180
nginx
一:准备
1.1)修改nginx.conf的日志格式,不然awstats无法分析。
vim /usr/local/nginx/conf/nginx.conf
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 logs/access.log access;

1.2)重启或重新加载nginx的配置文件
/etc/init.d/nginx restart 或 service nginx reload
1.3:自动切割nginx 日志
vim /opt/nginx_log.sh
#!/bin/bash
#
# Filename: nginxCutLog.sh
# Author: Qicheng
# Website: http://www.linuxidc.com/
# Description: 切割nginx日志
# Notes: 设置crontab,每天23点59分定时执行
#
ROOT_UID=0
if [ "$UID" -ne "$ROOT_UID" ];then
echo "Error: 必须以root用户运行此程序!"
exit 1
fi
nginx_logs_dir="/usr/local/nginx/logs"
nginx_pid_file="/usr/local/nginx/logs/nginx.pid"
# 切割后的日志文件名,例如access_20141022.log
nginx_log_today="$nginx_logs_dir/access_`date +%Y%m%d`.log"
[ -f "$nginx_log_today" ] && exit 1
mv $nginx_logs_dir/access.log $nginx_log_today
# 给nginx发送USR1信号,使重新打开新的access.log日志文件
[ -f $nginx_pid_file ] && /bin/kill -USR1 $(cat $nginx_pid_file)
1.4)计划任务
59 23 * * * /bin/bash /opt/nginx_log.sh
二:Awstats安装
2.1) 解压awstats包儿
tar -zxf awstats-7.6.tar.gz -C /usr/local
2.2)重命名
mv awstats-7.6 awstats
2.3)修改属主、属组权限
chown root:root awstats
2.4)新建文件夹
mkdir -p /var/lib/awstats
三:Awstats配置
3.1)执行配置脚本awstats_configure.pl
cd /usr/local/awstats/tools/
./awstats_configure.pl


3.2)修改配置文件
vim /etc/awstats/awstats.www.test.com.conf
LogFile="/var/log/nginx/access_%YYYY-24%MM-24%DD-24.log"
注:修改成后,这里日期格式“%YYYY-24%MM-24%DD-24”,是指24小时之前的年月日,也就是昨天的日期。而执行下条命令会报错,因为nginx是当天装的所以没有昨天的日志

在此去掉了24小时
LogFile="/usr/local/nginx/logs/access_%YYYY%MM%DD.log"

3.3)测试
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.test.com

四:配置awstats生成静态页面
4.1)利用awstats的工具将统计的结果生成静态文件:
mkdir -p /var/www/awstats
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=www.test.com -lang=cn -dir=/var/www/awstats -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

4.2)要把awstats统计结果页面进行密码保护,这里需要用到apache自带的工具htpasswd:
yum -y install httpd-tools
htpasswd -cd admin.pass admin

4.3)把生成的密码文件admin.pass放到nginx主配置目录下
mv admin.pass /usr/local/nginx/conf/

4.4)nginx配置扩展目录下新建awstats.conf配置文件
vim /usr/local/nginx/conf/conf.d/awstats.conf
server {
listen 83;
server_name localhost;
location ~ ^/awstats/ { # html 静态页面目录
root /var/www;
index index.html;
access_log off;
error_log off;
charset gb2312;
auth_basic "admin";
auth_basic_user_file admin.pass;
}
location ~ ^/icon/ { # 图标目录
root /usr/local/awstats/wwwroot;
index index.html;
access_log off;
error_log off;
}
}
4.5:重启nginx服务使上述生效
/etc/init.d/nginx restart
五:Web访问
http://www.test.com:83/awstats/awstats.www.test.com.html


六:awstats 进行多站点日志分析
6.1)执行配置脚本awstats_configure.pl
cd /usr/local/awstats/tools
./awstats_configure.pl


6.2)修改配置文件
vim /etc/awstats/awstats.www.test.com.conf
LogFile="/usr/local/nginx/logs/access_%YYYY%MM%DD.log"
6.3)测试
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.wff.com

6.4)配置awstats生成静态页面
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=www.wff.com -lang=cn -dir=/var/www/awstats -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

6.5)重启nginx
service nginx restart
Web访问
http://192.168.1.180:83/awstats/awstats.www.wff.com.html

网友评论