Awstats日誌分析(實戰)

環境準備

CentOS 7-4:192.168.18.148---->服務器

win 7-1:192.168.18.129---->客戶機

在CnetOS 7-4中安裝服務:
[root@localhost ~]# yum install bind httpd -y
在CnetOS 7-4中配置DNS服務:
[root@localhost ~]# vim /etc/named.conf
options {
        listen-on port 53 { any; };			#127.0.0.1改爲any
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        recursing-file  "/var/named/data/named.recursing";
        secroots-file   "/var/named/data/named.secroots";
        allow-query     { any; };			#localhost改爲any
#修改完成後按Esc退出插入模式,輸入:wq保存退出

[root@localhost ~]# vim /etc/named.rfc1912.zones
#添加以下五行
zone "kgc.com" IN {
        type master;
        file "kgc.com.zone";
        allow-update { none; };
};
#修改完成後按Esc退出插入模式,輸入:wq保存退出

[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost kgc.com.zone
[root@localhost named]# vim kgc.com.zone
#刪除最後一行之後再插入以下內容
www  IN A       192.168.18.148
[root@localhost named]# systemctl start named
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.18.148:80		#41行刪除註釋,IP地址爲linux的地址
#Listen 80		#42行添加註釋
ServerName www.kgc.com:80		#95行作此更改
#修改完成後按Esc退出插入模式,輸入:wq保存退出
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start httpd

win7-1客戶機上的驗證操作:

首先在網絡適配器中修改DNS解析地址IP:192.168.18.148

在這裏插入圖片描述

此時可以在瀏覽器中輸入:www.kgc.com訪問到Apache測試網頁

在這裏插入圖片描述

此時可以回CnetOS 7-4中安裝Awstats
[root@localhost ~]# cd /var/log/httpd/
[root@localhost httpd]# cat access_log
192.168.18.129 - - [16/Dec/2019:13:31:15 +0800] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
.......下面省略多行
#此時可以看到129的地址來訪問的記錄

[root@localhost httpd]# mkdir /aaa
[root@localhost httpd]# mount.cifs //192.168.10.190/rpm /aaa
Password for root@//192.168.10.190/rpm:
[root@localhost httpd]# cd /aaa
[root@localhost aaa]# ls
apr-1.6.2.tar.gz                  hzw.jpeg                   php
apr-util-1.6.0.tar.gz             inotify-tools-3.14.tar.gz  redis-5.0.7.tar.gz
awstats-7.6.tar.gz                jdk                        ruby-2.4.1.tar.gz
[root@localhost aaa]# tar zxvf awstats-7.6.tar.gz -C /opt/
[root@localhost aaa]# cd /opt/
[root@localhost opt]# mv awstats-7.6/ /usr/local/awstats
[root@localhost opt]# cd /usr/local/
[root@localhost local]# ls
awstats  bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@localhost local]# cd awstats/tools/
[root@localhost tools]# ls
awstats_buildstaticpages.pl  dolibarr            maillogconvert.pl   xslt
awstats_configure.pl         geoip_generator.pl  nginx
awstats_exportlib.pl         httpd_conf          urlaliasbuilder.pl
awstats_updateall.pl         logresolvemerge.pl  webmin
[root@localhost tools]# ./awstats_configure.pl
......此處省略多行
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):
> /etc/httpd/conf/httpd.conf		#輸入Apache的配置文件路徑
......此處省略多行
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ? y		#確定新建
......此處省略多行
Your web site, virtual server or profile name:
> www.kgc.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):
>		#直接回車
-----> Restart Web server with '/sbin/service httpd restart'
Redirecting to /bin/systemctl restart httpd.service			#此時會重啓apache服務
......此處省略多行
Press ENTER to continue...		#直接回車
......此處省略多行
> perl awstats.pl -update -config=www.kgc.com
You can also read your statistics for 'www.kgc.com' with URL:
> http://localhost/awstats/awstats.pl?config=www.kgc.com		#提示訪問路徑,在地址欄中輸入就可以看到日誌訪問系統的網頁
Press ENTER to finish...		#直接回車


