Nginx环境Awstats安装并建立站点查看统计数据 开启GeoIP地理位置支持

一、获取awstat
wget https://prdownloads.sourceforge.net/awstats/awstats-7.5.tar.gz

二、解压并放置awstats
tar -zxf awstats-7.5.tar.gz && mv awstats-7.5 /usr/local/awstats

三、配置awstats
cd /usr/local/awstats/ && perl tools/awstats_configure.pl

##########
#……..
#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

##########
#…….
#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:
##########

> www.chinulture.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

##########
#—–> Create config file ‘/etc/awstats/awstats.www.chinulture.com.conf’
# Config file /etc/awstats/awstats.www.chinulture.com.conf created.
#
#—–> Restart Web server with ‘/sbin/service httpd restart’
#httpd: unrecognized service
#
#—–> 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.chinulture.com
#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…
##########

# 继续回车,配置完毕
# 接下来自定义我们的配置文件

vi /etc/awstats/awstats.www.chinulture.com.conf

修改日志文件路径,找到LogFile=”/var/log/httpd/mylog.log”,修改为

LogFile=”/home/wwwlogs/%YYYY-24_%MM-24_%DD-24/www.chinulture.com.log”

其中%YYYY-24_%MM-24_%DD-24表示逐日nginx所生成的日志目录。这里要求我们按日分割nginx网站生成的
日志,正文会讲到。
如果日志文件有多个件
需要使用/usr/local/awstats/tools/logresolvemerge.pl脚本指定你需要的所有日志文件,然后再文本最后加入一个字符
“|”,表示合并所有文件结果。

LogFile=”/usr/local/awstats/tools/logresolvemerge.pl /home/wwwlogs/%YYYY-24_%MM-24_%DD-24/xxx.access.log
/home/wwwlogs/%YYYY-24_%MM-24_%DD-24/yyy.access.log|”

指定计算结果目录
系统计算出的结果是需要保存在磁盘上的,如果自定义这个路径呢?
在配置文件line 203附近,你可以找到系统的默认配置。

DirData=”/home/wwwroot/default/awstats”

# 然后我们需要对这个路径的权限做一些处理,此目录是awstats默认的查看统计数据的网页根目录,所以我们必
须执行以下操作

cd /home/wwwroot/ && mkdir awstats && chown -R www:www awstats

网页端刷新数据
如果需要在网页里能够主动刷新数据,请找到约line 239,并修改内容。
主动刷新数据

# modify
AllowToUpdateStatsFromBrowser=1
四、开启地理位置支持

如果想得到如下结果,需要安装并开启GEO支持。
地理位置支持
# 安装GEO支持

方法一:
cd ~/ && yum install GeoIP GeoIP-data GeoIP-devel perl-Geo-IP -y

方法二:
/**********
cd ~/ && wget http://www.maxmind.com/download/geoip/api/c/GeoIP-1.4.8.tar.gz

tar zxf GeoIP-1.4.8.tar.gz && cd GeoIP-1.4.8 && ./configure –prefix=/usr/local/GeoIP && make && make install

由于笔者编译GeoIP时自定义了安装路径,所以这里需要加载关联GeoIP模块库
# echo ‘/usr/local/GeoIP/lib’ >> /etc/ld.so.conf && /sbin/ldconfig /etc/ld.so.conf

再安装Geo-IP perl库
# wget http://www.maxmind.com/download/geoip/api/perl/Geo-IP-1.27.tar.gz && tar zxf Geo-IP-1.27.tar.gz && cd
Geo-IP-1.27
关联GeoIP库文件:
# perl Makefile.PL LIBS=’-L/usr/local/GeoIP/lib’ INC=’-I/usr/local/GeoIP/include’
make && make install

再安装Geo-IPfree
wget http://search.cpan.org/CPAN/authors/id/G/GM/GMPASSOS/Geo-IPfree-0.2.tar.gz && tar -zxvf Geo-IPfree-
0.2.tar.gz && cd Geo-IPfree-0.2 && perl Makefile.PL
make && make install

下载MaxMind免费提供的GeoIP和GeoIPCityLite数据包:
cd /etc/awstats/ && wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz && wget
http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz && wget
http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz

gzip -d GeoLiteCity.dat.gz && gzip -d GeoIP.dat.gz && gzip -d GeoIPASNum.dat.gz

数据包还可以利用crontab任务按日更新,每月更新一次
另外,从本地找到GeoIPISP.dat并上传到/etc/awstats/
vi /home/crontab/geoipdatup.sh ,并添加以下内容
wget -c http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz –directory-prefix=/etc/awstats/
&& wget -c http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz –directory-
prefix=/etc/awstats/ && wget -c http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz
–directory-prefix=/etc/awstats/
cd /etc/awstats/ && gzip -d GeoLiteCity.dat.gz && gzip -d GeoIP.dat.gz && gzip -d GeoIPASNum.dat.gz
再crontab -e ,添加以下内容
5 0 1 * * /home/crontab/geoipdatup.sh #每月1日凌晨5分更新
然后执行:service crond restart

配置awstats加载归属地数据包
# 安装完毕后修改配置文件vi /etc/awstats/awstats.www.chinulture.com.conf约 line 1429
LoadPlugin=”geoip GEOIP_STANDARD /etc/awstats/GeoIP.dat”
LoadPlugin=”geoip_city_maxmind GEOIP_STANDARD /etc/awstats/GeoLiteCity.dat”
LoadPlugin=”geoip_isp_maxmind GEOIP_STANDARD /etc/awstats/GeoIPISP.dat”
LoadPlugin=”geoip_asn_maxmind GEOIP_STANDARD /etc/awstats/GeoIPASNum.dat”

**********/

