理论+实验:Apache配置与应用 (Apache连接保持、访问控制、日志分割、AWStats日志分析)

一、Apache配置剖析

1.1 Apache连接保持

  • Apache连接保持相关参数
    KeepAlive
       是否打开连接保持,OFF关闭,ON打开
  • keepAliveTimeout
    一次连接多次请求之间的最大间隔时间,两次请求超过该时间连接断开
  • MaxKeepAliveRequests
    一次长连接能够传输的最大请求数量

1.1.1 Apache连接保持小实验

1. 打开浏览器输入 20.0.0.6 (这里是自己的虚拟机IP地址)

keep-alive :timeout=5 max=100   ##这里是原来的默认值
conetction:keep-Alive
####详解###
就是在timeout时间内又有新的连接过来,同时max会自动减1,直到为0,强制断掉。
  • 现在进行配置文件
步骤一:
###配置主配置文件###
vi /usr/local/httpd/conf/httpd.conf
Include conf/extra/httpd-default.conf####将前面的#号去掉,表示开启该模块
步骤二:
###配置连接保持###
vi /usr/local/httpd/conf/extra/httpd-default.conf
KeepAlive On
####保持连接开启(默认开启)
MaxKeepAliveRequests 200
####━次连接最多请求200个文件(默认100个)
KeepAliveTimeout 10
####无响应超时踢下线时间设置10秒(默认5秒)
timeout:过期时间5秒(对应httpd.conf里的参数是:KeepAliveTimeout),max是最多一百次请求,强制断掉连接
  • 现在用抓包工具进行抓包(这里需要注意:如果要进行抓包,要选择绑定虚拟机网卡的那个网卡进行抓包!!!)
    我们可以看见,原来的默认值已经变成我们刚才更改的配置参数里面的了

在这里插入图片描述

1.2 Apache访问控制

        Apache可以基于源主机名、源IP地址或源主机上的浏览器特征等信息对网站上的资源进行访问控制。它通过Allow指令允许某个主机访问服务器上的网站资源,通过Deny指令实现禁止访问。在允许或禁止访问网站资源时,还会用到Order指令,这个指令用来定义Allow或Deny指令起作用的顺序,其匹配原则是按照顺序进行匹配,若匹配成功则执行后面的默认指令。比如“Order Allow, Deny”表示先将源主机与允许规则进行匹配,若匹配成功则允许访问请求,反之则拒绝访问请求。

1.2.1 客户机限制

        通过Require配置项,可以根据客户端的主机名或IP地址来决定是否允许客户端访问。在httpd服务的主配置文件的、、、配置段中均可以使用Require配置项来控制客户端的访问。使用Require配置项时,需要设置客户端地址以构成完整的限制策略,地址形式可以是IP地址、网络地址、主机名或域名。当Require配置项之后为“all”时,表示匹配任意地址。限制策略的格式如下所示。

1、Require all granted:允许所有主机访问;
2、Require all denied:拒绝所有主机访问;
3、Require local:仅允许本地主机访问;
4、Require [not]host<主机名或域名列表>∶允许或拒绝指定主机或域名访问;
5、Require [not]ip <IP地址或网段列表>:允许或拒绝指定IP地址网络访问。
通常情况下,网站服务器是对所有客户机开放的,网页文档目录并未做任何限制,因此使用的是“Require all granted”的策略,表示允许从任何客户机访问,策略格式如下所示。

1.2.1.1 实验1

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

在这里插入图片描述

  • 现在去浏览器输入 20.0.0.6,显示成功

在这里插入图片描述

1.2.1.2 实验2

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

####列子2####
<Directory "/usr/local/httpd/htdocs/bbs">
......//省略部分内容
Require ip 192.168.100.2  ## 只允许让 192.168.100.2 可以通过访问
</Directory>
  • 现在在浏览器里输入 20.0.0.6 IP。显示访问失败!
    在这里插入图片描述
####列子2####
<Directory "/usr/local/httpd/htdocs/bbs">
......//省略部分内容
Require ip 192.168.100.2  ## 只允许让 192.168.100.2 可以通过访问
</Directory>

在这里插入图片描述

  • 现在输入 20.0.0.6 可以访问了!

在这里插入图片描述

1.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.168.0.0/24 192.168.1.0/24
</RequireAll>
</Directory>
当未被授权的客户机访问网站目录时,将会被拒绝访问。

在这里插入图片描述
在这里插入图片描述

  • 在浏览器输入 20.0.0.6进行测试
    在这里插入图片描述

1.2.2 用户授权限制

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

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

在这里插入图片描述

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

[root@www ~]# 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@www ~]# systemctl restart httpd    ####重启服务使配置生效

在这里插入图片描述

