Apacheアクセスログ解析(AWStats)(CentOS8)

提供:EELの自由帳

メインページ > CentOS_8_で自宅サーバー構築  > Apacheアクセスログ解析(AWStats)(CentOS8)

AWStatsを利用してApacheのアクセスログを解析し、Webサイトへのアクセス統計をWebブラウザから参照できるようにする。

※AWStatsをインストールして手順と動作の確認中です。書きかけの記事です。

AWStatsインストール

必要なパッケージをインストール

[root@host3 ~]# dnf install perl

[root@host3 ~]# dnf install perl-libwww-perl

AWStatsダウンロード

[root@host3 ~]# wget http://prdownloads.sourceforge.net/awstats/awstats-7.7-1.noarch.rpm

AWStatsインストール

[root@host3 ~]# rpm -ivh awstats-7.7-1.noarch.rpm

ダウンロードしたファイルを削除

[root@host3 ~]# rm -f awstats-7.7-1.noarch.rpm

awstats.pl編集

  • レポートの日付形式を「月 2月 2010」から「2010年 2月」へ変更
[root@host3 ~]# vi `rpm -ql awstats|grep "awstats\.pl"`

                                if ( $MonthRequired eq 'all' ) {
                                        print "$Message[6] $YearRequired";
                                }
                                else {
                                        print
                                          #"$Message[5] $MonthNumLib{$MonthRequired} $YearRequired"; ← 行頭に#を追加してコメントアウト
                                          "$YearRequired$Message[6] ".$MonthNumLib{$MonthRequired}; ← 追加(日付をYYYY年 MM月形式にする)
                                }
 
                        print( $MonthRequired eq 'all'
                                ? "$Message[6] $YearRequired"
                                #: "$Message[5] " ← 行頭に#を追加してコメントアウト
                                  #. $MonthNumLib{$MonthRequired} ← 行頭に#を追加してコメントアウト
                                  #. " $YearRequired" ← 行頭に#を追加してコメントアウト
                                : "$YearRequired$Message[6] $MonthNumLib{$MonthRequired}" ← 追加(日付をYYYY年 MM月形式にする)
                        );

                                        #print "$MonthNumLib{$monthix}<br />$YearRequired"; ← 行頭に#を追加してコメントアウト
                                        print "$YearRequired$Message[6]<br />$MonthNumLib{$monthix}"; ← 追加(日付をYYYY年 MM月形式にする)
                                        
                                        #print "$MonthNumLib{$monthix} $YearRequired"; ← 行頭に#を追加してコメントアウト
                                        print "$YearRequired$Message[6] $MonthNumLib{$monthix}"; ← 追加(日付をYYYY年 MM月形式にする)

AWStats設定

AWStats設定スクリプト実行

[root@host2 ~]# touch /etc/httpd/conf.d/awstats.conf ← Apache用AWStats設定ファイル作成

[root@host2 ~]# cd /usr/local/awstats/ ← AWStats設定スクリプト格納ディレクトリへ移動

[root@host2 awstats]# perl /usr/local/awstats/tools/awstats_configure.pl ← AWStats設定スクリプト実行

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

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.d/awstats.conf ← Apache用AWStats設定ファイル名応答

-----> Check and complete web server config file '/etc/httpd/conf.d/awstats.conf'
  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 '/etc/awstats/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応答(AWStats設定ファイル作成)

-----> 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:
> eeljp.net ← サイト名応答(例:eeljp.net)

-----> 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):
> ← 空ENTER(AWStats設定ファイル作成先はデフォルト=/etc/awstats)

-----> Create config file '/etc/awstats/awstats.centossrv.com.conf'
 Config file /etc/awstats/awstats.centossrv.com.conf created.

-----> Restart Web server with '/sbin/service httpd restart'
Redirecting to /bin/systemctl restart  httpd.service

-----> 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=centossrv.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...
 ← 空ENTER

A SIMPLE config file has been created: /etc/awstats/awstats.centossrv.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'centossrv.com' with command:
> perl awstats.pl -update -config=centossrv.com
You can also read your statistics for 'centossrv.com' with URL:
> http://localhost/awstats/awstats.pl?config=centossrv.com

Press ENTER to finish...
 ← 空ENTER

