Apache日志分析工具awstats配置

 

awstats软件下载地址:

http://www.awstats.org/#DOWNLOAD

https://awstats.sourceforge.io/#download

一、准备工作

1、一台Linux系统虚拟机或服务器,这里我使用的是CentOS7.3系统虚拟机

IP地址:192.168.115.120

域名:www. awstats.com

CentOS7.3中执行下面的内容添加域名解析

echo “192.168.115.120  www. awstats.com” >>/etc/hosts

 

2、一台用来测试配置结果的window10客户端

IP地址和CentOS7.3在同一网段,同样Windows10C:\Windows\System32\drivers\etc\hosts文件中也需要添加域名解析, 以记事本打开hosts文件,最后加上一行:192.168.115.120  www. awstats.com,保存退出。

二、安装Apache服务及相关配置

1Apache快速编译安装

yum -y install gcc gcc-c++ make pcre-devel expat-devel perl

wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.6.3.tar.gz

tar xf apr-1.6.3.tar.gz

cd apr-1.6.3

./configure

make && make install

cd ..

 

 

wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.1.tar.gz

tar xf apr-util-1.6.1.tar.gz

cd apr-util-1.6.1

./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

make && make install

cd ..

 

 

wget https://ftp.pcre.org/pub/pcre/pcre-8.34.tar.gz

tar xf pcre-8.34.tar.gz

cd pcre-8.34

./configure

make && make install

cd ..

 

 

wget http://www.zlib.net/fossils/zlib-1.2.11.tar.gz

tar xf zlib-1.2.11.tar.gz

cd zlib-1.2.11

./configure

make && make install

cd ..

 

wget  http://archive.apache.org/dist/httpd/httpd-2.4.7.tar.gz

tar xf httpd-2.4.7.tar.gz

cd httpd-2.4.7

./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-deflate --enable-expires --enable-headers --enable-modules=most --enable-so  --with-mpm=worker --enable-rewrite --enable-charset-lite --enable-cgid --enable-cgi

make && make install

cd ..

 

2、将Apache服务添加到系统服务并设置开机自启

cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd

vim /etc/init.d/httpd  编辑此文件添加以下两行内容

#!/bin/sh

#chkconfig:35 80 25

#description:web server

 

chkconfig --add httpd    httpd加入到service管理器

systemctl daemon-reload   重载系统系统管理器

systemctl enable httpd    设置httpd为开机自启

 

3Apache其他配置

设置主机名

vim /usr/local/apache2/conf/httpd.conf

ServerName www.awstats.com:80

/usr/local/apache2/bin/apachectl start

 

优化执行路径(把Apache的执行程序文件做个软链接使其可以直接执行)

ln -s /usr/local/apache2/conf/httpd.conf /etc/httpd.conf

ln -s /usr/local/apache2/bin/* /usr/local/bin/

 

三、配置Awstats日志分析软件

1、查看是否加载和编译cgi模块

注意:awstats不支持分析分割日志,如果日志已分割,需修改回来

确认是否加载了cgi模块

[root@localhost ~]# apachectl -D DUMP_MODULES | grep cgi

 

确认是否编译过cgi模块 

[root@localhost ~]# ls /usr/local/apache2/modules/|grep cgi 

mod_cgid.so

mod_cgi.so

mod_proxy_fcgi.so

mod_proxy_scgi.so

通过查看发现这里没有加载cgi模块,但是编译过cgi模块

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf   去掉cgi相关注释

LoadModule cgid_module modules/mod_cgid.so

LoadModule cgi_module modules/mod_cgi.so

 

2、下载并配置awstats软件

1)下载awstats软件并解压

[root@localhost ~]# wget https://jaist.dl.sourceforge.net/project/awstats/AWStats/7.6/awstats-7.6.tar.gz

[root@localhost ~]# tar xf awstats-7.6.tar.gz

[root@localhost ~]# mv awstats-7.6 /usr/local/awstats

2编辑awstats配置脚本

[root@localhost ~]# cd /usr/local/awstats/tools/

[root@localhost tools]# ./awstats_configure.pl

----- AWStats awstats_configure 1.0 (build 20140126) (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

  Found Web server Apache config file '/usr/local/apache2/conf/httpd.conf'

 

-----> Check and complete web server config file '/usr/local/apache2/conf/httpd.conf'

Warning: You Apache config file contains directives to write 'common' log files

This means that some features can't work (os, browsers and keywords detection).

Do you want me to setup Apache to write 'combined' log files [y/N] ? y

  Add 'Alias /awstatsclasses "/usr/local/awstats/wwwroot/classes/"'

  Add 'Alias /awstatscss "/usr/local/awstats/wwwroot/css/"'

  Add 'Alias /awstatsicons "/usr/local/awstats/wwwroot/icon/"'

  Add 'ScriptAlias /awstats/ "/usr/local/awstats/wwwroot/cgi-bin/"'

  Add '' directive

  AWStats directives added to Apache config file.

 

-----> 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:

> www.awstats.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.www.awstats.com.conf'

 Config file /etc/awstats/awstats.www.awstats.com.conf created.

 

-----> Restart Web server with '/sbin/service httpd restart'

 

-----> 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.awstats.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...    继续按回车

 

 

A SIMPLE config file has been created: /etc/awstats/awstats.www.awstats.com.conf

You should have a look inside to check and change manually main parameters.

You can then manually update your statistics for 'www.awstats.com' with command:

> perl awstats.pl -update -config=www.awstats.com

You can also read your statistics for 'www.awstats.com' with URL:

> http://localhost/awstats/awstats.pl?config=www.awstats.com

记住这一段,之后会在浏览器使用这个地址

 

Press ENTER to finish...     继续按回车

 

3)编辑awstats程序的主配置文件

[root@localhost ~]# vim /etc/awstats/awstats.www.awstats.com.conf

LogFile="/usr/local/apache2/logs/access_log"

DirData="/usr/local/awstats/awstats_data"

 

[root@localhost ~]# systemctl restart httpd

[root@localhost ~]# cd /usr/local/awstats/tools/

[root@localhost tools]# chmod +x awstats_updateall.pl

[root@localhost tools]# ./awstats_updateall.pl now   手动采集数据

 

4)设置crond开机自启和定时任务,每5分钟采集一次数据

[root@localhost tools]# crontab -e

*/5 * * * * /usr/local/awstats/tools/awstats_updateall.pl now

 

[root@localhost ~]# systemctl enable crond

[root@localhost ~]# systemctl list-unit-files | grep crond

crond.service                               enabled

 

5httpd.conf中添加允许访问的权限

[root@localhost ~]# vim /etc/httpd.conf

    Options None

    AllowOverride None

    Order allow,deny

    Allow from all

    Require all granted

 

[root@localhost ~]# systemctl restart httpd

 

四、在Windows的浏览器中进行测试

1、浏览器测试页面是否能正常访问

浏览器地址栏输入以下网址:http://192.168.115.120//awstats/awstats.pl?config=www.awstats.com会出现如下界面。

2、配置页面自动跳转优化统计页面访问

[root@localhost ~]# vim /usr/local/apache2/htdocs/index.html

 

上面跳转代码写好之后重启Apache服务

[root@localhost ~]# systemctl restart httpd

 

Windows浏览器地址栏输入www.awstats.com,就会自动跳转到http://192.168.115.120//awstats/awstats.pl?config=www.awstats.com地址了。

好了,到这里我们的Apache日志分析工具awstats就已经配置完成了。

 

注意:服务器如果没做dns的话,必须服务器和客户端上都修改hosts文件