Apache访问控制与日志管理(日志分割和AWStats日志分析)

一、Apache连接保持

●Apache连接保持相关参数
KeepAlive
是否打开连接保持,OFF关闭,ON打开
KeepAliveTimeout
一次连接多次请求之间的最大间隔时间,两次请求超过该时间连接断开
MaxKeepAliveRequests
一次长连接能够传输的最大请求数量
可以用来进行服务器优化,防止客户端不请求任何数据,但是一直占用着服务器的进程。
在这里插入图片描述

二、Apache访问控制概述

2.1Apache访问控制

●作用
控制对网站资源的访问
为特定的网站目录添加访问授权
●常用访问控制方式
客户机地址限制
用户授权限制

2.2基于客户端地址的访问控制

●使用Require配置项实现访问控制,按先后顺序限制
●Require配置项的常见语法
Require all granted ##允许所有人访问
Require all denied ##拒绝所有人访问
Require local ##允许本地用户访问
Require [not] host <主机名或域名列表> ##允许或者禁止某些主机或域名访问
Require [not] ip <IP地址或网段列表> ##允许或者禁止某些IP地址或网段访问
注意!!!
在apache主配置文件中;
需要将Require配置项添加在 容器中;
同时需要配置在<Directory “/var/www/html”> 配置段中,对该目录下的网页的访问进行限制
在这里插入图片描述

2.3用户授权控制

1.创建用户认证数据库
在这里插入图片描述

-c:(create)新建用户时使用
/etc/httpd/conf/user:新建保存用户信息的密码文件
tom:写在最后,新建的认证用户
2.添加用户授权配置
要将配置文件添加在<Directory “/var/www/html”>容器中
在这里插入图片描述

AuthName “DocumentRoot” ##指定受保护的领域名称(自己命名)
AuthType basic ##指定认证类型
AuthUserFile /etc/httpd/conf/user ##指定用户认证账号文件
Require valid-user ##要求通过认证才能访问
访问控制验证:
在这里插入图片描述

三、日志分割

●随着网站的访问量增加,默认情况下Apache的单个日志文件也会越来越大
日志文件占用磁盘空间很大
查看相关信息不方便
●对日志文件进行分割
Apache自带rotatelogs分割工具实现
第三方工具cronolog分割

3.1rotatelogs分割工具

date -s 09/05/20换日期
●配置网站的日志文件转交给rotatelogs分割处理
●配置格式
ErrorLog “I rotatelogs命令的绝对路径 -l 日志文件路径/网站名.error_%Y%m%d.log 86400”
…省略内容
CustomLog “| rotatelogs命令的绝对路径 -l 日志文件路径/网站名.access_%Y%m%d.log 86400” combined
#实际生产环境中,一个服务器绝大多数对应N个子域名站点,为了方便统一管理,可以用虚拟主机的方式进行配置,并用网站名标识日志文件

示例:

扫描二维码关注公众号,回复: 11649285 查看本文章
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf    ##进入apache的主配置文件
...省略内容
ErrorLog "| /usr/sbin/rotatelogs -l logs/www.test.com.error_%Y%m%d.log 86400"

CustomLog "| /usr/sbin/rotatelogs -l logs/www.test.com.access_%Y%m%d.log 8640
0" combined
...省略内容

注意:
|后面有空格,%Y%m%d:指定年月日,86400s=24h
错误日志服务启动就生成;访问日志在访问网站后才能生成
在这里插入图片描述

3.2第三方工具cronolog

●rpm安装cronolog工具
1.将准备好的cronolog软件包放进服务器系统中
2.rpm -ivh 安装cronolog工具
●配置网站日志文件转交给cronolog分割处理
配置格式
ErrorLog “l cronolog命令的绝对路径 日志文件路径/网站名.error_%Y%m%d.log”

CustomLog “I cronolog命令的绝对路径 日志文件路径/网站名.access_%Y%m%d.log” combined
示例:

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf    ##进入apache的主配置文件
...省略内容
ErrorLog "| /usr/sbin/cronolog  logs/www.test.com.error_%Y%m%d.log"

