AWStats
AWStats 通过分析 access log 生成访问数据。
安装
sudo apt install awstats libgeoip-dev php-fpm
在 sudo cpan
的命令行中运行 install Geo::IP
。
配置
AWStats
在 /etc/awstats/awstats.conf.local
中加入配置
LogFile="/var/log/nginx/access.log" LogFormat=1 LoadPlugin="geoip GEOIP_STANDARD /usr/share/GeoIP/GeoIP.dat"
为一个站点建立它的配置,文件为 /etc/awstats/awstats.$HOST.conf
,如 /etc/awstats/awstats.example.com.conf
。在其中加入
Include "/etc/awstats/awstats.conf" LogFile="/usr/share/awstats/tools/logresolvemerge.pl /var/log/nginx/example.com/access.log /var/log/nginx/example.com/access.log.1 |" SiteDomain="wiki.ruo-chen.wang"
Nginx
对应于站点配置中的 log path,在 example.com
的 Nginx 配置中加入
access_log /var/log/nginx/example.com/access.log
目录不存在则要创建
sudo mkdir /var/log/nginx/example.com
将以下内容写入 /etc/nginx/cgi-bin.php
[1]
<?php
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => 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[0]);
$head = fgets($pipes[1]);
while (strcmp($head, "\n")) {
header($head);
$head = fgets($pipes[1]);
}
fpassthru($pipes[1]);
fclose($pipes[1]);
fclose($pipes[2]);
$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");
}
?>
awstats.example.com
的配置:
server {
server_name awstats.example.com;
access_log off;
location ^~ /awstats-icon {
alias /usr/share/awstats/icon/;
}
location ~ ^/([a-z0-9-_\.]+)$ {
return 301 $scheme://awstats.example.com/cgi-bin/awstats.pl?config=$1;
}
location ~ ^/cgi-bin/.*\.(cgi|pl|py|rb) {
gzip off;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_index cgi-bin.php;
fastcgi_param SCRIPT_FILENAME /etc/nginx/cgi-bin.php;
fastcgi_param SCRIPT_NAME /cgi-bin/cgi-bin.php;
fastcgi_param X_SCRIPT_FILENAME /usr/lib/$fastcgi_script_name;
fastcgi_param X_SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REMOTE_USER $remote_user;
}
}
生成数据
sudo /usr/lib/cgi-bin/awstats.pl -config=example.com -update
如果配置正确,数据会生成到 /var/lib/awstats
下。访问 https://awstats.example.com/example.com
即可看到结果。