nginx+awstats安装过程

时间:2023-03-09 20:19:21
nginx+awstats安装过程

awstats来来回回也装了好多遍了,每次都是现装现查,隐约的记得整个配置比较麻烦,中间有几个需要特别注意的地方,又记不得那些需要特殊对待,只能边找资料边回忆,最终还是搞出来了,在此分享给大家。

首先去官网下载最新的安装包。

wget http://jaist.dl.sourceforge.net/project/awstats/AWStats/7.3/awstats-7.3.zip
unzip awstats-7.3.zip
cd awstats-7.3

查看下配置结构

[root@localhost awstats-7.3]# ls
docs README.TXT tools wwwroot
[root@localhost awstats-7.3]# tree -d
.
├── docs
│   ├── images
│   └── scripts
├── tools
│   ├── nginx
│   ├── webmin
│   └── xslt
└── wwwroot
├── cgi-bin
│   ├── lang
│   │   ├── tooltips_f
│   │   ├── tooltips_m
│   │   └── tooltips_w
│   ├── lib
│   └── plugins
│   └── example
├── classes
│   └── src
├── css
├── icon
│   ├── browser
│   ├── clock
│   ├── cpu
│   ├── flags
│   ├── mime
│   ├── os
│   └── other
└── js directories
[root@localhost awstats-7.3]#     

进入tools目录找到awstats_configure.pl 文件并且运行,它是一个向导,可以根据它的指示创建一个配置文件。 

-----> 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 #因为我们这里用的是 Nginx,所以写 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 '/usr/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:
#> www.moabc.net #统计网站的域名 例:www.xxx.com
-----> Define config file path
----> 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:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.moabc.net
#回头把该命令填入crontab 按指定时间执行
Or if you have several config files and prefer having only one command:
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue... 回车继续 A SIMPLE config file has been created: /etc/awstats/awstats.www.moabc.net.conf
#新配置文件所在的路径
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.moabc.net' with command:
> perl awstats.pl -update -config=www.moabc.net
You can also build static report pages for 'www.moabc.net' with command:
> perl awstats.pl -output=pagetype -config=www.moabc.net Press ENTER to finish...
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): #>

回车完成向导,接下来修改 www.xxx.com 的统计配置.

一般情况这个文件的位置是/etc/awstats目录下,文件名为awstats.www.xxx.com.conf。

这里面主要修改几个地方

LogFile="/var/log/httpd/mylog.log"

改为

LogFile="/opt/nginx/logs/access_%YYYY-0%MM-0%DD-0.log

然后保存,后续需要修改的地方咱们一会再说。

接下来就可以生产txt文件了,运行命令

[root@localhost wwwroot]# cd /home/awstats-7.3/wwwroot/cgi-bin/
[root@localhost cgi-bin]# ls
awdownloadcsv.pl awredir.pl awstats-fcgi.php awstats.model.conf awstats.pl lang lib plugins
[root@localhost cgi-bin]# ./awstats.pl -update -config=www.megaunity.com
Create/Update database for config "/etc/awstats/awstats.www.megaunity.com.conf" by AWStats version 7.3 (build 20140126)
From data in log file "/home/log/nginx/access_20140714.log"...
Phase 1 : First bypass old records, searching new record...
Direct access after last parsed record (after line 220965)
Jumped lines in file: 220965
Found 220965 already parsed records.
Parsed lines in file: 0
Found 0 dropped records,
Found 0 comments,
Found 0 blank records,
Found 0 corrupted records,
Found 0 old records,
Found 0 new qualified records.
[root@localhost cgi-bin]#


可以看到我是通过awstats.pl文件生产的数据文件,该文件位置在/var/lib/awstats下面。

[root@localhost cgi-bin]# ls /var/lib/awstats/
awstats072014.www.megaunity.com.txt

这个awstats072014.www.xxx.com.txt就是生产的数据库文件,但是咱们怎么能浏览浏览它呢,我通过很多博客的文章来搭建这个nginx环境下使用awstats,但是我浏览的所有博客都是通过awstats安装目录下一个自带的perl文件awstats_buildstaticpages.pl来生成静态页面浏览,每个博客都互相抄袭,都说nginx对perl支持不好。其实“当年”搭建awstats的前辈可能确实遇到了nginx对perl支持不好的问题,但是这么多年过去了,软件一直在不断更新,awstats已经可以像在apache下那样直接浏览url:http://awstats.cs.yilian.cc/cgi-bin/awstats.pl?config=www.xxx.com。

nginx+awstats安装过程

这是什么?打开看一下?

