CentOS 6.4 利用 Awstats 7.2 分析 Nginx 日誌

丶小炒肉發表於2016-05-31

一、配置Nginx 日誌分割 (略)

 

 

 

二、配置FCGI

 

1、安裝CPAN

 

wget http://search.cpan.org/CPAN/authors/id/A/AN/ANDK/CPAN-2.00.tar.gz

tar zxf CPAN-2.00.tar.gz

cd CPAN-2.00

perl Makefile.PL

make && make install

 

 

 

2、安裝FCGI和FCGI::ProcManager

 

wget http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/FCGI-0.74.tar.gz

tar zxf FCGI-0.74.tar.gz

cd FCGI-0.74

第一種安裝方法:perl -MCPAN -e 'install FCGI'

第二種安裝方法:perl Makefile.PL

make&&make install

 

wget http://search.cpan.org/CPAN/authors/id/B/BO/BOBTFISH/FCGI-ProcManager-0.24.tar.gz

tar zxf FCGI-ProcManager-0.24.tar.gz

cd FCGI-ProcManager-0.24

 

第一種安裝方法:perl -MCPAN -e 'install FCGI::ProcManager'

第二種安裝方法:perl Makefile.PL

make&&make install

 

 

 

 

3、建立FCGI啟動檔案

 

vi /opt/local/nginx/sbin/fcgi

 

 

 

#!/usr/bin/perl

use FCGI;

#perl -MCPAN -e 'install FCGI'

use Socket;

use POSIX qw(setsid);

#use Fcntl;

require 'syscall.ph';

&daemonize;

#this keeps the program alive or something after exec'ing perl scripts

END() { } BEGIN() { }

*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; };

eval q{exit};

if ($@) {

exit unless $@ =~ /^fakeexit/;

};

&main;

sub daemonize() {

chdir '/' or die "Can't chdir to /: $!";

defined(my $pid = fork) or die "Can't fork: $!";

exit if $pid;

setsid or die "Can't start a new session: $!";

umask 0;

}

sub main {

#$socket = FCGI::OpenSocket( "127.0.0.1:8999", 10 );

$socket = FCGI::OpenSocket( "/opt/local/nginx/fastcgi_temp/perl_cgi-dispatch.sock", 10 );

#use UNIX sockets - user running this script must have w access to the 'nginx' folder!!

$request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );

if ($request) { request_loop()};

FCGI::CloseSocket( $socket );

}

sub request_loop {

while( $request->Accept() >= 0 ) {

#processing any STDIN input from WebServer (for CGI-POST actions)

$stdin_passthrough ='';

$req_len = 0 + $req_params{'CONTENT_LENGTH'};

if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != 0) ){

my $bytes_read = 0;

while ($bytes_read < $req_len) {

my $data = '';

my $bytes = read(STDIN, $data, ($req_len - $bytes_read));

last if ($bytes == 0 || !defined($bytes));

$stdin_passthrough .= $data;

$bytes_read += $bytes;

}

}

#running the cgi app

if ( (-x $req_params{SCRIPT_FILENAME}) && #can I execute this?

(-s $req_params{SCRIPT_FILENAME}) && #Is this file empty?

(-r $req_params{SCRIPT_FILENAME}) #can I read this file?

){

pipe(CHILD_RD, PARENT_WR);

my $pid = open(KID_TO_READ, "-|");

unless(defined($pid)) {

print("Content-type: text/plain\r\n\r\n");

print "Error: CGI app returned no output - Executing $req_params

{SCRIPT_FILENAME} failed !\n";

next;

}

if ($pid > 0) {

close(CHILD_RD);

print PARENT_WR $stdin_passthrough;

close(PARENT_WR);

while(my $s = <KID_TO_READ>) { print $s; }

close KID_TO_READ;

waitpid($pid, 0);

} else {

foreach $key ( keys %req_params){

$ENV{$key} = $req_params{$key};

}

# cd to the script's local directory

if ($req_params{SCRIPT_FILENAME} =~ /^(.*)\/[^\/]+$/) {

chdir $1;

}

close(PARENT_WR);

close(STDIN);

#fcntl(CHILD_RD, F_DUPFD, 0);

syscall(&SYS_dup2, fileno(CHILD_RD), 0);

#open(STDIN, "<&CHILD_RD");

exec($req_params{SCRIPT_FILENAME});

die("exec failed");

}

}

else {

print("Content-type: text/plain\r\n\r\n");

print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is

not executable by this process.\n";

}

}

}

 

 

建立完成後,需要賦予fcgi執行許可權:

 

chmod 755 /opt/local/nginx/sbin/fcgi

 

 

啟動FPM(FastCGI 程式管理器)

 

perl /opt/local/nginx/sbin/fcgi

 

