Trước đây 2012-2013, mình hay dùng Apache cho web server nên có sử dụng awstats để thống kê số liệu như: Visit, Hits, Bandwidth, … cho từng ngày, tháng, năm.

Vì xem AWStats thống kê rất trực quan, nên tiện có dự án về Apache web server, hôm nay viết tiếp bài đơn giản liên quan phần thông kê cho Apache
AWStats là công cụ miễn phí:
– Dùng để phân tích log và xuất thông tin về web, streaming, ftp or mail server.
– Nó làm việc như CGI hoặc từ dòng lệnh và hiển thị tất cả các thông tin mà nó đọc từ tệp log ra dạng graphic trên trang web.
Tôi cũng không biết thực sự AWStats phân tích được tệp log lớn cỡ nào, còn những số liệu như hình trên thì nó làm việc một cách bình thường.
AWStats hỗ trợ phân tích các định dạng log (LogFormat) sau:
# 1 – Apache or Lotus Notes/Domino native combined log format (NCSA combined/XLF/ELF log format)
# 2 – IIS or ISA format (IIS W3C log format). See FAQ-COM115 For ISA.
# 3 – Webstar native log format.
# 4 – Apache or Squid native common log format (NCSA common/CLF log format)
Hôm nay, chúng ta sẽ sử dụng awstats to phân tích log cho Apache web server phiên bản Apache-2.4.6
Các bước cài đặt và cấu hình AWStats như sau trên CentOS 7:
Step1: Setup awstats
Download awstats bản mới nhất tại: https://www.awstats.org/
cd /opt/
wget https://prdownloads.sourceforge.net/awstats/awstats-7.7.tar.gz
tar zxvf awstats-7.7.tar.gz
mv awstats-7.7 /usr/local/awstats
#Chạy tệp cài đặt awstats
perl 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
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
—–> Check and complete web server config file ‘/etc/httpd/conf/httpd.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 ‘<Directory>’ 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: http://www.mysite.com
Example: demo
Your web site, virtual server or profile name:
> example.local
—–> 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.example.local.conf’
Config file /etc/awstats/awstats.example.local.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=example.local
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.example.local.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for ‘example.local’ with command:
> perl awstats.pl -update -config=example.local
You can also read your statistics for ‘example.local’ with URL:
> http://localhost/awstats/awstats.pl?config=example.local
Press ENTER to finish…
Ở trên chúng ta vào một số thông tin sau:
– Chỉ đường dẫn tệp tin cấu hình chính Apache (/etc/httpd/conf/httpd.conf)
– Thông tin domain sẽ cần để phân tích (cái này sau có thể phân tích thêm nhiều domain, virtualhost)
Step2: Thiết lập cấu hình cho awstats
Sửa một vài thông tin trong tệp awstats.example.local.conf với nội dung sau:
vim /etc/awstats/awstats.example.local.conf
LogFile="/var/www/example.local/logs/access.log #log path mà awstats sẽ phân tích
SiteDomain="example.local" #Tên domain
LogFormat=1 #Phân tích log cho định dạng Apache hỗ trợ NCSA Combined
mkdir /var/lib/awstats
chown -R apache:apache /var/lib/awstats
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=example.local
khi chạy awstats.pl như trên quá trình phân tích log diễn ra như sau:
Create/Update database for config “/etc/awstats/awstats.example.local.conf” by AWStats version 7.7 (build 20180105)
From data in log file “/var/www/example.local/logs/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: 218092
Found 180 corrupted records,
Found 217912 new qualified records.
####Sửa đổi cấu hình httpd.conf
Hiện tại awstat vẫn đang thiết lập cấu hình cho bản Apache-2.2.x về trước, do đó chúng ta sửa đổi nội dung dưới đây để phù hợp với bản Apache 2.4
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 ip 192.168.10.186
</Directory>
Step3: Đặt lịch để chạy script
Nếu muốn xem số liệu thủ công thì mỗi lần vào server và chạy lệnh sau:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=example.local
Tất nhiên thực tế ta sẽ đặt lịch để AWStats tự phân tích logs, việc của mình chỉ là xem qua màn hình số liệu graphic.
crontab -e
00 01 * * * /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=example.local
Step4: Truy cập Apache web server statistics
http://192.168.10.111:9090/awstats/awstats.pl?config=example.local

Like this:
Số lượt thích Đang tải...