[root@host2 awstats]# cd ← ディレクトリ移動(ホームへ戻る)

設定ファイル編集

[root@host2 ~]# vi /etc/awstats/awstats.eeljp.net.conf ← 設定ファイル編集
# "LogFile" contains the web, ftp or mail server log file to analyze.
# Possible values: A full path, or a relative path from awstats.pl directory.
# Example: "/var/log/apache/access.log"
# Example: "../logs/mycombinedlog.log"
# You can also use tags in this filename if you need a dynamic file name
# depending on date or time (Replacement is made by AWStats at the beginning
# of its execution). This is available tags :
#   %YYYY-n  is replaced with 4 digits year we were n hours ago
#   %YY-n    is replaced with 2 digits year we were n hours ago
#   %MM-n    is replaced with 2 digits month we were n hours ago
#   %MO-n    is replaced with 3 letters month we were n hours ago
#   %DD-n    is replaced with day we were n hours ago
#   %HH-n    is replaced with hour we were n hours ago
#   %NS-n    is replaced with number of seconds at 00:00 since 1970
#   %WM-n    is replaced with the week number in month (1-5)
#   %Wm-n    is replaced with the week number in month (0-4)
#   %WY-n    is replaced with the week number in year (01-52)
#   %Wy-n    is replaced with the week number in year (00-51)
#   %DW-n    is replaced with the day number in week (1-7, 1=sunday)
#                              use n=24 if you need (1-7, 1=monday)
#   %Dw-n    is replaced with the day number in week (0-6, 0=sunday)
#                              use n=24 if you need (0-6, 0=monday)
#   Use 0 for n if you need current year, month, day, hour...
# Example: "/var/log/access_log.%YYYY-0%MM-0%DD-0.log"
# Example: "C:/WINNT/system32/LogFiles/W3SVC1/ex%YY-24%MM-24%DD-24.log"
# You can also use a pipe if log file come from a pipe :
# Example: "gzip -d </var/log/apache/access.log.gz |"
# If there are several log files from load balancing servers :
# Example: "/pathtotools/logresolvemerge.pl *.log |"
#
LogFile="/var/log/httpd/access_log" ← Apacheアクセスログファイル名を指定

# Enter here all other possible domain names, addresses or virtual host
# aliases someone can use to access your site. Try to keep only the minimum
# number of possible names/addresses to have the best performances.
# You can repeat the "SiteDomain" value in this list.
# This parameter is used to analyze referer field in log file and to help
# AWStats to know if a referer URL is a local URL of same site or an URL of
# another site.
# Note: Use space between each value.
# Note: You can use regular expression values writing value with REGEX[value].
# Note: You can also use @/mypath/myfile if list of aliases are in a file.
# Example: "www.myserver.com localhost 127.0.0.1 REGEX[mydomain\.(net|org)$]"
#
HostAliases="127.0.0.1 localhost REGEX[^192\.168\.1\.]" ← 自ホストおよび内部(例:192.168.1.x)からのアクセスをアクセス元(リファラ)から除外する

# Do not include access from clients that match following criteria.
# If your log file contains IP addresses in host field, you must enter here
# matching IP addresses criteria.
# If DNS lookup is already done in your log file, you must enter here hostname
# criteria, else enter ip address criteria.
# The opposite parameter of "SkipHosts" is "OnlyHosts".
# Note: Use space between each value. This parameter is not case sensitive.
# Note: You can use regular expression values writing value with REGEX[value].
# Change : Effective for new updates only
# Example: "127.0.0.1 REGEX[^192\.168\.] REGEX[^10\.]"
# Example: "localhost REGEX[^.*\.localdomain$]"
# Default: ""
#
SkipHosts="127.0.0.1 localhost REGEX[^192\.168\.1\.]" ← 自ホストおよび内部(例:192.168.1.x)からのアクセスをアクセス元(クライアント)から除外する

