用awstats分析apache,nginx日志

1.下载安装awstats

wget http://awstats.sourceforge.net/
tar zxvf awstats-6.9.tar.gz
mv awstats-6.9/  /usr/local/
cd /usr/local/awstats-6.9/tools/

2.配置awstats
perl awstats_configure.pl
-----> 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:
/usr/local/awstats-6.9
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
Config file path ('none' to skip web server setup):
>none
-----> 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: www.mysite.com
Example: demo
Your web site, virtual server or profile name:
>test.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):
>Enter(安默认的,配置文件保存在/etc/awstats)

3.修改/etc/awstats/awstats.test.com.conf
#你要分析的日志文件位置
LogFile="/var/log/apache2/access.log"
#日志的类型1:apache 2:IIS
LogFormat=1
#要分析日志所属的域名
SiteDomain="test.com"
#是否允许在统计页面直接更新数据1:允许 0:不允许
AllowToUpdateStatsFromBrowser=1

4.更新awstats数据库
mkdir /var/lib/awstats/
/usr/local/awstats-6.9/wwwroot/cgi-bin/awstats.pl -config=test.com -update

5.配置apache
ScriptAlias /cgi-bin/ /usr/local/awstats-6.9/wwwroot/cgi-bin/
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all

6.访问http://test.com/cgi-bin/awstats.pl 来查看awstatc统计

7.配置nginx awstats需要Perl脚本支持,但是用的Nginx服务器,对Perl支持不好,于是通过FastCGI方式来使用Perl。首先安装Perl的FCGI模块

wget http://www.cpan.org/modules/by-module/FCGI/FCGI-0.67.tar.gz
tar -zxvf FCGI-0.67.tar.gz
cd FCGI-0.67
perl Makefile.PL
make && make install

安装FCGI-ProcManager

wget http://search.cpan.org/CPAN/authors/id/G/GB/GBJK/FCGI-ProcManager-0.19.tar.gz
tar -xzxf FCGI-ProcManager-0.19.tar.gz
cd FCGI-ProcManager-0.19
perl Makefile.PL
make
make install

Perl的FastCGI启动脚本

vi fcgi_perl

#!/usr/bin/perl -w
use FCGI;
use Socket;
use FCGI::ProcManager;
sub shutdown { FCGI::CloseSocket($socket); exit; }
sub restart { FCGI::CloseSocket($socket); &main; }
use sigtrap 'handler', \&shutdown, 'normal-signals';
use sigtrap 'handler', \&restart,'HUP';
require 'syscall.ph';
use POSIX qw(setsid);

#&daemonize; we don't daemonize when running under runsv
#this keeps the program alive or something after exec'ing perl scripts
END() { }
BEGIN() { }
{
    no warnings;
    *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 {
    #如果使用 IP sockets
    #$socket = FCGI::OpenSocket( "127.0.0.1:8999", 10 );
    #如果使用 UNIX sockets
    #$socket = FCGI::OpenSocket( "/var/run/perl_cgi-dispatch.sock", 10 );

    #foreach $item (keys %ENV) { delete $ENV{$item}; }
    #设置fastcgi进程数,默认四个
    my $n_processes = $ENV{FCGI_NPROCESSES} || 4;
    $proc_manager = FCGI::ProcManager->new( {n_processes => $n_processes} );
    #使用unix socket
    $socket = FCGI::OpenSocket( "$ENV{FCGI_SOCKET_PATH}", 10 );
    #设置Socket权限
    chmod 0777, $ENV{FCGI_SOCKET_PATH};

    #use UNIX sockets - user running this script must have w access to the 'nginx' folder!!
    $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket,&FCGI::FAIL_ACCEPT_ON_INTR );
    $proc_manager->pm_manage();
    if ($request) { request_loop() }
    FCGI::CloseSocket($socket);
}