[root@localhost awstats-7.3]# cat tools/nginx/awstats-nginx.conf
server {
listen 127.0.0.1:;
server_name localhost;
access_log /var/log/nginx/localhost.access_log main;
error_log /var/log/nginx/localhost.error_log info;
root /var/www/localhost/htdocs;
index index.html; # Restrict access
#auth_basic "Restricted";
#auth_basic_user_file /etc/awstats/htpasswd; # Static awstats files: HTML files stored in DOCUMENT_ROOT/awstats/
location /awstats/classes/ {
alias /usr/share/awstats/wwwroot/classes/;
} location /awstats/css/ {
alias /usr/share/awstats/wwwroot/css/;
} location /awstats/icon/ {
alias /usr/share/awstats/wwwroot/icon/;
} location /awstats-icon/ {
alias /usr/share/awstats/wwwroot/icon/;
} location /awstats/js/ {
alias /usr/share/awstats/wwwroot/js/;
} # Dynamic stats.
location ~ ^/cgi-bin/(awredir|awstats)\.pl {
gzip off;
fastcgi_pass 127.0.0.1:;
fastcgi_param SCRIPT_FILENAME /usr/share/awstats/wwwroot/cgi-bin/fcgi.php;
fastcgi_param X_SCRIPT_FILENAME /usr/share/awstats/wwwroot$fastcgi_script_name;
fastcgi_param X_SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
}
[root@localhost awstats-7.3]# cat tools/nginx/awstats-fcgi.php
<?php
$descriptorspec = array(
=> array('pipe', 'r'), // stdin is a pipe that the child will read from
=> array('pipe', 'w'), // stdout is a pipe that the child will write to
=> array('pipe', 'w') // stderr is a file to write to
); $newenv = $_SERVER;
$newenv['SCRIPT_FILENAME'] = $_SERVER['X_SCRIPT_FILENAME'];
$newenv['SCRIPT_NAME'] = $_SERVER['X_SCRIPT_NAME']; if (is_executable($_SERVER['X_SCRIPT_FILENAME'])) {
$process = proc_open($_SERVER['X_SCRIPT_FILENAME'], $descriptorspec, $pipes, NULL, $newenv); if (is_resource($process)) {
fclose($pipes[]);
$head = fgets($pipes[]); while (strcmp($head, "\n")) {
header($head);
$head = fgets($pipes[]);
} fpassthru($pipes[]);
fclose($pipes[]);
fclose($pipes[]); $return_value = proc_close($process);
} else {
header('Status: 500 Internal Server Error');
echo('Internal Server Error');
}
} else {
header('Status: 404 Page Not Found');
echo('Page Not Found');
}

我看到了一个php文件和一个nginx配置文件,不用说,很明显是模板。我想如果不支持为什么要给模板呢。我打开了配置文件看了一下,发现awstats把fcgi给了9000端口,这不就是php-fpm么?我突然觉的,是不是nginx不能像apache那样直接处理perl但是他能处理php啊,这个php文件就应该就是实现了awstats.pl一样的功能。

修改了fastcgi_param SCRIPT_FILENAME上下两行的awstats-fcgi.php的位置,并把它拷贝到cgi-bin/目录下,配置文件复制到相应的nginx配置文件目录下。重载nginx,访问下试试。

你会发现可以通过http://awstats.cs.yilian.cc/cgi-bin/awstats.pl?config=www.xxx.com这种形式正常访问了对不对。

配置文件的调整

以往我们都是通过awstats_configure.pl想到去创建配置文件,殊不知我们可以通过模板文件去拷贝然后使用include来引出模板配置文件,如我的配置文件如下

[root@localhost awstats]# ls
awstats.www.xxx.com.conf awstats.www.xxx.com.conf awstats.xxx.cc.conf common.conf

common.conf就是我的模板文件。

[root@localhost awstats]# cat awstats.www.xxx.com.conf
Include "common.conf"
LogFile="/home/log/nginx/access_%YYYY-0%MM-0%DD-24.log"
SiteDomain="www.xxx.com"
DirData="/var/lib/awstats"
[root@localhost awstats]#

这样方便管理 每个配置文件也不大。

修改模板配置,是awstats中文乱码恢复。

通过网上的文章介绍需要修改配置文件选项:#LoadPlugin="decodeutfkeys",打开该选项取消注释。

但是会发生报错:

Error: Plugin load for plugin 'decodeutfkeys' failed with return code: Error: Can't locate URI/Escape.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.1 /usr/local/share/perl/5.10.1 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl . /usr/share/awstats/lib /usr/share/awstats/plugins) at (eval 3) line 1.
Setup ('/etc/awstats/awstats.bbsnuaa.conf' file, web server or permissions) may be wrong.
Check config file, permissions and AWStats documentation (in 'docs' directory).
wget http://search.cpan.org/CPAN/authors/id/G/GA/GAAS/URI-1.36.tar.gz

解压缩文件
tar zxvf URI-1.36.tar.gz 进入解压缩后目录
cd URI-1.36 然后 perl Makefile.PL
make
make install

报错继续,然后使用另一种方法

安装URI/Escape模块

一种是通过perl shell网络安装:

[root@localhost cgi-bin]# perl -MCPAN -e 'install Encode'
Can't locate CPAN.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).
BEGIN failed--compilation aborted.

缺少CPNA,然后下载CPNA

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

tar -zxvf CPAN-1.9600.tar.gz

cd CPAN-1.9600

perl Makefile.PL

[root@localhost URI-1.60]# perl Makefile.PL
Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at Makefile.PL line 2.

还是报错,前前后后发现这个perl太费劲了,少了好多东西,没办法只好一个个安装。

yum install perl-Time-HiRes

最后执行以下三部恢复。

perl -MCPAN -e 'install Encode'
perl -MCPAN -e 'install URI::Escape'

root@server ~]# perl -MCPAN -e shell