# If you want to have hosts reported by name instead of ip address, AWStats
# need to make reverse DNS lookups (if not already done in your log file).
# With DNSLookup to 0, all hosts will be reported by their IP addresses and
# not by the full hostname of visitors (except if names are already available
# in log file).
# If you want/need to set DNSLookup to 1, don't forget that this will reduce
# dramatically AWStats update process speed. Do not use on large web sites.
# Note: Reverse DNS lookup is done on IPv4 only (Enable ipv6 plugin for IPv6).
# Note: Result of DNS Lookup can be used to build the Country report. However
# it is highly recommanded to enable the plugin 'geoipfree' or 'geoip' to
# have an accurate Country report with no need of DNS Lookup.
# Possible values:
# 0 - No DNS Lookup
# 1 - DNS Lookup is fully enabled
# 2 - DNS Lookup is made only from static DNS cache file (if it exists)
# Default: 2
#
DNSLookup=2
↓
DNSLookup=1 ← アクセス元の名前解決を行う(アクセス元をIPアドレスではなくホスト名で表示する)

# Plugin: GeoIPfree
# Perl modules required: Geo::IPfree version 0.2+ (from Graciliano M.P.)
# Country chart is built from an Internet IP-Country database.
# This plugin is useless for intranet only log files.
# Note: You must choose between using this plugin (need Perl Geo::IPfree
# module, database less up to date) or the GeoIP plugin (need Perl Geo::IP
# module from Maxmind, database more up to date).
# Note: Activestate provide a corrupted version of Geo::IPfree 0.2 Perl
# module, so install it from elsewhere (from www.cpan.org for example).
# This plugin reduces AWStats speed of 10% !
#
#LoadPlugin="geoipfree"
↓
LoadPlugin="geoipfree" ← コメント解除(アクセス元国情報を詳細に取得)

# When AWStats builds HTML links in its report pages, it starts those links
# with "http://". However some links might be HTTPS links, so you can enter
# here the root of all your HTTPS links. If all your site is a SSL web site,
# just enter "/".
# This parameter is not used if ShowLinksOnUrl is 0.
# Example: "/shopping"
# Example: "/"
# Default: ""
#
UseHTTPSLinkForUrl="/" ← /を指定※Webサーバー間通信内容暗号化を導入している場合のみ
[root@host2 ~]# yum -y install perl-CPAN ← perl-CPANインストール

[root@host2 ~]# perl -MCPAN -e shell ← perl-CPAN起動
 
CPAN.pm requires configuration, but most of it can be done automatically.
If you answer 'no' below, you will enter an interactive dialog for each
configuration option instead.

Would you like to configure as much as possible automatically? [yes] ← 空ENTER

Autoconfigured everything but 'urllist'.

Now you need to choose your CPAN mirror sites.  You can let me
pick mirrors for you, you can select them from a list or you
can enter them by hand.

Would you like me to automatically choose some CPAN mirror
sites for you? (This means connecting to the Internet) [yes] ← 空ENTER
Trying to fetch a mirror list from the Internet
Fetching with LWP:
http://www.perl.org/CPAN/MIRRORED.BY

Looking for CPAN mirrors near you (please be patient)
.......................... done!

New urllist
  http://www.msg.com.mx/CPAN/
  http://ftp.osuosl.org/pub/CPAN/
  http://cpan.knowledgematters.net/

Autoconfiguration complete.

commit: wrote '/root/.cpan/CPAN/MyConfig.pm'

You can re-run configuration any time with 'o conf init' in the CPAN shell
Terminal does not support AddHistory.

cpan shell -- CPAN exploration and modules installation (v1.9800)
Enter 'h' for help.

cpan[1]> install Geo::IPfree ← Geo::IPfreeモジュールインストール

cpan[2]> quit ← perl-CPAN終了
Terminal does not support GetHistory.
Lockfile removed.

AWStatsデータベース作成

AWStatsデータベース初期作成

Apacheのログファイルを過去分も含めて全てAWStatsのデータベースに取り込む

[root@host2 ~]# vi awstatsinit.sh ← AWStatsデータベース初期作成スクリプト作成
#!/bin/bash

# データベース作成先ディレクトリ作成
mkdir -p `grep ^DirData /etc/awstats/awstats.$1.conf | awk -F= '{print $2}'|tr -d \"`

# AWStats設定ファイルよりApacheログファイル名取得
logfile=`grep ^LogFile /etc/awstats/awstats.$1.conf|sed -e 's/LogFile="\([^ ]*\)"/\1/p' -e d`