sub request_loop {
    while ( $request->Accept() >= 0 ) {
        $proc_manager->pm_pre_dispatch();

        #processing any STDIN input from WebServer (for CGI-POST actions)
        $stdin_passthrough = "";
        {
            no warnings;
            $req_len = 0 + $req_params{'CONTENT_LENGTH'};
        };
        if ( ( $req_params{'REQUEST_METHOD'} eq 'POST' ) && ( $req_len != 0 ) ){
            my $bytes_read = 0;
            if ( $bytes_read == 0 ) {
                close(CHILD_RD);
                close(CHILD_ERR);
                print PARENT_WR $stdin_passthrough;
                close(PARENT_WR);
                $rin = $rout = $ein = $eout = "";
                vec( $rin, fileno(CHILD_O), 1 ) = 1;
                vec( $rin, fileno(PARENT_ERR), 1 ) = 1;
                $ein = $rin;
                $nfound = 0;

                while ( $nfound = select( $rout = $rin, undef, $ein = $eout, 10 ) ){
                    die "$!" unless $nfound != -1;
                    $r1 = vec( $rout, fileno(PARENT_ERR), 1 ) == 1;
                    $r2 = vec( $rout, fileno(CHILD_O), 1 ) == 1;
                    $e1 = vec( $eout, fileno(PARENT_ERR), 1 ) == 1;
                    $e2 = vec( $eout, fileno(CHILD_O), 1 ) == 1;

                    if ($r1) {
                        while ( $bytes = read( PARENT_ERR, $errbytes, 4096 ) ) {
                            print STDERR $errbytes;
                        }

                        if ($!) {
                            $err = $!;
                            die $!;
                            vec( $rin, fileno(PARENT_ERR), 1 ) = 0
                            unless ( $err == EINTR or $err == EAGAIN );
                        }
                    }

                    if ($r2) {
                        while ( $bytes = read( CHILD_O, $s, 4096 ) ) {
                            print $s;
                        }

                        if ( !defined($bytes) ) {
                            $err = $!;
                            die $!;
                            vec( $rin, fileno(CHILD_O), 1 ) = 0
                            unless ( $err == EINTR or $err == EAGAIN );
                        }
                    }
                    last if ( $e1 || $e2 );
                }
                close CHILD_RD;
                close PARENT_ERR;
                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(PARENT_ERR);
                close(STDIN);
                close(STDERR);

                #fcntl(CHILD_RD, F_DUPFD, 0);
                syscall( &SYS_dup2, fileno(CHILD_RD), 0 );
                syscall( &SYS_dup2, fileno(CHILD_ERR), 2 );

                #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";
        }
    }
}

再设置启动的脚本

vi startfcgiperl

#!/bin/bash
export FCGI_SOCKET_PATH="/tmp/perl_fcgi.socket"
export FCGI_NPROCESSES=4
/etc/init.d/fcgi_perl > /dev/null 2>&1 &

为Nginx添加FastCGI的Perl支持

vi /usr/local/nginx/conf/nginx.conf

location ~ .*\.pl$
{
fastcgi_index awstats.pl;
include perl.conf;
}

location /awstats {
alias /usr/local/awstats-6.9/wwwroot/cgi-bin/;
}

vi /usr/local/nginx/conf/perl.conf

fastcgi_pass unix:/tmp/perl_fcgi.socket;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param SCRIPT_FILENAME /var/www/awstats-6.8/wwwroot/cgi-bin$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /usr/local/awstats-6.9/wwwroot/cgi-bin/awstats.pl;
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;
让awstats分析nginx的日志

cp /etc/awstats/awstats.test.com.conf /etc/awstats/awstats.test2.com.conf
修改/etc/awstats/awstats.test2.com.conf
#你要分析的日志文件位置
LogFile="/web/logs/access.log"
#日志的类型1:apache 2:IIS
LogFormat=1
#要分析日志所属的域名
SiteDomain="test2.com"
#是否允许在统计页面直接更新数据1:允许 0:不允许
AllowToUpdateStatsFromBrowser=1

访问http://test2.com/awstats/awstats.pl查看统计

发表评论