指引网

当前位置: 主页 > 操作系统 > Linux >

linux中搭建awstats分析nginx日志

来源:网络 作者:佚名 点击: 时间:2017-05-13 00:16
[摘要]  用awstats来分析nginx日志,会让运维感觉很直观,也很方便的知道每天有多少pv,用户对那些页面访问得比较多,这样也容易有针对性的去优化web服务器.下面我们来看看怎么安装.

系统:centos 5.x
  需要的软件包:awstats-7.3.tar.gz

1.修改nginx日志格式

 代码如下 复制代码

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for" "$request_time"';

access_log  /var/log/www/access.log  main;

ps:
把log_format这段代码放在你nginx的http的定义段中,可以在下面的每一个server中引用此格式,不必在每个server里面都去定义格式.

2.切割nginx日志脚本

 代码如下 复制代码

vi cut_log.sh

#!/bin/bash
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
logs_path="/var/log/www/"
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/sbin/nginx -s reopen
/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

将会有如下一些提示:

 代码如下 复制代码

-----> Running OS detected: Linux, BSD orUnix

----->Check for web server install
Enterfull config file path of your Web server.
Example:/etc/httpd/httpd.conf
Example:/usr/local/apache2/conf/httpd.conf
Example:c:Program filesapache groupapacheconfhttpd.conf Config file path ('none' to skip web server setup):
>none #这里添none并回车,因为我们没有使用apache
回车之后下一个选项:
Yourweb server config file(s) could not be found.
Youwill need to setup your web server manually to declare AWStats
script as a CGI, if you want tobuild 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 anew AWStats config/profilefile (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: www.mysite.com
Example: demo
Yourweb site, virtual server or profile name:
>www.111cn.net #这里输入你要分析的域名,或是随便一个你易记的配置名并回车

----->Define config file path In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directorypath to store config file(s) (Enter fordefault):
> #直接回车,定义你的配置文件存放的路径,使用默认路径/etc/awstats

----->Create config file '/etc/awstats/awstats.nginx.conf'
Configfile /etc/awstats/awstats.nginx.conf created.
-----> Add updateprocess inside a scheduler Sorry, configure.pl does not support automatic addto cron yet.
You can do it manually by adding the following command to yourcron: /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx
Or if you have several config files and prefer having only onecommand: /usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue...#按回车继续
A SIMPLE config file has been created: /etc/awstats/awstats.nginx.conf You should have a lookinside to check and change manually main parameters.
Youcan then manually update your statistics for'yuyuanchun.com' withcommand: > perl awstats.pl -update -config=nginx
Youcan also build static report pages for'nginx' with command:> perl awstats.pl -output=pagetype -config=nginx
PressENTER to finish... #回车完成配置文件的创建

完成配置文件的创建后,我们还要修改一下.因为我是按天切割的日志,切割完成后交由awstats去分析.并不是让awstats去分时正在时时增长的也就是正在被写入的日志,这样的好处是不至于遗漏数据,并且分析已经切割完成的日志,更不用担心会有冲突.坏处是我一天切割一次日志,你要等第二天才能看昨天的一些详细数据.

 代码如下 复制代码
vi /etc/awstats/awstats.www.111cn.net.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/www.111cn.net/awstats/www.111cn.net

当然也可以用脚本来实现:
vi awstats.sh

 代码如下 复制代码

#!/bin/bash
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
mkdir -p /var/www/vhosts/www.111cn.net/awstats/www.111cn.net
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update 
-config=www.111cn.net -lang=cn -dir=/var/www/vhosts/www.111cn.net/awstats/www.111cn.net 
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

ps:
/usr/local/awstats/tools/awstats_buildstaticpages.pl    Awstats 静态页面生成工具
-update -config=www.111cn.net     更新配置项
-lang=cn     语言为中文
-dir=/var/www/vhosts/www.111cn.net/awstats/www.111cn.net    统计结果输出目录
-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配置

 代码如下 复制代码

server {
        listen       80;
        server_name www.111cn.net;
        access_log  /var/log/www/access.log  main;
        root /var/www/vhosts/www.111cn.net;
        index index.php index.html index.htm;

        location ~ ^/awstats/ {
                root /var/www/vhosts/www.111cn.net/;
                autoindex on;
                index index.html;
                access_log off;
                auth_basic "Please enter your password:";
                auth_basic_user_file /var/www/vhosts/htpasswd;
                }

        location ~ ^/icon/ {
                root   /usr/local/awstats/wwwroot;
                index  index.html;
                access_log off;
                }
}

然后重启nginx,当然我加了身份认证的,只让自己可以看,保证安全性.好了,awstats的安装配置就到这里了.剩下的就是等生成数据.

------分隔线----------------------------
栏目列表
推荐内容