在上述配置内容中,相关配置项的含义如下。

  1. AuthName:定义受保护的领域名称,该内容将在浏览器弹出的认证对话框中显示。
  2. AuthType:设置认证的类型,Basic表示基本认证。
  3. AuthUserFile:设置用于保存用户账号、密码的认证文件路径。
  4. require valid-user:要求只有认证文件中的合法用户才能访问。其中,valid-user表示所有合法用户,若只授权给单个用户,可改为指定的用户名(如webadmin)
  • 验证用户访问授权
    20.0.0.6 输入 webadmin 密码,访问成功!

在这里插入图片描述
在这里插入图片描述

二、Apache日志管理

2.1 日志分割

  • 随着网站的访问量增加,默认情况下Apache的单个日志文件也会越来越大
    1、日志文件占用磁盘空间很大
    2、查看相关信息不方便

  • 对日志文件进行分割
    1、Apache自带rotatelogs分割工具实现
    2、第三方工具cronolog分割

2.1.1 rotatelogs分割工具

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

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

[root@www ~]# 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@www ~]# systemctl restart httpd
[root@www~]#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服务,查看日志文件是否已经按日期分割

在这里插入图片描述

找到 ErroLog,CustomLog两个地方,改配置

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

然后这时候在浏览器在重新登录一下,就可以看见日志文件了(不登录的话,会没有access日志文件)

在这里插入图片描述

在这里插入图片描述

2.1.2 第三方工具cronolog分割

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

##1、编译安装cronolog工具##
[root@www ~]# tar zxvf cronolog-1.6.2.tar.gz
[root@www ~]# cd cronolog-1.6.2
[root@www cronolog-1.6.2]#./configure
[root@www cronolog-1.6.2]# make &&make install
##2、设置cronolog工具工具分割apache日志##
[root@www ~]# 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@wwW~]# systemctl restart httpd

2.2 AWStats日志分析

  • Perl语言开发的一款开源日志分析系统
  • 可用来分析Apache、Samba、Vsftpd、lIS等服务器的访问日志
  • 结合crond等计划任务服务,可对日志内容定期进行分析

2.2.1 实验步骤

2.2.1.1 编译安装HTTP服务

  • 将这三个软件包传到opt目录下
httpd-2.4.29.tar.gz
  • 对压缩包进行解压
[root@localhost ~]# cd /opt
[root@localhost opt]# ll
总用量 8020
-rw-r--r--  1 root root 1071074 8月   4 17:33 apr-1.6.2.tar.gz
-rw-r--r--  1 root root  565507 8月   4 17:33 apr-util-1.6.0.tar.gz
-rw-r--r--  1 root root 6567926 8月   4 17:33 httpd-2.4.29.tar.bz2
drwxr-xr-x. 2 root root       6 3月  26 2015 rh

[root@localhost opt]# tar zxvf apr-1.6.2.tar.gz
[root@localhost opt]# tar zxvf apr-util-1.6.0.tar.gz
[root@localhost opt]# yum -y install bzip2  ## 如果是最小化安装需要安装bzip2
[root@localhost opt]# tar jxvf httpd-2.4.29.tar.bz2

[root@localhost opt]# mv apr-1.6.2 httpd-2.4.29/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util 
  • 安装环境编译HTTP的环境
##################安装环境##################
[root@localhost opt]# 
yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl
  • 安装配置模块
[root@localhost opt]# cd /opt/httpd-2.4.29/  ## 安装模块
./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
####配置模块解释####
--prefix:指定将 httpd 服务程序安装到哪个目录下,如/usr/local/httpd
--enable-so:启用动态加载模块支持,使 httpd 具备进一步扩展功能的能力。
--enable-rewrite:启用网页地址重写功能,用于网站优化及目录迁移维护。
--enable-charset-lite:启动字符集支持,以便支持使用各种字符集编码的网
--enable-cgi:启用 CGI 脚本程序支持,便于扩展网站的应用访问能力。
  • 编译安装
###编译及安装### 
make -j3 && make install                 
### -j3是你核心数,最大不要超过虚拟机的核心数
  • 优化执行路径
####优化执行路径####
ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/
httpd -v      ###查看下HTTP版本
Server version: Apache/2.4.29 (Unix)
Server built:   Aug 31 2020 02:01:04
  • 建立[service].service 配置文件添加系统给服务。在/lib/systemd/system/目录下,建立一个以.service 结尾的单元(unit)配置文件,用于控制由 systemd 管理或监控的 httpd 服务
###建立[service].service 配置文件添加系统给服务####
在/lib/systemd/system/目录下,建立一个以.service 结尾的单元(unit)配置文件,
用于控制由 systemd 管理或监控的 httpd 服务
[root@localhost ~]# cd /lib/systemd/system/
[root@localhost system]# vim httpd.service
[Unit]
Description=The Apache HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/httpd/logs/httpd.pid
ExecStart= /usr/local/bin/apachectl $OPTIONS
ExecrReload= /bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
  • 设置启动http和开机自启http,并且查看http的运行状态
