実践Linux
RockyLinux8 RockyLinux8 目次へ TOP(HOME)へ
アクセス解析ツールawstatsの設置 2021年7月
●AWStats(7.8-2) の入手
まず、必要なパッケージ(perl、perl-libwww-perl)があるかどうか。なければインストールしておく。
# dnf list installed | grep perl
# dnf install perl
# dnf install perl-libwww-perl
AWStatsの入手には、外部レポジトリの追加(EPEL)が必要となる。
# dnf -y install epel-release
設定ファイル/etc/yum.repos.d/epel.repoの編集
[epel]セクションで「enabled=0」とする。 = 通常はリポジトリ無効。
enabled=0 で通常時は無効としている場合、一時的にリポジトリを有効にしてインストールするには、
# dnf --enablerepo=epel install [パッケージ名]
AWStats本体のインストール
調査 # dnf --enablerepo=epel list | grep awstats
インストール # dnf --enablerepo=epel -y install awstats
しかし、エラーがでてうまくいかない。
エラー:
問題: conflicting requests
- nothing provides perl(Switch) needed by awstats-7.7-5.el8.noarch
perl(Switch)が必要なようだ。
しかし、dnf関係ではどうやら存在しない。インターネットで探してみる。
https://pkgs.org/download/perl(Switch)
CentOS8のCentOS PowerTools x86_64よりperl-Switch-2.17-10.el8.noarch.rpmを開いてみると、
http://mirror.centos.org/centos/8/PowerTools/x86_64/os/Packages/perl-Switch-2.17-10.el8.noarch.rpm
がある。
そこで、
# wget http://mirror.centos.org/centos/8/PowerTools/x86_64/os/Packages/perl-Switch-2.17-10.el8.noarch.rpm ダウンロード
# rpm -ivh perl-Switch-2.17-10.el8.noarch.rpm インストール
# rm -f perl-Switch-2.17-10.el8.noarch.rpm ダウンロードファイルを削除
awstatsインストールに再度挑戦してみる。
# dnf --enablerepo=epel -y install awstats
成功。
●awstatsのconfファイルの編集
/etc/awstats/awstats.ns1.conf が自動作成されている。
ここでは名称をawstats.www.my-dom.xxx.confに変更。
編集
LogFile= Webサーバのログファイルの場所がフルパスで指定されていることを確認します。awstats.plのディレクトリからの相対パスも利用できます。
LogType= Webサーバのログを解析する場合には"W"、ストリーミングサーバのログファイルを解析する場合には"S"、メールサーバのログを解析する場合には"M"、FTPサーバのログを解析する場合には"F"が設定されていることを確認します。
LogFormat= ログフォーマット。combinedの場合は 1、commonの場合は 4( ただしこの場合ブラウザ情報等、一部の解析はできなくなる)。
DirIcons= AWStatsのアイコンディレクトリの相対URLが反映されていることを確認します.
SiteDomain= 解析したいWebサーバーに到達するために利用されるメインのドメインの名前もしくはイントラネットのサーバー名を設定します. 同一サイトが複数の名前でアクセスされる可能性がある場合は, HostAliasパラメータのリストに追加します.
HostAliases= ホストのエイリアス名。REGEXは正規表現。
DNSLookup= DNSの逆引き(ホスト名を表示) 0は行わない、1は完全に行う、2は キャッシュファイルからのみ。
DirData= データ保存用ディレクトリの位置を指定します。
DirCgi= AWStats のディレクトリを相対 URL で指定します。
DirIcons= icon のディレクトリを相対 URL で指定します。
以下抜粋
LogFile="/var/log/httpd/www_access_log" このlogファイル名はhttpd.confで確認しておくこと。
LogType=W
LogFormat=1
#SiteDomain="ns1" 下に変更
SiteDomain="www.my-dom.xxx"
#HostAliases="REGEX[^.*ns1$]" 下に変更
HostAliases="REGEX[my-dom\.net$]"
DNSLookup=2
DirData="/var/lib/awstats"
DirCgi="/awstats"
DirIcons="/awstatsicons"
DefaultFile="index.php index.html"
#SkipHosts="127.0.0.1" 下に変更
SkipHosts="127.0.0.1 REGEX[^192\.168\.]" LAN側とローカルからのアクセスはスキップするように指定する。
SkipFiles=""
OnlyFiles=""
●/etc/httpd/conf.d/awstats.confの編集
Alias /awstatscss "/usr/share/awstats/wwwroot/css/"
Alias /awstatsicons "/usr/share/awstats/wwwroot/icon/"
ScriptAlias /awstats/ "/usr/share/awstats/wwwroot/cgi-bin/"
<Directory "/usr/share/awstats/wwwroot">
Options None
AllowOverride None
<IfModule mod_authz_core.c>
# Apache 2.4
#Require local コメントアウトして、下に変更
Require ip 127.0.0.1 192.168.7 (すべてに許可する場合は、Require all granted)
</IfModule>
<IfModule !mod_authz_core.c> これは、Apache 2.2用なので使われない
# Apache 2.2
Order allow,deny
Allow from 127.0.0.1
Allow from ::1
</IfModule>
</Directory>
<IfModule mod_env.c>
SetEnv PERL5LIB /usr/share/awstats/lib:/usr/share/awstats/plugins
</IfModule>
httpdを再起動
●レポート手動出力 ( 自動では/etc/cron.hourly/awstats で、 1時間毎に自動更新されることになっている )
# /usr/share/awstats/wwwroot/cgi-bin/awstats.pl -config=www.my-dom.xxx -update
-config=www.my-dom.xxxでawstats.www.my-dom.xxx.confを指定するということになる。
●ブラウザで見る
http://www.my-dom.xxx/awstats/awstats.pl?config=www.my-dom.xxx にアクセスし、解析結果画面が正しく表示されれば OK 。
しかし、ローカル接続ではアクセス権がないとエラーが出る。
ローカルでは、http://localhost/awstats/awstats.pl?config=www.my-dom.xxxにすればOK。
●cronで解析内容の更新
まず、/etc/cron.hourly/awstatsを/etc/cron.daily/に移動しておく。
/etc/logrotate.confをみると、ログは weekly、4世代分残すことになっている。
awstatsのconfでLogFile="/var/log/httpd/www_access_log"としたので、これが突然rotateされてしまうと連続した解析結果が得られないことになる。
いったん、/var/lib/awstatsの統計データをいったんすべて消去してクリア。
# cat /var/log/httpd/www_access_log-* /var/log/httpd/www_access_log > /usr/share/awstats/log_html
を実行して/usr/share/awstats/log_htmlに全てのログを集める。
/etc/awstats/awstats.www.my-dom.xxx.confの編集
LogFile="/usr/share/awstats/log_html"に変更。
次に
# /usr/share/awstats/wwwroot/cgi-bin/awstats.pl -config=www.my-dom.xxx -update
を実行してこれまでの全統計データを作成しておく。
/etc/cron.daily/awstatsの編集
#!/bin/sh
# 下2行はコメントアウト
#exec /usr/share/awstats/tools/awstats_updateall.pl now -configdir="/etc/awstats" -awstatsprog="/usr/share/awstats/wwwroot/cgi-bin/awstats.pl" >/dev/null
#exit 0
rm /usr/share/awstats/log_html
cat /var/log/httpd/www_access_log-* /var/log/httpd/www_access_log > /usr/share/awstats/log_html
exec /usr/share/awstats/wwwroot/cgi-bin/awstats.pl -config=www.my-dom.xxx -update >/dev/null 2>&1
exit 0
awstats は、統計データを作成するときに、自分がどの時点までのログの統計データを処理したかを覚えていて、古いアクセスログデータを awstats 与えても、無視するようになっている──という機能を利用している。
SELinuxの属性を調整(他のスクリプトに合わせる)
# restorecon -F /etc/cron.daily/awstats
※ /var/www/html/に/awstats/index.htmlなどがあると、/etc/httpd/conf.d/awstats.confで設定したAliasとケンカして開くことができない。/awstats2/index.htmlなどに変更しておくとOK。
TOP(HOME)へ