更改

添加3,648字节 、 2021年4月25日 (日) 02:46
建立内容为“[https://www.awstats.org/ AWStats] 通过分析 access log 生成访问数据。 在 Ubuntu 20.04 上安装 AWStats,使用 Nginx 作为服务器。 ==…”的新页面
[https://www.awstats.org/ AWStats] 通过分析 access log 生成访问数据。

在 [[Ubuntu]] 20.04 上安装 AWStats,使用 [[Nginx]] 作为服务器。

== 安装 ==

sudo apt install awstats libgeoip-dev php-fpm

在 <code>sudo cpan</code> 的命令行中运行 <code><nowiki>install Geo::IP</nowiki></code>。

== 配置 ==

=== AWStats ===

在 <code>/etc/awstats/awstats.conf.local</code> 中加入配置

LogFile="/var/log/nginx/access.log"
LogFormat=1
LoadPlugin="geoip GEOIP_STANDARD /usr/share/GeoIP/GeoIP.dat"

为一个站点建立它的配置,文件为 {{code|bash|/etc/awstats/awstats.$HOST.conf}},如 <code>/etc/awstats/awstats.example.com.conf</code>。在其中加入

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,在 <code>example.com</code> 的 Nginx 配置中加入

access_log /var/log/nginx/example.com/access.log

目录不存在则要创建

sudo mkdir /var/log/nginx/example.com

将以下内容写入 <code>/etc/nginx/cgi-bin.php</code> <ref>[[archwiki:AWStats]]</ref>
<syntaxhighlight lang=php>
<?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");
}
?>
</syntaxhighlight>

<code>awstats.example.com</code> 的配置:
<syntaxhighlight lang=nginx>
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;
}
}
</syntaxhighlight>

== 生成数据 ==

sudo /usr/lib/cgi-bin/awstats.pl -config=example.com -update

如果配置正确,数据会生成到 <code>/var/lib/awstats</code> 下。访问 <code><nowiki>https://awstats.example.com/example.com</nowiki></code> 即可看到结果。

== 外部链接 ==

* [https://larsee.com/blog/2020/06/awstats-on-ubuntu-20-04-with-nginx/ Installing AWStats on Ubuntu 20.04 with Nginx]

[[Category:Server]]

== 参考资料 ==

<references />