Apache 配置与应用(日志分割--日志分析等)

前言

Apache HTTP Server 之所以受到众多企业的青睐,得益于其代码开源、跨平台、功能模块化、可灵活定制等诸多优点,其不仅性能稳定,在安全性方面的表现也十分出色。

一、Apache 连接保持

Apache 通过设置文件httpd-default.conf中相关的连接保持参数来开启与控制连接保持功能。
(1)KeepAlive
是否打开连接保持,OFF关闭,ON打开

(2)KeepAlive Timeout
一次连接多次请求之间的最大间隔时间,两次请求超过该时间连接断开

(3)MaxKeepAliveRequests
一次连接能够传输的最大请求数量

二、Apache 的访问控制

为了更好地控制对网站资源的访问,可以为特定的网站目录添加访问授权。

Apache 的访问控制作用:
控制对网站资源的访问
为特定的网站目录添加访问授权

常用访问控制方式:
客户机地址限制
用户授权限制

2.1、客户机地址限制

通过Require配置项,可以根据客户端的主机名或IP地址来决定是否允许客户端访问。
在httpd服务的主配置文件的< Location >、< Directory >、< Files >、< Limit >配置段中均可使用Require配置项来控制客户端的访问。使用Require配置项时,需要设置客户端地址以构成完整的限制策略,地址形式是IP地址、网络地址、主机或域名。

Require配置项的常见语法

Require all granted 允许所有之间访问
Require all denied 拒绝所有主机访问
Require local 仅允许本地主机访问
Require [not] host <主机名或域名列表>
Require [not] ip <IP地址或网段列表>
使用not禁止访问时要将其置于< RequireAll >< /RequireAll >容器中并在容器中指定相应的限制策略

2.1.1、实验1

#####例子1###默认目录允许所有
<Directory " /usr/local/httpd/htdocs">
######省略部分内容
Require all granted
</Directory>

在这里插入图片描述
现在去浏览器输入 20.0.0.5,显示成功
在这里插入图片描述

2.1.2、实验2

定义限制策略时,多个不带“not”的Require配置语句之间是“或”的关系,即任意一条Require配置语句满足条件均可访问。若既出现了不带“not”的Require配置语句,又出现了带“not”的Require配置语句,则配置语句之间是“与”的关系,即同时满足所有Require配置语句才能访问。
需要使用“仅允许”的限制策略时,应使用quire配置语句明确设置允许策略,只允许一部分主机访问。例如,若只希望IP地址为192.168.10.2的主机能够访问,目录区域应做

####例子2####
<Directory "/usr/local/httpd/htdocs/bbs">
......//省略部分内容
Require ip 192.168.10.2  ## 只允许让 192.168.10.2 可以通过访问
</Directory>

在这里插入图片描述
现在在浏览器里输入 20.0.0.5 IP。显示访问失败!
在这里插入图片描述

2.1.3、实验3

反之,需要使用“仅拒绝”的限制策略时,灵活使用Require与Require not配置语句设置拒绝访问策略,仅禁止一部分主机访问。在使用not禁止访问时要将其置于容器中,并在容器中设置相应的限制策略。例如,若只希望禁止来自两个内网网段192.168.100.0/24和192.168.1.0/24的主机访问,但允许其他任何主机访问,可以使用如下限制策略

#####例子3###
<Directory "/usr/local/httpd/htdocs/bbs">
......//省略部分内容
<RequireAll>
Require all granted
Require not ip 192.10.0.0/24 192.10.1.0/24
</RequireAll>
</Directory>
当未被授权的客户机访问网站目录时,将会被拒绝访问。

在这里插入图片描述
在这里插入图片描述
现在去浏览器输入 20.0.0.5,显示成功

2.2、用户授权限制

httpd 的基本认证通过校验用户名、密码组合来判断是否允许用户访问。授权访问的用户账号需要事先建立,并保存在固定的数据文件中。使用专门的htpasswd工具程序,可以创建授权用户数据文件,并维护其中的用户账号。
使用htpasswd工具时,必须指定用户数据文件的位置,添加“-c”选项表示新建立此文件。例如,执行以下操作可以新建数据文件/usr/local/httpd/conf/.awspwd,其中包含一个名为webadmin的用户信息

[root@localhost ~]# cd /usr/local/httpd/
[root@localhost httpd]# htpasswd -c /usr/local/httpd/conf/.awspwd webadmin
New password:
Re-type new password:
Adding password for user webadmin

