CentOS安装Awstats分析nginx日志并通过htpasswd认证访问

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 这篇笔记记录了在CentOS7.6中安装Awstats7.7,使用Awstats分析nginx日志,以及使用php+nginx搭建web管理界面,通过htpasswd认证访问的过程

首先确定系统已经安装了perl

perl -version

This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
(with 38 registered patches, see perl -V for more detail)

Copyright 1987-2012, Larry Wall

1.安装依赖,缺啥装啥

yum -y  install gcc gcc-c++ zlib-devel perl-devel perl-ExtUtils-MakeMaker httpd-tools

我们要分析的是nginx的日志,但认证访问使用的是httpd-tools这个工具,httpd-tools自带httpd,所以安装后需要确定httpd不是启动状态,停止服务,卸载开机启动

#centos6.x
service httpd stop
chkconfig httpd off
#centos7.x
systemctl stop httpd
systemctl disable httpd

2.开启nginx访问日志
开启nginx.conf日志格式段代码

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

假设要分析的网站是www.testawstats.xyz,编辑该站点的nginx配置,记录访问日志

    access_log  /var/log/nginx/www.testawstats.xyz.access.log  main;

3.安装GeoIP库

mkdir -p /usr/local/geoip
cd /usr/local/geoip
wget -c http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
wget -c http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
#解压
gzip -d GeoLiteCity.dat.gz
gzip -d GeoIP.dat.gz

4.安装GeoIP C库

#下载
wget -c https://www.maxmind.com/download/geoip/api/c/GeoIP-1.4.8.tar.gz
tar -zxvf GeoIP-1.4.8.tar.gz
cd GeoIP-1.4.8
#安装
./configure
make && make install 

5.安装GeoIP perl库

#下载
wget -c https://www.maxmind.com/download/geoip/api/perl/Geo-IP-1.40.tar.gz
tar -zxvf Geo-IP-1.40.tar.gz
cd Geo-IP-1.40
#安装
perl Makefile.PL LIBS='-L/usr/local/lib'
make && make install

6.安装CPAN库,awstats支持中文要用到

#下载
wget -c https://cpan.metacpan.org/authors/id/G/GA/GAAS/URI-1.36.tar.gz
#如果提示证书问题则加--no-check-certificate参数
wget -c --no-check-certificate https://cpan.metacpan.org/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

7.安装awstats
创建源码保存目录

mkdir -p /usr/local/src/awstats
cd /usr/local/src/awstats

下载,解压

wget -c https://prdownloads.sourceforge.net/awstats/awstats-7.7.tar.gz
tar -zxvf awstats-7.7.tar.gz

将源码拷贝到运行目录

cp -r awstats-7.7 /usr/local/awstats

8.配置awstats
进入awstats工具目录

cd /usr/local/awstats/tools/

创建配置文件

./awstats_configure.pl

----- AWStats awstats_configure 1.0 (build 20140126) (c) Laurent Destailleur -----
This tool will help you to configure AWStats to analyze statistics for
one web server. You can try to use it to let it do all that is possible
in AWStats setup, however following the step by step manual setup
documentation (docs/index.html) is often a better idea. Above all if:
- You are not an administrator user,
- You want to analyze downloaded log files without web server,
- You want to analyze mail or ftp log files instead of web log files,
- You need to analyze load balanced servers log files,
- You want to 'understand' all possible ways to use AWStats...
Read the AWStats documentation (docs/index.html).

-----> Running OS detected: Linux, BSD or Unix

-----> 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.testawstats.xyz

输入www.testawstats.xyz继续,之后一路回车完毕
完毕之前会提示你创建的配置文件路径和分析日志命令

......
 Config file /etc/awstats/awstats.www.testawstats.xyz.conf created.

-----> 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.testawstats.xyz
......

修改配置

vim /etc/awstats/awstats.www.testawstats.xyz.conf

LogFile="/var/log/nginx/www.testawstats.xyz.access.log"
DirData="/var/lib/awstats"
LoadPlugin="decodeutfkeys"
LoadPlugin="geoip GEOIP_STANDARD /usr/local/geoip/GeoIP.dat"
LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /usr/local/geoip/GeoLiteCity.dat"

创建数据目录

mkdir /var/lib/awstats

9.分析日志

/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.testawstats.xyz
Create/Update database for config "/etc/awstats/awstats.www.testawstats.xyz.conf" by AWStats version 7.7 (build 20180105)
From data in log file "/var/log/nginx/www.testawstats.xyz.access.log"...
Phase 1 : First bypass old records, searching new record...
Searching new records from beginning of log file...
Phase 2 : Now process new records (Flush history on disk after 20000 hosts)...
Jumped lines in file: 0
Parsed lines in file: 12
 Found 0 dropped records,
 Found 0 comments,
 Found 0 blank records,
 Found 0 corrupted records,
 Found 0 old records,
 Found 12 new qualified records.

成功分析了日志文件
10.搭建web界面
创建站点目录,目录会是空的,主要是conf和站点目录一一对应,不但conf要在一起,站点目录也要在一起,管理上别乱

mkdir -p /www/awstats/wwwroot

复制php文件awstats的wwwroot目录

cp /usr/local/awstats/tools/nginx/awstats-fcgi.php /usr/local/awstats/wwwroot/cgi-bin/fcgi.php

生成密钥文件

htpasswd -c -m /usr/local/htpasswd.pass youname
New password: 
Re-type new password: 
Adding password for user youname

创建nginx配置

cp /usr/local/awstats/tools/nginx/awstats-nginx.conf /etc/nginx/conf.d/awstats-nginx.conf

