前面两篇都在讲述如何去部署nginx下的awstats日志分析工具,如今终于轮到apache。做为老牌的网页服务器,awstats对apache的支持很是完美,因此整个配置过程也是十分简单。所以,在这里我就拓展了一下部署,实现了对多站点的日志分析功能。css
注意:自本篇博文发表之日,apache-2.4.x仍是没法支持部署awstats日志分析结果访问动态化,不过能够用前篇日志分析工具Awstats实战之Nginx篇-分析结果静态化中讲到的方法来实施部署awstats。所以,本篇博文将用apache2.2.25版原本为你们讲解如何部署awstats日志分析结果动态化及多站点。html
环境:nginx
CentOS 6.4 x86_64 ip:192.168.1.113 站点1:域名www.sunsky.com(server和client都经过hosts文件解析) 站点2:域名www.skysun.com(server和client都经过hosts文件解析) apache-2.2.25 编译安装,路径/usr/local/apache,服务开启状态 日志记录格式为apache默认的combined格式,切勿更改,不然会形成awstats没法分析日志。 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined apr-1.4.8 apr-util-1.5.2 awstats-7.2.tar.gz
1、日志自动切割web
因为apache自带的日志轮询工具rotatelogs,据专家说在进行日志切割时容易丢日志,因此这里咱们就用cronolog来作日志切割。数据库
一、下载并安装cronologapache
cd /server/tools/ wget http://cronolog.org/download/cronolog-1.6.2.tar.gz tar zxf cronolog-1.6.2.tar.gz cd cronolog-1.6.2 ./configure make&&make install
二、配置apache使用cronologvim
因为实验用的apache开启了虚拟主机功能,因此如下配置都在虚拟主机中进行,这样也是为了方便对多站点的分析操做。安全
vim /usr/local/apache/httpd/extra/httpd-vhosts.conf bash
将配置文件中的CustomLog和ErrorLog替换为下面的(因为咱们要对多站点,因此这里要替换两个虚拟主机的,而且将日志存放名字作有效的区分才好)服务器
www.sunsky.com CustomLog "|/usr/local/sbin/cronolog /app/logs/sunsky_access_%Y%m%d.log" combined ErrorLog "|/usr/local/sbin/cronolog /app/logs/sunsky_error_%Y%m%d.log" www.skysun.com CustomLog "|/usr/local/sbin/cronolog /app/logs/skysun_access_%Y%m%d.log" combined ErrorLog "|/usr/local/sbin/cronolog /app/logs/skysun_error_%Y%m%d.log"
注意:这里必定要注意对两个网站的日志名称配置。
2、Awstats的安装与配置
一、部署awstats
首先咱们要下载awstats软件包,并将其放在常规目录(/usr/local)下
wget http://awstats.sourceforge.net/files/awstats-7.2.tar.gz tar zxf awstats-7.2.tar.gz mv awstats-7.2 /usr/local/awstats
因为wget下载下来的包中权限是非root的,因此这里要修改权限,不然稍后*.pl将没法运行
chown -R root.root /usr/local/awstats chmod +x /usr/local/awstats/tools/*.pl chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl
接下来咱们要执行awstats/tools下的awstats_configure.pl配置向导,用来生成awstats的配置文件,awstats配置文件的命名规则是awstats.website.conf
cd /usr/local/awstats/tools/ ./awstats_configure.pl
此时会出现以下提示
----- AWStats awstats_configure 1.0 (build 1.9) (c) Laurent Destailleur ----- This tool will help you to configure AWStats to analyze statistics for one web server. You can try to use it to let it do all that is possible in AWStats setup, however following the step by step manual setup documentation (docs/index.html) is often a better idea. Above all if: - You are not an administrator user, - You want to analyze downloaded log files without web server, - You want to analyze mail or ftp log files instead of web log files, - You need to analyze load balanced servers log files, - You want to 'understand' all possible ways to use AWStats... Read the AWStats documentation (docs/index.html). -----> 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): > /usr/local/apache/conf/httpd.conf #有多是新版本的缘由,这里会自动找到apache的配置文件,无需咱们再本身填了,不过会问你一次,你回答y便可 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 #询问是否建立一个新的配置文件,这里填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: > www.sunsky.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): > #这里要填写你配置文件存放路径,咱们使用它默认的路径/etc/awstats,因此直接回车便可 -----> Create config file '/etc/awstats/awstats.www.sunsky.com.conf' Config file /etc/awstats/awstats.www.sunsky.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=www.sunsky.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... #提示不能自动加入crontab定时任务,须要稍后本身添加,咱们按回车继续便可 A SIMPLE config file has been created: /etc/awstats/awstats.www.sunsky.com.conf You should have a look inside to check and change manually main parameters. You can then manually update your statistics for 'www.sunsky.com' with command: > perl awstats.pl -update -config=www.sunsky.com You can also build static report pages for 'www.sunsky.com' with command: > perl awstats.pl -output=pagetype -config=www.sunsky.com Press ENTER to finish... #提示配置文件建立完成和如何更新配置及创建静态报告页,这里咱们回车便可结束这个配置向导
结束这个向导之后,咱们对www.sunsky.com站点的awstats配置文件已经配置好了,下面配置www.skysun.com的配置文件,步骤和上面同样,只是在域名的地方作更改便可。
二、修改awstats配置文件
完成配置文件的建立以后,咱们还须要对/etc/awstats/awstats.www.sunsky.com.conf里的一些参数进行修改。
sed -i 's#LogFile="/var/log/httpd/mylog.log"#LogFile="/app/logs/sunsky_access_%YYYY-24%MM-24%DD-24.log"#g' /etc/awstats/awstats.www.sunsky.com.conf
这里更改的目的是指定awstats须要分析的nginx的日志文件路径。这里的路径你们要按本身的日志路径来填。
sed -i 's#DirData="/var/lib/awstats"#DirData="/usr/local/awstats/data"#g' /etc/awstats/awstats.www.sunsky.com.conf
这里更改的目的是指定awstats的数据库配置文件(即awstats的数据库(纯文本))。
以上的两个替换操做进行完以后必定要用命令查看替换是否成功,以便及早发现纰漏。
grep "LogFile=" /etc/awstats/awstats.www.sunsky.com.conf grep "DirData=" /etc/awstats/awstats.www.sunsky.com.conf
查询替换结果正确以后,便可进行下面的步骤。
一样,重复以上的2个sed步骤,来对www.skysun.com的awstats配置文件作操做,须要注意的是命令里面的日志路径和对应的awstats的数据库配置文件不要搞错便可。
因为,此处没有/usr/local/awstats/data目录,因此咱们要建立出来
mkdir /usr/local/awstats/data
此时咱们要对/usr/local/awstats目录进行属主变动,变动为apache的daemon用户,否则一会访问就会报错。这里也是和nginx的一个明显区别所在。
chown -R daemon /usr/local/awstats
三、生成awstats统计信息数据库
如今咱们须要用awstats来生成对日志的统计分析信息出来了。因为咱们用的是支持perl的FCGI动态化访问页面,因此此处咱们只须要直接更新数据库便可。FCGI程序会自动将数据库以动态页面的形式展示出来,无须再手动生成静态页面了。本处咱们用脚原本完成。
vim /server/scripts/awstats_up.sh #!/bin/sh /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.sunsky.com >/dev/null 2>&1 /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.skysun.com >/dev/null 2>&1
该脚本里面用下面的命令也是能够的。
/usr/local/awstats/tools/awstats_updateall.pl now
运行该脚本生成分析结果
/bin/sh /server/scripts/awstats_up.sh
提示:因为咱们这里用的apache使用cronolog日志轮询工具使得文件出来都是带时间格式的。所以你可能会想,为何不改为对当天的日志进行操做。首先在这里咱们能够实时的进行日志分析数据库更新来查看实时的日志,不过这里不建议你们这样去作。
3、配置日志分析页面的来访ip的地址位置显示
这里咱们用国内最准确的ip数据库——QQ纯真库(点击下载),你们下载以后经过CRT用lrzsz工具传上去,具体步骤这里不写了。
附件里面有三个文件qqhostinfo.pm,qqwry.pl和QQWry.Dat,咱们将这三个文件通通都放到/usr/local/awstats/wwwroot/cgi-bin/plugins中。
接下来,咱们修改qqwry.pl文件,将./QQWry.Dat修改成${DIR}/plugins/QQWry
vim /usr/local/awstats/wwwroot/cgi-bin/plugins/qqwry.pl #my $ipfile="./QQWry.Dat";
修改成
my $ipfile="${DIR}/plugins/QQWry.Dat";
而后编辑awstats的配置文件/etc/awstats/awstats.www.sunsky.com.conf(根据你前面配置的站点信息生成的文件),将LoadPlugin="hostinfo"替换为LoadPlugin="qqhostinfo"便可。
sed -i 's#\#LoadPlugin="hostinfo"#LoadPlugin="qqhostinfo"#g' /etc/awstats/awstats.www.sunsky.com.conf sed -i 's#\#LoadPlugin="hostinfo"#LoadPlugin="qqhostinfo"#g' /etc/awstats/awstats.www.skysun.com.conf
切记,在这些替换完以后必定要查看替换是否成功,以便及早发生纰漏。
grep "LoadPlugin=\"qqhostinfo\"" /etc/awstats/awstats.www.sunsky.com.conf
若是检查无误,那么咱们的ip地址位置显示就配置好了,在后面的日志分析中,咱们就能够清楚的看到来访ip的地理位置信息了。
4、配置apache
首先咱们打开apache的主配置文件/usr/local/apache/conf/httpd.conf能够看到里面多出了如下几行:
# # Directives to allow use of AWStats as a CGI # Alias /awstatsclasses "/usr/local/awstats/wwwroot/classes/" Alias /awstatscss "/usr/local/awstats/wwwroot/css/" Alias /awstatsicons "/usr/local/awstats/wwwroot/icon/" ScriptAlias /awstats/ "/usr/local/awstats/wwwroot/cgi-bin/" # # This is to permit URL access to scripts/files in AWStats directory. # <Directory "/usr/local/awstats/wwwroot"> Options None AllowOverride None Order allow,deny Allow from all </Directory>
咱们能够看到awstats已经在apache的主配置文件配置好了须要的参数,是否是很方便啊。此时咱们还须要在配置虚拟目录配置文件里面配置几个参数来实现站点的统计信息安全。
vim /usr/local/apache/httpd/extra/httpd-vhosts.conf
添加如下两行参数进去
auth_basic "Restricted"; auth_basic_user_file /usr/local/nginx/htpasswd.pass;
添加以后的文件信息
<VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/www/sunsky" ServerName dummy-host.example.com ServerAlias www.sunsky.com CustomLog "|/usr/local/sbin/cronolog /app/logs/sunsky_access_%Y%m%d.log" combined ErrorLog "|/usr/local/sbin/cronolog /app/logs/sunsky_error_%Y%m%d.log" auth_basic "Restricted"; auth_basic_user_file /usr/local/apache/htpasswd.pass; </VirtualHost> <VirtualHost *:80> ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/www/skysun" ServerName www.skysun.com CustomLog "|/usr/local/sbin/cronolog /app/logs/skysun_access_%Y%m%d.log" combined ErrorLog "|/usr/local/sbin/cronolog /app/logs/skysun_error_%Y%m%d.log" auth_basic "Restricted"; auth_basic_user_file /usr/local/apache/htpasswd.pass; </VirtualHost>
接下来咱们用下面的命令来生成加密文件和加密的帐号及密码
htpasswd -c -m /usr/local/nginx/htpasswd.pass admin #用户名为admin,回车以后输入两次密码即完成建立
若是,你顺利的执行了上面的全部操做,那么OK!如今你已经能够经过访问
http://www.sunsky.com/awstats/awstats.pl?config=www.sunsky.com http://www.skysun.com/awstats/awstats.pl?config=www.skysun.com
来查看你多个站点的日志分析信息了。
5、配置awstats自动运行
为了让整个日志的统计过程能够实现自动化,咱们将awstats.sh脚本加入crontab定时任务中去
0 1 * * * /bin/sh /server/scripts/awstats_up.sh >/dev/null 2>&1
也许你会问为何不把cronolog也加入开机启动呢,由于cronolog已经默认被apache用来调用记录日志,因此无需加入到crontab定时任务中。
至此,咱们已经经过在nginx和apache上部署awstats日志访问分析工具来实现了对站点来访信息的分析。整个系列的博文很少,只有三篇,不过还算是详尽,把改涉及到的都涉及到了。中间有不少技术是连同的,好比在apche上进行的多站点,在nginx上的静态化访问等,尽管我没有再出,不过咱们均可以将他们来回应用实现部署需求。我也相信,只有融会贯通了,咱们你们才能更好的部署好这一利器!