>install URI::Escape
解决乱码问题。

安装QQ纯真地址库

通过车东的博客学习到了安装QQ地址库的方法,但是安装过程相当惨烈啊。

安装如下:

把 qqhostinfo.pm,QQWry.dat,qqwry.pl三个文件拷贝到/home/awstats-7.3/wwwroot/cgi-bin/plugins下面
然后修改qqwry.pl文件,把里面的路径修改正确。
my $ipfile="${DIR}/plugins/qqwry.dat"
注意${DIR}指定路径是什么,对应了的安装路径。
然后再执行 vim defualt.conf(之前的模板配置文件)
添加:LoadPlugin="qqhostinfo"

到此QQ地址库算是安装完成了。

安装国家地理插件

 因为我之前安装过,所以我就之前现在配置文件添加了插件信息。
所以我还是直接修改模板配置文件default.conf添加插件信息
LoadPlugin="geoip GEOIP_STANDARD /home/awstats-7.3/wwwroot/cgi-bin/GeoIP.dat"
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /home/awstats-7.3/wwwroot/cgi-bin/GeoLiteCity.dat"
这里我参考了http://blog.****.net/kumu_linux/article/details/8146958的博客
需要的安装包如下:
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
然后把两个文件移入/home/awstats-7.3/wwwroot/cgi-bin目录中gunzip解压即可。

GeoIP安装步骤如下:

先下载GeoIP C库:

去https://www.maxmind.com/download/geoip/api/c/下载GeoIP-1.4.8.tar.gz

# tar xf GeoIP-1.4.8.tar.gz

cd进入解压目录,执行如下操作安装

# ./configure ; make && make install 

接下来下载GeoIP perl库:

去https://www.maxmind.com/download/geoip/api/perl/下载Geo-IP-1.40.tar.gz

# tar xf Geo-IP-1.40.tar.gz

cd 进入解压目录,执行如下操作安装

perl Makefile.PL LIBS='-L/usr/local/lib' 

//这里必需使用这种方法安装,笔者尝试把/usr/local/lib加入/etc/ld.so.conf.d/geoip.conf并执行ldconfig,单独执行perl Makefile.PL还是发现不了GeoIP的C库,所以只能以perl Makefile.PL LIBS='-L/usr/local/lib' 这种方式执行

# make && make install

 rm -rf /var/lib/awstats/* #删除旧的统计数据

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

重新生成数据即可,查看监控页面就可以获取来访者的国家和地区了

这里面主要容易犯的错误就是发现不了GeoIP的C库,啥事GEOIP的C库 就是之前安装的GeoIP-1.4.8.tar.gz 这个东西,这个东西安装之后你会发现如下目录里面会多出东西

 [root@localhost plugins]# ls /usr/local/lib/
libGeoIP.a libGeoIP.la libGeoIP.so libGeoIP.so. libGeoIP.so.1.4. libGeoIPUpdate.a libGeoIPUpdate.la libGeoIPUpdate.so libGeoIPUpdate.so. libGeoIPUpdate.so.0.0.
[root@localhost plugins]#
这些文件就是正确安装了C库出来的东西,如果没有 那么说明你C库安装不成功 ,建议查看是不是服务器少了支持perl的包,或者gcc这些基础的东西。

  参考文档:http://blog.****.net/kumu_linux/article/details/8146958

       http://www.chedong.com/tech/awstats.html