閉じる

AWStats(Apacheアクセスログ解析)の導入・設定

[root@server1 ~]# dnf –enablerepo=crb -y install perl-Switch ← AWStatsのインストールに必要なパッケージをインストールします。


[root@server1 ~]# dnf -y install awstats ← AWStatsをインストールします。

[root@server1 ~]# rm -f /etc/httpd/conf.d/awstats.conf ; touch /etc/httpd/conf.d/awstats.conf ← Apache用AWStats設定のファイルを初期化します。

[root@server1 ~]# cd /usr/share/awstats ← AWStatsの設定スクリプト格納ディレクトリへ移動します。


[root@server1 ~]# perl `rpm -ql awstats|grep “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:
> www.zoah.net ← サイト名応答(例:www.zoah.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@server1 ~]# cd ← ディレクトリ移動(ホームへ戻ります。)

[root@server1 ~]# vi /etc/awstats/awstats.www.zoah.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アクセスログファイル名を指定します。暗号化している場合は、 ssl_access_log を指定して下さい。

# 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="centossrv.com www.zoah.net 127.0.0.1 localhost REGEX[^192\.168\.100\.]" ← 自ホストおよび内部(例:192.168.100.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\.100\.]" ← 自ホストおよび内部(例:192.168.100.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" ← 行頭の#を削除してコメントを解除します。(アクセス元国情報を詳細に取得します。)

# 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@server1 ~]# dnf -y install perl-CPAN ← perl-CPANをインストールします。


[root@server1 ~]# 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.

[root@server1 ~]# 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@server1 ~]# bash awstatsinit.sh www.zoah.net ← AWStatsのデータベース初期作成スクリプトを実行します。


[root@server1 ~]# rm -f awstatsinit.sh ← AWStatsのデータベース初期作成スクリプトを削除します。

[root@server1 ~]# 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@server1 ~]# chmod +x /etc/cron.daily/awstats ← AWStatsのデータベース自動更新設定スクリプトへ実行権限を付加します。

[root@server1 ~]# 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
}

[root@server1 ~]# vi /etc/httpd/conf.d/awstats.conf ← Apache用AWStats設定ファイルを編集します。

<Directory "/usr/share/awstats/wwwroot">
    Options None
    AllowOverride None
    Require all granted ← 追加(Order句、Allow句のApache2.4変更対応)
    #Order allow,deny ← 行頭に#を追加してコメントアウトします。
    #Allow from all ← 行頭に#を追加してコメントアウトします。
</Directory>

[root@server1 ~]# systemctl reload httpd ← Apacheに設定を反映させます。

http://Webサーバー名/awstats/awstats.plへアクセスすると、アクセス統計ページが表示されます。

© 2025 Linuxで自宅サーバー(ホームサーバー)-構築・設定方法- | WordPress テーマ: CrestaProject の Annina Free