五、解决中文乱码,生成数据并测试

如果安装AWS后搜索处中文乱码,请修改vi /etc/awstats/awstats.www.chinulture.com.conf约line 1204,删除注释。
LoadPlugin=”decodeutfkeys”

生成数据并测试
perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -config=www.chinulture.com
生成Awstats统计结果静态页面
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=guoxuesg.com -lang=cn –
dir=/home/wwwroot/default/awstats/ -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
说明:
/usr/local/awstats/tools/awstats_buildstaticpages.pl #Awstats 静态页面生成工具
-update -config=www.chinulture.com #更新配置项,指定配置文件
-lang=cn #语言为中文
-dir= #指定静态页面的存储位置
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl #Awstats日志更新程序路径
六、创建虚拟机,随时查看并更新统计报表
cp /usr/local/awstats/tools/nginx/awstats-fcgi.php /usr/local/awstats/wwwroot/cgi-bin/fcgi.php
执行lnmp vhost add ,添加查看并更新统计数据的虚机:aws.chinulture.com
vi /usr/local/awstats/tools/nginx/awstats-nginx.conf 复制并修改为以下内容,代替nginx下aws.chinulture.com.conf的内容:

server
{
listen 80;
#listen [::]:80;
server_name aws.blog.chinulture.com;
index index.htm;
#access_log /home/wwwlogs/aws.blog.chinulture.com.access_log main;
error_log /home/wwwlogs/aws.blog.chinulture.com.error_log info;
root /home/wwwroot/default/awstats;

# Restrict access
#auth_basic “Restricted”;
#auth_basic_user_file /etc/awstats/htpasswd;

#Rewrite
location ~ ^/([a-z0-9-_\.]+)$ {
return 301 $scheme://aws.chinulture.com/cgi-bin/awstats.pl?config=$1;
}

# Dynamic stats.
location ~ ^/cgi-bin/(awredir|awstats)\.pl {
gzip off;
include fastcgi_params;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_param SCRIPT_FILENAME /usr/local/awstats/wwwroot/cgi-bin/fcgi.php;
fastcgi_param SCRIPT_NAME /cgi-bin/fcgi.php;
fastcgi_param X_SCRIPT_FILENAME /usr/local/awstats/wwwroot$fastcgi_script_name;
fastcgi_param X_SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REMOTE_USER $remote_user;
}

location /classes/ {
alias /usr/local/awstats/wwwroot/classes/;
}

location /css/ {
alias /usr/local/awstats/wwwroot/css/;
}

location /icon/ {
alias /usr/local/awstats/wwwroot/icon/;
}

location /awstatsicons/ {
alias /usr/local/awstats/wwwroot/icon/;
}

location /js/ {
alias /usr/local/awstats/wwwroot/js/;
}

access_log /home/wwwlogs/aws.blog.chinulture.com.log access;
}

如果出现403\502或500错误,可能是函数proc_open()被禁导致。vi /usr/local/php/etc/php.ini

七、添加其他网站统计
从第三步开始,执行到第五步的修改乱码。

發佈留言