如何在Fedora 28上安装AWStats
圣地学子
・9 分钟阅读
AWStats是分析网络流量的有用工具,本教程将指导你,在Fedora28上安装、配置和nginx web服务器的AWStats 。
如果还没有安装Nginx,请执行以下操作:
sudo dnf install nginx
sudo systemctl enable --now nginx
安装AWStats和工具
首先,你需要安装一些东西,幸运的是,这非常简单,因为你所需要的所有软件都在Fedora的存储库中:
sudo dnf install awstats httpd-tools php-fpm
httpd-tools
是一个包含一些我们需要的工具的软件包,如htpasswd
,它设计用于Apache,但是也可以与Nginx一起使用,另一个工具php-fpm
允许我们从Nginx运行PHP脚本。
我们需要通过systemctl
启用php-fpm
:
sudo systemctl enable --now php-fpm
为你的站点配置AWStats
在使用AWStats之前,我们需要配置它,复制配置文件模板,为你的站点创建一个新配置:
sudo cp /etc/awstats/awstats.model.conf /etc/awstats/awstats.<yoursitename>.conf
现在编辑该文件:
sudoedit /etc/awstats/awstats.<yoursitename>.conf
向下滚动到下面的行 LogFile="/var/log/httpd/access_log"
因为我们正在使用Nginx而不是Apache,所以,需要更改,将其改为 LogFile="/var/log/nginx/access.log"
。
接下来,向下滚动到DirIcons="/awstatsicons"的行,如果找不到,请键入/DirIcons
,然后按ENTER键跳到它,将此行更改为DirIcons="../icon"
。
最后,AWStats需要知道它正在分析哪个网站,这里的两个相关配置选项是SiteDomain
和HostAliases
,SiteDomain
将仅仅是站点的域名; HostAliases将是其他可能使用(例如,如果站点是www.example.com,则可以将example.com放在这里,如果它是同一)的域名的列表。
设置权限
强烈建议不要将AWStats作为root用户运行,我们将在Nginx安装过程中已经设置好的nginx
用户下运行AWStats 。
为此,我们需要使nginx成为AWStats(/var/lib/awstats )的目录的所有者:
sudo chown -R nginx /var/lib/awstats
第一次运行AWStats
在后面的步骤中,我们将设置AWStats在服务器日志被回滚时运行,但是第一次,最好手动运行它,使用以下命令执行这个操作:
sudo -u nginx /usr/share/awstats/wwwroot/cgi-bin/awstats.pl -config=<yoursitename>
注意:-u nginx
部分告诉sudo
以nginx
用户而不是root运行该命令。
输出将类似于以下内容:
Create/Update database for config"/etc/awstats/awstats.<yoursitename>.conf" by AWStats version 7.7 (build 20180105)
From data in log file"/var/log/nginx/access.log"...
Phase 1: First bypass old records, searching new record...
Direct access after last parsed record (after line 0)
Jumped lines in file: 0
Found 0 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.
配置Nginx以在线查看AWStats
接下来,我们将配置Nginx,以便我们可以从网站上查看我们的网站自己的统计信息,而不是通过命令行界面。编辑你的主Nginx配置文件:
sudoedit /etc/nginx/nginx.conf
在这里,我们将添加一个文件夹来包含网站的AWStats部分,在本教程中,我们将称它为webstats
,但是你可以将它称为。
找到配置文件的部分,说明server
,行之后 include /etc/nginx/default.d/*.conf;
添加新的部分:
location /webstats/ {
alias /usr/share/awstats/wwwroot/;
location ~ /cgi-bin/(.+.pl) {
include fastcgi.conf;
fastcgi_pass php-fpm;
fastcgi_split_path_info ^/webstats/(.+.pl)(.*)$;
fastcgi_param SCRIPT_FILENAME /usr/share/awstats/tools/nginx/awstats-fcgi.php;
fastcgi_param X_SCRIPT_FILENAME /usr/share/awstats/wwwroot/$fastcgi_script_name;
fastcgi_param X_SCRIPT_NAME $fastcgi_script_name;
}
}
本节告诉Nginx当我们到浏览器中的webstats
目录时,它应该服务AWStats root,如果在cgi-bin
目录中,它应该运行。
编辑配置文件后,需要重新启动Nginx :
sudo systemctl restart nginx
现在打开浏览器并转到 <your website>/webstats/cgi-bin/awstats.pl?config=<yoursitename>
这是你网站的AWStats主页,
确保页面加载正确,并且你可以在右上角看到AWStats logo ,如果页面未加载或logo丢失,则可能在上一步中配置了错误的内容-返回,并且确保所有路径都正确。
保护AWStats
我们将使用Nginx在统计页上输入密码,再次打开/etc/nginx/nginx.conf
,返回你添加的节,并在location/webstats/{
下面添加以下行:
auth_basic"Username and password required to access AWStats";
auth_basic_user_file /etc/nginx/.htpasswd;
现在我们需要创建.htpasswd
文件,退出配置文件并运行以下命令:
sudo htpasswd -c /etc/nginx/.htpasswd <username>
你的用户名可以是任何字符,但是它不应该包含空格,而且应该是唯一的,不能像admin或webmaster那样容易被猜出来,运行命令时,将提示你输入密码,输入安全密码,然后确认。
重新加载Nginx :
sudo systemctl restart nginx
尝试再次访问AWStats ,这次,你将被询问你的用户名和密码,输入它们,你就会像以前一样被转到AWStats主页。
每天运行AWStats以及回滚日志
最后,我们需要更新统计信息。我们将使用cron
,它是一个任务计划程序实用程序,它在Fedora (以及其他Linux发行版)上已经预先安装,要每天午夜运行AWStats,就编辑/etc/crontab,并且在下面添加下面的行:
0 0 * * * nginx /usr/share/awstats/wwwroot/cgi-bin/awstats.pl -config=<yoursitename>
为了避免丢失数据,我们还希望AWStats在日志被回滚时运行,要做到这一点,编辑/etc/logrotate.d/nginx
,在postrotate
部分之上,添加以下内容:
prerotate
/usr/share/awstats/wwwroot/cgi-bin/awstats.pl -config=<yoursitename>
endscript
现在AWStats已经设置好了,可以开始了。