首页 > 代码库 > nginx + awstats 部署记录
nginx + awstats 部署记录
版本:
Tengine/2.2.0 (nginx/1.8.1)
awstats-7.6
一、安装Tengine 与 环境
1、下载安装包,并编译安装
tar -zxvf tengine-2.2.0.tar.gz cd tengine-2.2.0 ./configure --prefix=/usr/local/nginx make make install
2.修改Nginx日志格式为:
注释掉tengine自带的格式
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ ‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" $http_x_forwarded_for‘;
3、安装CPAN
wget http://search.cpan.org/CPAN/authors/id/A/AN/ANDK/CPAN-2.10.tar.gz tar -xf CPAN-2.10.tar.gz cd CPAN-2.10 perl Makefile.PL make make install
4、安装FCGI和FCGI::ProcManager
wget http://search.cpan.org/CPAN/authors/id/B/BO/BOBTFISH/FCGI-ProcManager-0.24.tar.gz tar -xf FCGI-ProcManager-0.24.tar.gz cd FCGI-ProcManager-0.24 perl Makefile.PL make&&make install
4.1 创建FCGI启动文件
将以下内容写入新建 的/usr/local/nginx/sbin/fcgi文件中
[root@ctos5 sbin]# cat /usr/local/nginx/sbin/fcgi
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( "/usr/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"; } } }
赋予可执行权限:
# chmod +x /usr/local/nginx/sbin/fcgi
4.2 启动FPM(FastCGI 进程管理器)
perl /usr/local/nginx/sbin/fcgi >/dev/null 2>$1
二、安装与配置Awstats
1、安装主程序awstats-7.6
wget http://awstats.sourceforge.net/files/awstats-7.6.tar.gz tar -xzvf awstats-7.6.tar.gz mv awstats-7.6 /home/work/ cd /home/work/wstats-7.6 chown -R root.root /usr/local/awstats cd ./tools/ ./awstats_configure.pl
脚本交互1: Config file path (‘none‘ to skip web server setup): 因为在此我们使用的是nginx,所以填写none 脚本交互2: Do you want me to build a new AWStats config/profile file (required if first install) [y/N] 输入Y创建一个新的统计配置文件。 脚本交互3: Your web site, virtual server or profile name: > www.test.com 在这输入自己的网站域名 脚本交互4: Directory path to store config file(s) (Enter for default): > 使用默认配置,生成配置文件 后面的直接按回车就可以
1.1 修改awsatats要分析的nginx日志文件
sed -i ‘s#LogFile="/var/log/httpd/mylog.log"#LogFile="/home/logs/nginx/access_%YYYY-24%MM-24%DD-24.log"#g‘ /etc/awstats/awstats.www.baidu.com.conf
1.2 修改awstats的数据库文件,纯文本格式
sed -i ‘s#DirData="http://www.mamicode.com/var/lib/awstats"#DirData="http://www.mamicode.com/home/work/awstats-7.6/data"#g‘ /etc/awstats/awstats.www.baidu.com.conf
1.3 创建awstats统计结果存放目录
现在我们需要用awstats来生成对日志的统计分析信息出来了;
由于我们用的是支持perl的FCGI动态化访问页面,所以此处我们只需要直接更新数据库即可;
FCGI程序会自动将数据库以动态页面的形式展现出来,无须再手动生成静态页面了;
本处用脚本配合计划任务完成。
# vim awstats_up.sh
#!/bin/bash - /home/work/awstats-7.6/wwwroot/cgi-bin/awstats.pl -update -config=www.baidu.com >/dev/null 2>&1
或手动执行:
/home/work/awstats-7.6/wwwroot/cgi-bin/awstats.pl -update -config=www.baidu.com
nginx + awstats 部署记录
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。