#檢查Apache配置文件中是否有awstats的模塊:
[root@localhost tools]# cd /etc/httpd/conf
[root@localhost conf]# vim httpd.conf
#按大寫字母G到末行,可以看到新加入的awstats內容,如下:
#注意需要做權限的修改!!!
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		此行需要註釋掉
     Require all granted		#添加此行,同意所有人進行訪問
</Directory>
#修改後按Esc退出插入模式,輸入:wq保存退出

[root@localhost conf]# cd /etc/awstats/
[root@localhost awstats]# ls
awstats.www.kgc.com.conf
[root@localhost awstats]# vim awstats.www.kgc.com.conf
LogFile="/var/log/httpd/access_log"		#50行,改爲需要監控的access_log日誌文件的位置
DirData="/var/lib/awstats"		#220行,分析日誌的數據存放位置,默認不存在,需要創建
#修改後按Esc退出插入模式,輸入:wq保存退出

[root@localhost awstats]# lcd /var/lib/
[root@localhost lib]# ls
AccountsService 	alsa	alternative		authconfig 		bluetooth       
certmonger    chrony       color        colord          dav
......以下省略多項
#此時沒有awstats文件,需要手動創建
[root@localhost lib]# mkdir awstats		
[root@localhost awstats]# systemctl restart httpd		#重啓服務

驗證:

在win 7-1的瀏覽器地址欄中輸入以下網址就可以訪問Awstats網頁:http://localhost/awstats/awstats.pl?config=www.kgc.com

但是此時訪問是沒有進行統計的,需要使用第二個工具

在這裏插入圖片描述

[root@localhost awstats]# cd /usr/local/awstats/
[root@localhost awstats]# cd tools/
[root@localhost tools]# ./awstats_updateall.pl now			#更新數據
Running '"/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -update -config=www.kgc.com -configdir="/etc/awstats"' to update config www.kgc.com
Create/Update database for config "/etc/awstats/awstats.www.kgc.com.conf" by AWStats version 7.6 (build 20161204)
From data in log file "/var/log/httpd/access_log"...
Phase 1 : First bypass old records, searching new record...
Searching new records from beginning of log file...
Phase 2 : Now process new records (Flush history on disk after 20000 hosts)...
Jumped lines in file: 0
Parsed lines in file: 43
 Found 0 dropped records,
 Found 0 comments,
 Found 0 blank records,
 Found 0 corrupted records,
 Found 0 old records,
 Found 43 new qualified records.

此時統計分析的數據會發生變化:

在這裏插入圖片描述

通過計劃性任務自動執行更新數據:

因爲每次如果想看最新的日誌統計信息,就必須要執行更新數據的腳本,對此我們可已使用週期性計劃任務進行優化:
[root@localhost tools]# crontab -e
*/5 * * * * /usr/local/awstats/tools/awstats_updateall.pl now		#每5分鐘更新一次
#插入內容後按Esc退出插入模式,輸入:wq保存退出

crontab: installing new crontab
#此時會提示有新的計劃性任務
[root@localhost tools]# systemctl start crond
您在 /var/spool/mail/root 中有郵件

優化網頁地址:

因爲url地址長度太長,此處我們可以對此進行優化:
[root@localhost tools]# cd /var/www/html
[root@localhost html]# vim aws.html
<html>
  <head>
    <meta http-equiv=refresh content="0;url=http://www.kgc.com/awstats/awstats.pl?config=www.kgc.com">
  </head>
  <body></body>
</html>
#插入內容後按Esc退出插入模式,輸入:wq保存退出

驗證:在win 7-1中的瀏覽器地址欄中輸入:http://www.kgc.com/aws.html

就可以直接訪問Awstats的日誌分析網頁

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章