在这里插入图片描述
添加用户授权配置
有了授权用户账号以后,还需要修改httpd.conf配置文件,在特定的目录区域中添加授权配置,以启用基本认证并设置允许哪些用户访问。例如,若只允许.awspwd数据文件中的任一用户访问系统,可以执行以下操作。

  [root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf
<Directory "/usr/local/httpd/htdocs">
AuthName "DocumentRoot"
AuthType Basic
AuthUserFile /usr/local/httpd/conf/.awspwd
Require valid-user
</Directory>
[root@localhost ~]# systemctl restart httpd    ####重启服务使配置生效

在这里插入图片描述
在上述配置内容中,相关配置项的含义如下。
AuthName:定义受保护的领域名称,该内容将在浏览器弹出的认证对话框中显示。

AuthType:设置认证的类型,Basic表示基本认证。

AuthUserFile:设置用于保存用户账号、密码的认证文件路径。

require valid-user:要求只有认证文件中的合法用户才能访问。其中,valid-user表示所有合法用户,若只授权给单个用户,可改为指定的用户名(如webadmin)

验证用户访问授权
在这里插入图片描述
在这里插入图片描述

三、Apache 日志分割

随着网站的访问量越来越大,默认情况下Apache服务器产生的单个日志文件也会越来越大,如果不对日志进行分割,那么如果日志文件占用磁盘空间很大的话势必会将整个日志文件删除,这样也丢失了很多对网站比较宝贵的信息,而这些日志可以用来进行访问分析、网络安全监察、网络运行状况监控等。另外,如果服务器遇到故障时,运维人员要打开日志文件进行分析,打开的过程会消耗很长时间,也势必会增加处理故障的时间。因此管理好这些海量的日志对网站的意义很大,我们会将Apache的日志进行按每天的日期自动分割。下面介绍两种方法均可实现。

3.1、Apache 自带rotatelogs 分割工具

首先我们将Apache主配置文件httpd.conf打开,配置网站的日志文件转交给rotatelogs分割处理。

[root@localhost ~]# mkdir /var/log/httpd/
[root@www ~]# vim /usr/local/httpd/conf/httpd.conf
......####省略部分内容
ErrorLog "/usr/local/bin/rotatelogs-l/var/log/httpd/error_%Y%m%d.log 86400"
CustomLog "/usr/local/bin/rotatelogs-l/var/log/httpd/access_%Y%m%d.log 86400" common
[root@localhost ~]# systemctl restart httpd
[root@localhost~]#l/var/log/httpd/
-rw-r-r- 1 root root 18147月717:54 access_20180707.log
-rw-r-r- 1 root root 584 7月717:55 error_20180707.log

其中ErrorLog行是错误日志,不用太多关注,一般不会记录错误的访问,-l 表示使用本地时间代替GMT时间作为时间基准。注意:在一个改变GMT偏移量(比如夏令时)的环境中使用 -l 会导致不可预料的结果。
CustomLog 行是定义访问日志格式,86400表示一天,即每天生成一个新的日志文件。重启Apache服务,查看日志文件是否已经按日期分割

3.2、使用第三方工具cronolog 分割

除了Apache自带rotatelogs分割工具,也可使用第三方工具cronolog对Apache日志进行分割,具体操作如下所示。

 ##1、编译安装cronolog工具##
[root@localhost ~]# tar zxvf cronolog-1.6.2.tar.gz
[root@localhost ~]# cd cronolog-1.6.2
[root@localhost cronolog-1.6.2]#./configure
[root@localhost cronolog-1.6.2]# make &&make install

##2、设置cronolog工具工具分割apache日志##
[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf
ErrorLog "|/usr/local/sbin/cronolog /var/log/httpd/www.51xit.top-error_%Y%m%d.log"
CustomLog "/usr/local/sbin/cronolog /var/log/httpd/www.51xit.top-access%Y%m%d.log" common
[root@localhost~]# systemctl restart httpd

四、AWStats 日志分析

Perl语言开发的一款开源日志分析系统

可用来分析Apache、Samba、Vsftpd、lIS等服务器的访问日志

结合crond等计划任务服务,可对日志内容定期进行分析

4.1、部署AWStats 分析系统

4.1.1、安装AWStats 软件包

首先虚拟上已安装Apache 服务(前面博客已经详解)

[root@localhost ~]# yum -y install wget  ## 安装 wget 软件
[root@localhost ~]# wget http://awstats.org/files/awstats-7.6.tar.gz  在线下载awstats软件包
[root@localhost ~]# tar zxvf awstats-7.6.tar.gz  ## 解压软件包
[root@localhost ~]# mv awstats-7.6 /usr/local/awstats  ## 移动到 /usr/local/awstats 目录

4.1.2、为要统计的站点建立配置文件

[root@localhost ~]# cd /usr/local/awstats/tools/
[root@localhost tools]# ll
total 168
-rwxr-xr-x 1 tx tx 19788 Aug 27  2016 awstats_buildstaticpages.pl
-rwxr-xr-x 1 tx tx 25990 Aug 27  2016 awstats_configure.pl
-rwxr-xr-x 1 tx tx 12593 Jan 30  2016 awstats_exportlib.pl
-rwxr-xr-x 1 tx tx  5389 Sep 23  2014 awstats_updateall.pl
drwxr-xr-x 2 tx tx    23 Sep 23  2014 dolibarr
-rwxr-xr-x 1 tx tx 16357 Sep 23  2014 geoip_generator.pl
-rw-r--r-- 1 tx tx   855 Sep 23  2014 httpd_conf
-rwxr-xr-x 1 tx tx 33291 Aug 27  2016 logresolvemerge.pl
-rwxr-xr-x 1 tx tx 27771 Aug 27  2016 maillogconvert.pl
drwxr-xr-x 2 tx tx    74 Dec  3  2016 nginx
-rwxr-xr-x 1 tx tx  9755 Sep 23  2014 urlaliasbuilder.pl
drwxr-xr-x 2 tx tx    64 Dec  3  2016 webmin
drwxr-xr-x 2 tx tx   161 Sep 23  2014 xslt

[root@localhost tools]# chmod +x awstats_configure.pl   ## 给目录进行提权操作,提升执行权
[root@localhost tools]# ./awstats_configure.pl ## 配置脚本将查找并识别httpd服务的主配置文件,以便自动添加相关配置内容
4.1.2.1、指定httpd 主配置文件的路径
Config file path ('none' to skip web server setup):
> /usr/local/httpd/conf/httpd.conf            ##输入httpd.conf配置文件的路径
4.1.2.2、设置日志类型
Warning: You Apache config file contains directives to write 'common' log files
This means that some features can't work (os, browsers and keywords detection).
Do you want me to setup Apache to write 'combined' log files [y/N] ? y   ## 这里当提示是否修改日志类型时,建议选择“y”,然后配置脚本,将会自动修改 httpd.conf 配置文件,以添加访问 AWAstats 系统的相关配置内容
4.1.2.3、为指定Web 站点创建配置文件
-----> 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.51xit.com   ## 指定要统计的目标网站名称


-----> Define config file path
In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directory path to store config file(s) (Enter for default): ## 这边默认回车就可以了

-----> Create config file '/etc/awstats/awstats.www.51xit.com.conf'
 Config file /etc/awstats/awstats.www.51xit.com.conf created.
 ## 这边的配置文件已经完成了
4.1.2.4、配置httpd 并重启
[root@localhost tools]# vi /usr/local/httpd/conf/httpd.conf
<IfModule !mpm_prefork_module>
        LoadModule cgid_module modules/mod_cgid.so
</IfModule>
<IfModule mpm_prefork_module>
        LoadModule cgi_module modules/mod_cgi.so
</IfModule>  ##将#去掉##
<Directory "/usr/local/awstats/wwwroot">
    Options None
    AllowOverride None
#    Order allow,deny
#    Allow from all
    Require all granted
</Directory>   ###在合适位置添加##
[root@localhost tools]# systemctl restart httpd

4.1.3、修改站点统计配置文件

[root@localhost tools]# vi /etc/awstats/awstats.www.51xit.com.conf
LogFile="/usr/local/httpd/logs/access_log"
DirData="/var/lib/awstats"
[root@localhost ~]# mkdir /var/lib/awstats
[root@localhost ~]# cd /usr/local/awstats/
[root@localhost awstats]# cd tools/
[root@localhost tools]# chmod +x awstats_updateall.pl 
[root@localhost tools]# ./awstats_updateall.pl now ##运行,而后在浏览器中访问##

4.2、访问AWStats 分析系统

现在在浏览器输入 http://20.0.0.6/awstats/awstats.pl?config=www.51xit.com。会出现以下网址,
在这里插入图片描述
实验成功,还能进行优化

在访问AWStats 系统时,需要制定 awstats 目录、脚本位置等信息,这样既不便于记忆,输入也很麻烦。我们这边可以简化操作,可以在Web目录下建立一个自动跳转的HTML网页。用户只需要访问 www.51xit.com/awb.html,就可以自动跳转到 www.51xit.com站点的AWStats日志分析页面。

[root@localhost tools]# vi /usr/local/httpd/htdocs/awb.html
<html>
<head>
<meta http-equiv=refresh content="0;
url=http://20.0.0.6/awstats/awstats.pl?config=www.51xit.com">
</head>
<body></body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_48191211/article/details/108333629