修改nginx配置
假设我的awstats界面域名是awstats.testawstats.xyz,我的nginx配置是这样子的

server {
    listen 80;
    server_name awstats.testawstats.xyz;
    access_log /var/log/nginx/localhost.access_log main;
    error_log /var/log/nginx/localhost.error_log info;
    root /www/awstats/wwwroot;
    index index.html;

    # Restrict access
    auth_basic "Restricted";
    auth_basic_user_file /usr/local/htpasswd.pass;


    # Static awstats files: HTML files stored in DOCUMENT_ROOT/awstats/
    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 /awstats-icon/ {
        alias /usr/local/awstats/wwwroot/icon/;
    }

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

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

11.访问web界面
http://awstats.testawstats.xyz/cgi-bin/awstats.pl?config=www.testawstats.xyz

假如出现这个界面,说明htpasswd认证设置成功了,输入用户名密码,继续

ok,整个流程都通了
12.定时分析日志

#编辑定时任务
crontab -e
#设置时间和脚本,每天00:01分执行一次
1 0 * * * /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.testawstats.xyz
#esc然后:wq保存并退出

centos安装nginx和php请移步以下地址:
CentOS7源码编译安装nginx+php7.2+mysql5.7并使用systemctl管理
CentOS7yum安装nginx+php7+mysql
CentOS6.9源码编译安装nginx+php7+mysql环境
CentOS6.9yum安装nginx+php7+mysql环境
原文地址:https://www.jmsite.cn/blog-351.html

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
3月前
|
监控 应用服务中间件 定位技术
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
298 3
|
4月前
|
存储 Linux Docker
centos系统清理docker日志文件
通过以上方法,可以有效清理和管理CentOS系统中的Docker日志文件,防止日志文件占用过多磁盘空间。选择合适的方法取决于具体的应用场景和需求,可以结合手动清理、logrotate和调整日志驱动等多种方式,确保系统的高效运行。
407 2
|
5月前
|
应用服务中间件 Linux nginx
CentOS7安装Nginx
CentOS7安装Nginx
|
7月前
|
应用服务中间件 Linux 网络安全
2022年超详细在CentOS 7上安装Nginx方法(源码安装)
这篇文章提供了在CentOS 7系统上通过源码安装Nginx的详细步骤,包括从官网下载Nginx源码包、上传至虚拟机、解压、删除压缩包、编译安装前的配置、安装PCRE库(因为Nginx使用PCRE库解析正则表达式)、安装zlib和OpenSSL库(用于支持HTTPS协议)、重新编译Nginx、安装后启动Nginx服务、关闭服务、修改默认端口、以及重启服务测试等步骤。文章还提供了相关命令和操作截图,帮助用户更好地理解和执行安装过程。
2022年超详细在CentOS 7上安装Nginx方法(源码安装)
|
5月前
|
缓存 Linux 编译器
【C++】CentOS环境搭建-安装log4cplus日志组件包及报错解决方案
通过上述步骤,您应该能够在CentOS环境中成功安装并使用log4cplus日志组件。面对任何安装或使用过程中出现的问题,仔细检查错误信息,对照提供的解决方案进行调整,通常都能找到合适的解决之道。log4cplus的强大功能将为您的项目提供灵活、高效的日志管理方案,助力软件开发与维护。
189 0
|
7月前
|
应用服务中间件 Linux nginx
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
|
7月前
|
应用服务中间件 Linux nginx
在CentOS上使用源码包安装Nginx、以及手动启动Nginx的步骤过程
这篇文章介绍了在CentOS系统上使用Nginx源码包进行安装和配置的详细步骤,包括源码包的获取、解压、配置、编译、安装、启动验证以及注意事项。
555 0
在CentOS上使用源码包安装Nginx、以及手动启动Nginx的步骤过程
|
7月前
|
应用服务中间件 Shell nginx
shell分析nginx日志的一些指令
shell分析nginx日志的一些指令
|
15天前
|
Linux 虚拟化 Windows
VMware隐藏黑科技!CentOS安装这个神器效率翻倍 文件互传竟比U盘还快?
### 安装VMtools的作用及步骤 安装VMtools可以实现Windows与CentOS之间的字符串和命令粘贴、文件夹共享等操作。在CentOS中,通过VMware菜单栏选择“VMware Tools”,将下载的压缩包拷贝到`/opt`目录并解压。接着进入终端,使用`./vmware-install.pl`命令完成安装。安装后重启系统,即可实现Windows与CentOS之间的字符和文件共享。具体步骤包括解压文件、启动命令行终端、安装VMtools以及配置文件共享。
52 16
VMware隐藏黑科技!CentOS安装这个神器效率翻倍 文件互传竟比U盘还快?
|
15天前
|
存储 IDE Linux
零基础保姆级教程!手把手教你免费玩转Linux CentOS安装+学习环境搭建(附避坑指南)
本文详细介绍了在VMware虚拟机中安装CentOS 6.8的全过程。首先,需确保已安装VMware并开启V-CPU虚拟化功能,可通过BIOS设置或使用LeoMoon CPU-V工具检测。接着,下载CentOS镜像文件,并在VMware中新建虚拟机,配置CPU、内存、硬盘等参数。最后,加载ISO镜像启动虚拟机,按照提示完成CentOS的安装,包括语言、键盘、存储方式、地区、密码设置及硬盘分区等步骤。安装完成后,以root用户登录即可进入系统桌面,开始学习Linux命令和操作。
72 12
零基础保姆级教程!手把手教你免费玩转Linux CentOS安装+学习环境搭建(附避坑指南)