(執行出錯,Can't locate sys/syscall.ph in @INC (did you run h2ph?) (@INC contains: /usr/lib/perl5/i686-linux…… )

執行 cd /usr/include

h2ph *.h */*.h

 

 

 

 

 

 

 

 

 

 

 

 

 

[root@localhost tools]# ./awstats_configure.pl

 

----- AWStats awstats_configure 1.0 (build 1.9) (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

Warning: AWStats standard directory on Linux OS is '/usr/local/awstats'.

If you want to use standard directory, you should first move all content

of AWStats distribution from current directory:

/opt/local/awstats

to standard directory:

/usr/local/awstats

And then, run configure.pl from this location.

Do you want to continue setup from this NON standard directory [yN] ? y 非預設配置路徑 是否 繼續

 

-----> 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 是否選擇這幾個配置檔案 Nginx 選 none 為否

Config file path ('none' to skip web server setup):

> none

 

Your web server config file(s) could not be found.

You will need to setup your web server manually to declare AWStats

script as a CGI, if you want to build reports dynamically.

See AWStats setup documentation (file docs/index.html)

 

-----> Update model config file '/opt/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 是否建立配置檔案 選擇 y

 

-----> 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: 網站域名

> aws.qq.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): 配置檔案儲存路徑

> /opt/local/awstats/

 

-----> Create config file '/opt/local/awstats//awstats.aws.qq.com.conf'

Config file /opt/local/awstats//awstats.aws.qq.com.conf created.

 

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

/opt/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=aws.qq.com

Or if you have several config files and prefer having only one command:

/opt/local/awstats/tools/awstats_updateall.pl now

Press ENTER to continue...

 

 

A SIMPLE config file has been created: /opt/local/awstats//awstats.aws.qq.com.conf

You should have a look inside to check and change manually main parameters.

You can then manually update your statistics for 'aws.qq.com' with command:

> perl awstats.pl -update -config=aws.qq.com

You can also build static report pages for 'aws.qq.com' with command:

> perl awstats.pl -output=pagetype -config=aws.qq.com

 

Press ENTER to finish...

 

 

 

 

 

 

 

 

 

修改配置檔案 vi /opt/local/awstats/awstats.aws.qq.com.conf

 

LogFile 多臺可用 logresolvemerge.pl 合併多個日誌檔案 配合萬用字元

 

LogFile="/opt/htdocs/upload/mxaws/logresolvemerge.pl /data/access_*.log |"

 

 

DirData 配置日誌分析記錄

 

DirData"/opt/local/awstats/data"

 

 

配置 更新按鈕

 

AllowToUpdateStatsFromBrowser=0 修改為 AllowToUpdateStatsFromBrowser=1

 

 

 

 

 

配置指令碼,然後生成awstats統計資訊資料庫

 

vi awstats_up.sh

 

#!/bin/sh

/opt/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=/opt/local/awstats/awstats.aws.qq.com.conf >/dev/null 2>&1

 

 

 

四、配置日誌分析頁面的來訪ip的地址位置顯示 (略)

 

這裡我們用國內最準確的ip資料庫——QQ純真庫我在部落格附件裡面放了這個工具,大家下載之後通過CRT用lrzsz工具傳上去,具體步驟這裡不寫了。

 

附件裡面有三個檔案qqhostinfo.pm,qqwry.pl和QQWry.Dat,我們將這三個檔案統統都放到/opt/local/awstats/wwwroot/cgi-bin/plugins中。

 

接下來,我們修改qqwry.pl檔案,將./QQWry.Dat修改為${DIR}/plugins/QQWry.Dat

 

修改配置檔案

 

將LoadPlugin="hostinfo"替換為LoadPlugin="qqhostinfo"即可。

 

 

五、配置nginx

 

server {

listen 80;

server_name aws.qq.com;

location / {

root /www/sunsky;

index index.html index.htm;

access_log /app/logs/www_access.log main;

}

location ~* ^/cgi-bin/.*\.pl$ {

root /usr/local/awstats/wwwroot;

fastcgi_pass unix:/usr/local/nginx/fastcgi_temp/perl_cgi-dispatch.sock;

fastcgi_index index.pl;

include fastcgi_params;

charset gb2312;

auth_basic "Restricted"; #有些網站不願意公開網站流量資訊,所以加個認證

auth_basic_user_file /usr/local/nginx/htpasswd.pass; #該檔案由apache的加密認證工具htpasswd建立

}

location ~ ^/icon/ {

root /usr/local/awstats/wwwroot/;

index index.html;

access_log off;

error_log off;

charset gb2312;

}

}

 

 

 

為了保持nginx.conf主配置檔案更加整潔乾淨,所以我們將fastcgi_param的一系列引數新增

 

vi /opt/local/nginx/conf/fastcgi_params

 

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param QUERY_STRING $query_string;

fastcgi_param REQUEST_METHOD $request_method;

fastcgi_param CONTENT_TYPE $content_type;

fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

fastcgi_param REQUEST_URI $request_uri;

fastcgi_param DOCUMENT_URI $document_uri;

fastcgi_param DOCUMENT_ROOT $document_root;

fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param REMOTE_ADDR $remote_addr;

fastcgi_param REMOTE_PORT $remote_port;

fastcgi_param SERVER_ADDR $server_addr;

fastcgi_param SERVER_PORT $server_port;

fastcgi_param SERVER_NAME $server_name;

fastcgi_read_timeout 60;

 

 

針對上面的加密,由於nginx沒有好的加密認證工具,需要藉助apache的htpasswd來實現加密認證功能:

 

 

htpasswd -c -m /opt/local/nginx/htpasswd.pass jicki

 

 

無法檢視icon 是~許可權問題! chmod 授權訪問目錄

 

 

瀏覽器輸入 http://myurl/cgi-bin/awstats.pl?config=myurl

 

相關文章