# 過去ログを古い順に処理
grep dateext /etc/logrotate.conf > /dev/null 2>&1
if [ $? -eq 0 ]; then
    ls $logfile-* > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        # 過去ログファイル名に日付が設定されている場合
        for log in `ls $logfile-*|sort`
        do
            `rpm -ql awstats|grep "awstats\.pl"` \
            -config=$1 -update -logfile=$log
        done
    fi
else
    ls $logfile.* > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        # 過去ログファイル名に通番が設定されている場合
        for log in `ls $logfile.*|sort -r`
        do
            `rpm -ql awstats|grep "awstats\.pl"` \
            -config=$1 -update -logfile=$log
        done
    fi
fi

# 現在ログ処理
for log in `ls $logfile|sort`
do
    `rpm -ql awstats|grep "awstats\.pl"` \
    -config=$1 -update -logfile=$log
done

[root@host2 ~]# sh awstatsinit.sh sudachi.jp ← AWStatsデータベース初期作成スクリプト実行
※ログ量により時間がかかる

[root@host2 ~]# rm -f awstatsinit.sh ← AWStatsデータベース初期作成スクリプト削除

[root@host2 ~]# vi /etc/cron.daily/awstats ← AWStatsデータベース自動更新設定スクリプト作成
#!/bin/bash
for conf in `ls /etc/awstats/|grep -v awstats.model.conf`
do
    site=`echo $conf|sed -e 's/awstats\.\([^ ]*\)\.conf/\1/p' -e d`
    `rpm -ql awstats|grep "awstats\.pl"` -config=$site -update > /dev/null
done

[root@host2 ~]# chmod +x /etc/cron.daily/awstats ← AWStatsデータベース自動更新設定スクリプトへ実行権限付加

Apacheログローテーション設定

Apacheログファイル切替え時、AWStatsのデータベースに取り込んでから切替えを行うようにする
[root@centos ~]# vi /etc/logrotate.d/httpd ← Apacheログローテーション設定ファイル編集
/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    ----追加(ここから)----
    prerotate
        `rpm -ql awstats|grep "awstats_updateall\.pl"` now -confdir="/etc/awstats" \
        -awstatsprog="`rpm -ql awstats|grep "awstats\.pl"`" >/dev/null
    endscript
    ----追加(ここまで)----
    delaycompress
    postrotate
        /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
    endscript
}

Apache設定

[root@host2 ~]# vi /etc/httpd/conf.d/awstats.conf ← Apache用AWStats設定ファイル編集
<Directory "/usr/local/awstats/wwwroot">
    Options None
    AllowOverride None
    Require all granted ← 追加(Order句、Allow句のApache2.4変更対応)
    #Order allow,deny ← 行頭に#を追加してコメントアウト
    #Allow from all ← 行頭に#を追加してコメントアウト
</Directory>

[root@host2 ~]# systemctl reload httpd ← Apache設定反映

AWStats確認

http://Webサーバー名/awstats/awstats.plへアクセスしてアクセス統計ページが表示されることを確認

アクセス統計ページHTML版の作成

awstats.plは、実行するたびにAWStatsのデータベースからログ情報を入力してアクセス統計ページを作成するため、サーバーに負荷がかかることから、awstats.plは内部からのみ実行できるようにする。また、万が一、AWStatsのデータベースが失われた場合、アクセス統計が見れなくなってしまうため、アクセス統計ページをHTMLで作成するようにする。

アクセス統計ページHTML版格納ディレクトリ設定

awstats.plアクセス制限

Apache設定反映

[root@host3 ~]# systemctl reload httpd

アクセス統計ページHTML版作成

アクセス統計ページHTML版作成スクリプト作成

アクセス統計ページHTML版作成スクリプトへ実行権限付加

[root@host3 ~]# chmod 700 awstatsreport.sh

アクセス統計ページHTML版作成スクリプト実行

[root@host3 ~]# ./awstatsreport.sh sudachi.jp /var/www/awstatsreport

アクセス統計ページHTML版確認

  • http://Webサーバー名/awstatsreport/へアクセスして今月分のアクセス統計ページが表示されることを確認
  • 過去のアクセス統計ページはhttp://Webサーバー名/awstatsreport/年月(YYYYMM).html(例:http://Webサーバー名/awstatsreport/200601.html)へアクセスする