用awstats来分析nginx日志,会让运维感觉很直观,也很方便的知道每天有多少pv,用户对那些页面访问得比较多,这样也容易有针对性的去优化web服务器.下面我们来看看怎么安装.
系统:centos 5.x
需要的软件包:awstats-7.3.tar.gz
1.修改nginx日志格式
vi /etc/nginx/nginx.conf
1 |
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' |
2 |
'$status $body_bytes_sent "$http_referer" ' |
3 |
'"$http_user_agent" "$http_x_forwarded_for" "$request_time"'; |
1 |
access_log /var/log/www/access.log main; |
ps:
把log_format这段代码放在你nginx的http的定义段中,可以在下面的每一个server中引用此格式,不必在每个server里面都去定义格式.
2.切割nginx日志脚本
vi cut_log.sh
02 |
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin |
03 |
logs_path="/var/log/www/" |
04 |
date_dir=${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/$(date -d "yesterday" +"%d")/ |
05 |
gzip_date_dir=${logs_path}$(date -d "-2 day" +"%Y")/$(date -d "-2 day" +"%m")/$(date -d "-2 day" +"%d")/ |
08 |
mv ${logs_path}*access.log $date_dir |
09 |
/usr/sbin/nginx -s reopen |
10 |
/usr/bin/gzip ${gzip_date_dir}*.log |
然后丢到crontab里,每天晚上11点57分执行切割:
57 23 * * * /bin/sh /root/webbak/cut_log.sh
3.安装awstats
wget http://awstats.sourceforge.net/files/awstats-7.3.tar.gz
tar zxf awstats-7.3.tar.gz && mv awstats-7.3 /usr/local/awstats
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
将会有如下一些提示:
01 |
-----> Running OS detected: Linux, BSD orUnix |
03 |
----->Check for web server install |
04 |
Enterfull config file path of your Web server. |
05 |
Example:/etc/httpd/httpd.conf |
06 |
Example:/usr/local/apache2/conf/httpd.conf |
07 |
Example:c:\Program files\apache group\apache\conf\httpd.conf Config file path ('none' to skip web server setup): |
08 |
>none #这里添none并回车,因为我们没有使用apache |
10 |
Yourweb server config file(s) could not be found. |
11 |
Youwill need to setup your web server manually to declare AWStats |
12 |
script as a CGI, if you want tobuild reports dynamically. See AWStats setup documentation (file docs/index.html) |
13 |
----->Update model config file '/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf' File awstats.model.conf updated. |
15 |
----->Need to create a new config file ? Do you want me to build anew AWStats config/profilefile (required if first install) [y/N] ? y #这里选Y,创建一个新的配置文件 |
17 |
-----> Define config file name to create |
18 |
What is the name of your web site or profile analysis ? |
19 |
Example: www.mysite.com |
21 |
Yourweb site, virtual server or profile name: |
22 |
>blog.slogra.com #这里输入你要分析的域名,或是随便一个你易记的配置名并回车 |
24 |
----->Define config file path In which directory do you plan to store your config file(s) ? |
26 |
Directorypath to store config file(s) (Enter fordefault): |
27 |
> #直接回车,定义你的配置文件存放的路径,使用默认路径/etc/awstats |
29 |
----->Create config file '/etc/awstats/awstats.nginx.conf' |
30 |
Configfile /etc/awstats/awstats.nginx.conf created. |
31 |
-----> Add updateprocess inside a scheduler Sorry, configure.pl does not support automatic addto cron yet. |
32 |
You can do it manually by adding the following command to yourcron: /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx |
33 |
Or if you have several config files and prefer having only onecommand: /usr/local/awstats/tools/awstats_updateall.pl now |
34 |
Press ENTER to continue...#按回车继续 |
35 |
A SIMPLE config file has been created: /etc/awstats/awstats.nginx.conf You should have a lookinside to check and change manually main parameters. |
36 |
Youcan then manually update your statistics for'yuyuanchun.com' withcommand: > perl awstats.pl -update -config=nginx |
37 |
Youcan also build static report pages for'nginx' with command:> perl awstats.pl -output=pagetype -config=nginx |
38 |
PressENTER to finish... #回车完成配置文件的创建 |
完成配置文件的创建后,我们还要修改一下.因为我是按天切割的日志,切割完成后交由awstats去分析.并不是让awstats去分时正在时时增长的也就是正在被写入的日志,这样的好处是不至于遗漏数据,并且分析已经切割完成的日志,更不用担心会有冲突.坏处是我一天切割一次日志,你要等第二天才能看昨天的一些详细数据.
vi /etc/awstats/awstats.blog.slogra.com.conf
把LogFile="/var/log/httpd/mylog.log"
修改为:
LogFile="/var/log/www/%YYYY-24/%MM-24/%DD-24/access.log"
以上的完整路径是切割后保存的nginx日志文件.其中%YYYY-24/%MM-24/%DD-24表示年月日都减去24小时,也就是昨天的日志目录.
4.创建awstats用于记录数据的目录
mkdir -p /var/lib/awstats
5.创建存放awstats生成的静态文件的目录
mkdir -p /var/www/vhosts/blog.slogra.com/awstats/blog.slogra.com
当然也可以用脚本来实现:
vi awstats.sh
2 |
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin |
3 |
mkdir -p /var/www/vhosts/blog.slogra.com/awstats/blog.slogra.com |
4 |
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update \ |
5 |
-config=blog.slogra.com -lang=cn -dir=/var/www/vhosts/blog.slogra.com/awstats/blog.slogra.com \ |
6 |
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl |
ps:
/usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats 静态页面生成工具
-update -config=blog.slogra.com 更新配置项
-lang=cn 语言为中文
-dir=/var/www/vhosts/blog.slogra.com/awstats/blog.slogra.com 统计结果输出目录
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路径
然后把awstat.sh也丢到crontab里执行:
59 23 * * * /bin/sh /root/webbak/awstats.sh
最后手动执行下awstats.sh
./awstats.sh
如果你看到它生成了一堆网页,那就说明成功了.
6.修改nginx配置
03 |
server_name blog.slogra.com; |
04 |
access_log /var/log/www/access.log main; |
05 |
root /var/www/vhosts/blog.slogra.com; |
06 |
index index.php index.html index.htm; |
08 |
location ~ ^/awstats/ { |
09 |
root /var/www/vhosts/blog.slogra.com/; |
13 |
auth_basic "Please enter your password:"; |
14 |
auth_basic_user_file /var/www/vhosts/htpasswd; |
18 |
root /usr/local/awstats/wwwroot; |
然后重启nginx,当然我加了身份认证的,只让自己可以看,保证安全性.好了,awstats的安装配置就到这里了.剩下的就是等生成数据.
ps:本文参考了下面两位的文章而来的.
http://www.wkii.org/use-awstats-automatic-analysis-nginx-log.html
http://lxw66.blog.51cto.com/5547576/1323712
(责任编辑:IT) |