CustomLog "| /usr/sbin/cronolog  logs/www.test.com.access_%Y%m%d.log" combined
...省略内容

注意:
和apache自带的rotatelogs工具比,配置换了cronolog命令的绝对路径,少了-l和86400

四、AWStats日志分析系统介绍

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

部署AWStats日志分析系统详细过程

1.安装AWStats软件包

[root@localhost opt]# mkdir /usr/local/awstats
[root@localhost opt]# tar zxf awstats-7.6.tar.gz -C /usr/local/awstats

2.为要统计的站点建立配置文件

[root@localhost ~]# cd /usr/local/awstats/awstats-7.6/tools/
[root@localhost tools]# ls
awstats_buildstaticpages.pl  dolibarr            maillogconvert.pl   xslt
awstats_configure.pl         geoip_generator.pl  nginx
awstats_exportlib.pl         httpd_conf          urlaliasbuilder.pl
awstats_updateall.pl         logresolvemerge.pl  webmin
[root@localhost tools]# ./awstats_configure.pl     ##软件包解压后的tools工具中自带配置脚本
...省略内容
Enter full config file path of your Web server.
Config file path ('none' to skip web server setup):
> /etc/httpd/conf/httpd.conf         ##输入Web服务器的主配置文件地址
What is the name of your web site or profile analysis ?
Your web site, virtual server or profile name:
> www.test.com                         ##输入域名
...省略内容
-----> Create config file '/etc/awstats/awstats.www.test.com.conf'      ##为要统计的站点建立的配置文件
 Config file /etc/awstats/awstats.www.test.com.conf created.
...省略内容
You can also read your statistics for 'www.test.com' with URL:
> http://localhost/awstats/awstats.pl?config=www.test.com     ##AWStats日志分析系统的登录地址
其他操作都是y 或者 回车

3.编辑要统计的站点主配置文件参数

[root@localhost tools]# vim /etc/httpd/conf/httpd.conf 
...省略内容
<Directory "/usr/local/awstats/awstats-7.6/wwwroot">        ##搜索“awstats”可以快速定位
    Options None
    AllowOverride None
  #Order allow,deny              ##添加注释
  #Allow from all                   ##添加注释 
  Require all granted              ##允许所有人访问
</Directory>
...省略内容

4.修改刚才新建的站点统计配置文件

[root@localhost tools]# vim /etc/awstats/awstats.www.test.com.conf
...省略内容
LogFile="/var/log/httpd/access_log"            ##修改访问日志文件位置
...省略内容
DirData="/var/lib/awstats"                            ##awstats文件默认不存在,需要创建
...省略内容
[root@localhost tools]# mkdir /var/lib/awstats
[root@localhost tools]# systemctl start httpd

5.重启apache服务后,登录AWStats日志分析系统的地址

[root@localhost named]# systemctl restart httpd
http://www.test.com/awstats/awstats.pl?config=www.test.com

在这里插入图片描述

6.使用awstats软件包tools目录下的更新脚本更新数据

[root@localhost named]# cd /usr/local/awstats/awstats-7.6/tools/
[root@localhost tools]# ls
awstats_buildstaticpages.pl  dolibarr            maillogconvert.pl   xslt
awstats_configure.pl         geoip_generator.pl  nginx
awstats_exportlib.pl         httpd_conf          urlaliasbuilder.pl
awstats_updateall.pl         logresolvemerge.pl  webmin
[root@localhost tools]# ./awstats_updateall.pl now

在这里插入图片描述

7…/awstats_updateall.pl now每次手动输入不现实,可结合crontab计划性任务更新数据

[root@localhost tools]# crontab -e
*/5 * * * * /usr/local/awstats/tools/awstats_updateall.pl now

8.优化awstats网页地址

[root@localhost tools]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# vim aws.html
<html>
 <head>
  <meta http-equiv=refresh content="0;
   url=http://14.0.0.40/awstats/awstats.pl?config=www.test.com">
 </head>
 <body></body>
</html>
##这样就可以使用http://14.0.0.40/aws.html来访问了

猜你喜欢

转载自blog.csdn.net/chengu04/article/details/107827403