• Nginx下设置运行Awstats

    Posted on 一月 9th, 2009 inetdemon No comments

    还没有安装awstats的先安装配置awstats。由于Nginx目前的稳定版本0.6.34对perl和fastcgi的支持还不是很好。所以有2种方案在nginx下使用awstats. 1. 用awstats静态发布;2. 安装nginx的perl fastcgi。推荐第一种方式。
    无论使用那种方式,首先必须先要把nginx的默认log格式改成和apache的combine格式相同:

    log_format main ‘$remote_addr – $remote_user [$time_local] ‘
    ‘”$request” $status $body_bytes_sent “$http_referer” ‘
    ‘”$http_user_agent” “$http_x_forwarded_for”‘;

    然后在awstats的配置文件中加入以下:

    LogFormat=”%host %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot %otherquot”

    建立logrotate,这个部分我们假设用静态生成的方式

    vi /etc/logrotate.d/nginx

    /usr/local/nginx/logs/access.log {
    daily
    missingok
    rotate 52
    compress
    delaycompress notifempty
    create 644 nginx root sharedscripts
    prerotate
    /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=www.abc.com -dir=/app/public/awstats/data/ -lang=cn -configdir=/usr/local/awstats/wwwroot/cgi-bin/ -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
    #/usr/local/awstats/wwwroot/cgi-bin/awstats.pl --config=www.abc.com -update
    endscript
    postrotate
    if [ -f /var/run/nginx.pid ]; then
    kill -USR1 `cat /var/run/nginx.pid`
    fi
    endscript
    }

    logrotate由cron执行, 需要编辑 /etc/crontab
    将cron.daily的执行时间设置为0 0 * * *分
    使用nginx的cgi配置

    需要先让perl的fastcgi方式运行并在后台侦听socket或端口,nginx的官方wiki中有一篇介绍如何编写这个脚本,其中需要把/opt/nginx/fcgi/cgi.sock修改成/var/run/fcgi.sock。这里很重要的一点是设置/var/run/fcgi.sock,我有一次就是因为这个问题,没有能运行起来

    chown nobody:nobody /var/run/fcgi.sock
    /usr/local/bin/cgiwrap-fcgi.pl &

    如果不能运行,先安装perl的FCGI和FCGI::ProcManager模块
    perl -MCPAN -e 'install FCGI'
    perl -MCPAN -e 'install FCGI::ProcManager'

    如果可以运行了,那perlfastcgi已经在后台运行了,现在配置nginx.conf来支持perl
    location ~ .*\.pl$ { 
            gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped 
            fastcgi_pass  unix:/var/run/fcgi.sock; 
            fastcgi_index awstats.pl; 
            include fastcgi_params; 
            }

    据nginx wiki的说明,现在这段代码,但是还是很ugly,可见还并不stable。英文不好的网管可以看这篇中文翻译,只要按着一步执行就可以了。下面给出完整nginx.conf中awstats的server配置:

    server {
            listen       80;
            server_name  wa.mysite.com;
            access_log   logs/wa.access.log main;
            root /usr/local/awstats/wwwroot/cgi-bin/;
            auth_basic            “Restricted”;
            auth_basic_user_file  sasabrand.pass;

            location / {
                index awstats.pl?config=www.mysite.com;
            }

            location ~ .*\.pl?$ {
                gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped
                fastcgi_pass  unix:/var/run/fcgi.sock;
                fastcgi_index awstats.pl;
                fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_read_timeout 60;
                include        fastcgi_params;
            }

            location ~ .*\.php?$ {
                fastcgi_pass   127.0.0.1:1026;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
        }

    Leave a reply