[root@localhost system]# systemctl start httpd.service
[root@localhost system]# systemctl enable httpd.service         ###开机自启http
[root@localhost system]# systemctl is-enabled httpd.service   ###检查HTTP单元是否启动
  • httpd.conf修改配置文件
####httpd.conf修改配置文件###
[root@localhost system]# vi /usr/local/httpd/conf/httpd.conf
[root@localhost system]# ServerName www.51xit.com:80  ###这里面的网址可以自己任意改。
[root@localhost system]# systemctl restart httpd####重启httpd服务
  • 现在我们在浏览器进行试验!
    在这里插入图片描述

2.2.1.2 部署AWStats分析系统

yum -y install wget  ## 安装 wget 软件
wget http://awstats.org/files/awstats-7.6.tar.gz  在线下载awstats软件包
tar zxvf awstats-7.6.tar.gz  ## 解压软件包
mv awstats-7.6 /usr/local/awstats  ## 移动到 /usr/local/awstats 目录
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服务的主配置文件,以便自动添加相关配置内容

Config file path ('none' to skip web server setup):
> /usr/local/httpd/conf/httpd.conf
> -----> Check and complete web server config file '/usr/local/httpd/conf/httpd.conf'   ##输入httpd.conf配置文件的路径

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 系统的相关配置内容


-----> 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.
 ## 这边的配置文件已经完成了

## 这边进入改配置文件,如下操作
[root@localhost tools]# vi /usr/local/httpd/conf/httpd.conf
<Directory "/usr/local/awstats/wwwroot">
    Options None
    AllowOverride None
    #Order allow,deny
    #Allow from all
    Require all granted
</Directory>
== wq 保存退出

[root@localhost tools]# systemctl restart httpd  ## 刷新httpd服务

## 修改站点统计配置文件
[root@localhost tools]# vi /etc/awstats/awstats.www.51xit.com.conf 
LogFile="/usr/local/httpd/logs/access_log"
DirData="/var/lib/awstats"
== wq 保存退出
## LogFile用来指定日志路径,应设置Web日志文件的实际位置;DirData用来指定数据目录,可以采用默认值,但需要创建指定的目录(/var/lib/awstats)

[root@localhost tools]# systemctl restart httpd  ## 刷新 httpd 服务
[root@localhost tools]# mkdir /var/lib/awstats  ## 手动创建指定目录


[root@localhost tools]# chmod +x awstats_updateall.pl  ## 提升执行权
[root@localhost tools]# ./awstats_updateall.pl now  ## 执行该脚本时,系统将会自动分析新增的日志内容,并将结果更新到统计数据库中。

Running '"/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -update -config=www.51xit.com -configdir="/etc/awstats"' to update config www.51xit.com
Create/Update database for config "/etc/awstats/awstats.www.51xit.com.conf" by AWStats version 7.6 (build 20161204)
From data in log file "/usr/local/httpd/logs/access_log"...
Phase 1 : First bypass old records, searching new record...
Searching new records from beginning of log file...
Jumped lines in file: 0
Parsed lines in file: 20
 Found 0 dropped records,
 Found 0 comments,
 Found 0 blank records,
 Found 20 corrupted records,
 Found 0 old records,
 Found 0 new qualified records.
 == wq 保存退出
 
 ### 这边做计划任务 ###
 做计划任务的目的是,当我们不在的时候,可以进行自动化运维操作
 [root@localhost tools]# crontab -e
*/5 * * * * /usr/local/awstats/tools/awstats_updateall.pl now  ## 每5分钟执行一次
[root@localhost tools]# systemctl start crond   ## 启动计划任务
[root@localhost tools]# systemctl enable crond   ## 开机自启计划任务


[root@localhost tools]# vi /usr/local/httpd/conf/httpd.conf
<IfModule !mpm_prefork_module>
LoadModule cgid_module modules/mod_cgid.so                            ###这边修改下将#去掉
</lfModule>
<lfModule mpm prefork module>
LoadModule cgi_module modules/mod_cgi.so                                 ###这边修改下将#去掉
</IfModule>
<Directory "/usr/local/awstats/wwwroot">
== wq 保存退出
[root@localhost tools]# systemctl restart httpd 
  • 现在在浏览器输入 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.66/awstats/awstats.pl?config=www.51xit.com">
</head>
<body></body>
</html>
==  保存退出

然后再浏览器输入 www.51xit.com/awb.html 就可以访问 AWStats日志分析页面了。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44733021/article/details/108349401