nginx+awstats+geoip详细步骤

nginx+awstats+geoip详细步骤

nginx+awstats+geoip详细步骤

来源:http://bbs.s108.com
转载请注明出处.

参考了以下文章:
牛博网友博客:http://www.bullog.cn/blogs/shunz/archives/157311.aspx
车东博客:http://www.chedong.com/tech/awstats.html
awstats官网文档:http://awstats.sourceforge.net/docs/awstats_setup.html
无名氏博客:http://www.structure28.com/blog/ ... and-logrotated.html
nginx官方wiki:http://wiki.codemongers.com/NginxSimpleCGI
刘晖博客:http://www.anywolfs.com/liuhui/article.asp?id=242

1.awstats的安装:
参考官方文档:http://awstats.sourceforge.net/docs/awstats_setup.html
需要注意的是icon路径的设置问题,在nginx.conf和awstats.mysite.conf里的要能对应上.

2.GeoIP安装:
先下载安装C库:GeoIP C解包后.下载地址:http://www.maxmind.com/download/geoip/api/c/
%./configure; make
#make install
然后下载Perl库:GeoIP Perl解包后.下载地址:http://www.maxmind.com/download/geoip/api/perl/
%perl MakeFile.PL; make
#make install
然后
perl -MCPAN -e 'install "Geo::IP"'
perl -MCPAN -e 'install "Geo::IPfree"'

下载GeoIP/GeoIPCityLite包:解包并部署到awstats目录下
http://www.maxmind.com/download/geoip/d ... ity.dat.gz
http://www.maxmind.com/download/geoip/d ... oIP.dat.gz

修改/etc/awstats/awstats.mysite.conf
LoadPlugin="decodeutfkeys" //中文spider统计
LoadPlugin="geoip GEOIP_STANDARD /PATH/TO/GeoIP.dat"
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /PATH/TO/GeoLiteCity.dat"

3.使其能分析nginx的日志:
1)设置nginx.conf的日志格式如下:
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

2)在/etc/awstats/awstats.mysite.conf里设置log格式如下(默认值是1):
LogFormat="%host %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot %otherquot"

3)新建一个日志回滚,vi /etc/logrotate.d/nginx (路径根据操作系统不同而变化)
内容如下:
/var/log/nginx/access.log {
daily
missingok
rotate 52
compress
delaycompress notifempty
create 644 nginx root sharedscripts
prerotate
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl --config=www.mysite.com -update
endscript
postrotate
if [ -f /opt/nginx/logs/nginx.pid ]; then
kill -USR1 `cat /opt/nginx/logs/nginx.pid`
fi
endscript
}
4)安装perl 的FCGI支持.
perl -MCPAN -e 'install FCGI'
perl -MCPAN -e 'install FCGI:rocManager'
或者安装下面的步骤手动安装.
wget http://www.cpan.org/modules/by-module/F ... .67.tar.gz
tar zxvf FCGI-0.67.tar.gz
cd FCGI-0.67
perl Makefile.PL
make
make install
然后perl -MCPAN -e 'install FCGI:rocManager'

5)建立一个脚本,注意里面的有一行路径设置,后面要和nginx.conf里的对应起来:
$socket = FCGI::OpenSocket( "/opt/nginx/fcgi/cgi.sock", 10 )

#!/usr/bin/perl -w
use FCGI;
use Socket;
use FCGI:rocManager;
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 {

#$socket = FCGI::OpenSocket( "127.0.0.1:8999", 10 ); #use IP sockets
#$socket = FCGI::OpenSocket( "/var/run/nginx/perl_cgi-dispatch.sock", 10 ); #use UNIX sockets - user running this script must have w access to the 'nginx' folder!!
#foreach $item (keys %ENV) { delete $ENV{$item}; }
$proc_manager = FCGI:rocManager->new( {n_processes => 5} );
$socket = FCGI::OpenSocket( "/opt/nginx/fcgi/cgi.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,
&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;
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 );
pipe( PARENT_ERR, CHILD_ERR );
my $pid = open( CHILD_O, "-|" );
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;
}
$oldfh = select(PARENT_ERR);
$| = 1;
select(CHILD_O);
$| = 1;
select($oldfh);
if ( $pid > 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";
}
}
}
6)修改nginx.conf,使其支持cgi.
http {
root /var/www/htdocs;
index index.html;
location ~ .*\.pl$ {
gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped
fastcgi_pass unix:/var/run/nginx/cgiwrap-dispatch.sock;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME /var/www/cgi-bin$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;
}
}
修改完重启nginx.

另外还需要建个虚拟路径到awstats的wwwroot,这样就可以在浏览器里查看awstats统计信息了.
GeoIP和GeoIPfree并不能同时使用,另外我觉得qqhostinfo(QQ纯真IP数据库)更好.对国内ip定位非常准确.

PS:笑脸太多.建议禁用Smilies
去掉simles,不然代码、脚本很乱。谢谢
好帖子~收藏了~谢谢LZ分享~
有点乱,禁用 Smilies吧