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

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
日志服务 SLS,月写入数据量 50GB 1个月
简介: 这篇笔记记录了在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日志并进行多维度分析。
相关文章
|
2月前
|
应用服务中间件 nginx Docker
本地通过域名访问虚拟机上nginx的服务、搭建域名访问环境一(反向代理配置)
这篇文章介绍了如何通过域名在本地访问虚拟机上的nginx服务,包括创建nginx容器、修改配置文件、修改本地host文件以及进行访问测试的详细步骤。文章提供了具体的Docker命令来创建并配置nginx容器,展示了配置文件的修改示例,说明了如何在本地系统的hosts文件中添加虚拟机IP和自定义域名,以及如何通过浏览器进行测试访问。
本地通过域名访问虚拟机上nginx的服务、搭建域名访问环境一(反向代理配置)
|
28天前
|
Ubuntu 应用服务中间件 数据库
Nginx配置:阻止非国内IP地址访问的设置方法
此外,出于用户隐私和法律合规性的考虑,应慎重考虑阻止特定国家或地区IP地址的决策。在某些情况下,这可能被视为歧视性或违反当地法律。
51 2
|
2月前
|
JavaScript 应用服务中间件 PHP
nginx server 禁止特定目录下的某类文件访问
【8月更文挑战第26天】这段Nginx配置代码旨在保护`/uploads/`目录下的文件,禁止执行任何`.php`, `.html`, `.htm`, 或 `.js`等潜在有害文件,即便被访问也无法运行。取而代之的是重定向到首页。为了实现这一设置,用户需要定位到对应子域名的`.conf`配置文件中进行相应修改。若网站支持多个访问域名,则需确保在正确的`.conf`文件中实施此配置。
35 1
|
2月前
|
应用服务中间件 Linux nginx
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
|
3月前
|
JavaScript 应用服务中间件 nginx
【项目部署系列教程】5. nginx配置反向代理,解决跨域接口的访问
【项目部署系列教程】5. nginx配置反向代理,解决跨域接口的访问
129 10
|
3月前
|
负载均衡 应用服务中间件 nginx
nginx配置kibana访问用户名和密码认证、及无认证访问配置
nginx配置kibana访问用户名和密码认证、及无认证访问配置
152 0
|
3月前
|
前端开发 应用服务中间件 nginx
网页设计,若依项目修改(It must be done)01----若依打包位置,nginx代理前端静态资源和后端接口,就是怎样设置转载,访问固定端口,让他访问其他资料的配置文件,访问/,给你那些
网页设计,若依项目修改(It must be done)01----若依打包位置,nginx代理前端静态资源和后端接口,就是怎样设置转载,访问固定端口,让他访问其他资料的配置文件,访问/,给你那些
|
3月前
|
网络协议 应用服务中间件 网络安全
如何排查Nginx配置问题导致的域名访问错误
如何排查Nginx配置问题导致的域名访问错误
333 2
|
4月前
|
安全 应用服务中间件 nginx
Nginx访问403
Nginx访问403
140 3
|
3月前
|
应用服务中间件 Linux 开发工具
Nginx14---目录结构分析,查看Ngnix访问日志命令的写法​
Nginx14---目录结构分析,查看Ngnix访问日志命令的写法​
下一篇
无影云桌面