apache配置与应用

apache连接保持

相关参数
KeepAlive 是否打开连接保持,off关闭,on打开
KeepAlive Timeout 一次连接多次请求之间最大间隔时间,两次请求超过时间连接断开
MaxKeepAliveRequests 一次长连接能够传输最大请求数量

apache访问控制概述

作用
控制对网站资源访问
作为特定网站目录添加访问授权
常用控制方式
客户机地址限制
用户授权限制
在这里插入图片描述

连接保持

apache安装完成后进入主配置文件

vi /usr/local/httpd/conf/httpd.conf
lnclude conf/extra/httpd-default.conf  ##将前面#去掉开启该模块

之后配置连接保持

vi /usr/local/httpd/conf/extra/httpd-default.conf
KeepAlive On   ##保持连接开启(默认开启)
MaxKeepAliveRequests 200  ##一次连接最多请求200个文件(默认100)
KeepAliveTimeout 10  ##无响应超时踢下线时间设置10秒

设置完成重启httpd服务

systemctl restart httpd

客户机限制

通过Require配置项,可以根据客户端主机名或IP地址来决定是否允许客户端访问,在httpd服务的主配置文件location directory files limit 配置段种均可以使用require配置来控制客户端访问。

require all granted 允许所有主机访问
require all denied 拒绝所有主机访问
require local 仅允许本地主机访问
require [not] host <主机名或域名列表> 允许或拒绝指定主机或域名访问;
require [not] ip <IP地址或网段列表> 允许或拒绝指定IP地址网络访问

通常情况下网站服务器是对所有的客户机开放的,网页文档目录并未作任何限制,因此使用“require all granted”策略,表示允许从任何客户机访问策略
在这里插入图片描述

如果规定地址访问
在这里插入图片描述

用户授权

httpd 服务器支持使用摘要认证(Digest)和基本认证(Basic)两种方式。使用摘要认 证需要在编译 httpd 之前添加“–enable-auth-digest”选项,但并不是所有的浏览器都支持摘要认证(Digest);而基本认证(Basic)是 httpd 服务的基本功能,不需要预先配置特别的选项。
httpd的基本认证通过校验用户名、密码组合来判断是否允许用户访问。授权访问的用户账号需要事先建立,并保存在固定的数据文件中。使用专门的htpasswd工具程序,可以创建授权用户数据文件,并维护其中的用户账号

[root@localhost conf]# /usr/local/httpd/bin/htpasswd -c /usr/local/httpd/conf/.awspwd weixin
                    //-c创建文件,为Apache用户weixin设置登入验证密码
New password:  //在此输入密码
Re-type new password: 
Adding password for user weixin
[root@localhost conf]# /usr/local/httpd/bin/htpasswd /usr/local/httpd/conf/.awspwd weixin1
                   //上面已经创建了文件,所有这里不用-c了
New password:  //在此输入密码
Re-type new password: 
Adding password for user weixin1
[root@localhost conf]# cat /usr/local/httpd/conf/.awspwd   //查看用户数据文件
weixin:$apr1$f9ASKCFP$bR1a5zuq0DS0Z4dxQCrhq0
weixin1:$aadr1$f9DHCFP$bSHJGDuq0DS0Z4dxQCrh
[root@localhost opt]# vi /usr/local/httpd/conf/extra/httpd-vhosts.conf //配置访问验证(这里就用虚拟web主机做配置了)注意:要开启主配置文件中虚拟主机模块
<VirtualHost 20.0.0.11:80>
    DocumentRoot "/opt/www/html/51xit.top"
    ServerName www.51xit.top
    ErrorLog "logs/www.51xit.top.error_log"
    CustomLog "logs/www.51xit.top.access_log" common
    <Directory "/opt/www/html">
	####在访问控制模块配置以下内容
      AuthName "DocumentRoot"
      AuthType Basic
      AuthUserFile /usr/local/httpd/conf/.awspwd
      Require valid-user
   ###############################
    </Directory>
</VirtualHost>

解释
Authname 定义保护领域名称,该内容将在浏览器弹出认证对话框显示
Authtype 设置认证类型,basic表示基本认证
Authuserfile 设置用于保存用户账号、密码认证文件路径
require valid -user 要求只有认证文件中的合法用户才能访问。其中,valid-user 表示 所有合法用户,若只授权给单个用户,可改为指定的用户名 

apache 日志分割

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

使用自带工具

 [root@localhost local]# vi /usr/local/httpd/conf/httpd.conf 
ErrorLog "|/usr/local/bin/rotatelogs -l /var/log/httpd/error-log_%Y%m%d.log 86400"
CustomLog "|/usr/local/httpd/bin/rotatelogs -l /var/log/httpd/access_%Y%m%d.log 86400" combined

使用第三方工具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
[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" combined
[root@localhost~]# systemctl restart httpd

Awstats分析系统

在 httpd 服务器的访问日志文件 access_log 中,记录了大量的客户机访问信息,通过 分析这些信息,可以及时了解 Web 站点的访问情况,如每天或特定时间段的访问 IP 数量, 点击量最大的页面等。
AWStats是使用Perl语言开发的一款开源日志分析系统,它不仅可用来分析Apache 网 站服务器的访问日志,也可以用来分析 Samba、Vsftpd、IIS 等服务的日志信息。结合 crond 等计划任务服务,可以对不断增长的日志内容定期进行分析

安装awstats软件包

[root@localhost opt]# tar zxf awstats-7.7.tar.gz
[root@localhost opt]# mv /opt/awstats-7.7 /usr/local/awstats

统计站点建立配置文件

[root@localhost opt]# cd /usr/local/awstats
[root@localhost awstats]# ls
docs  README.md  tools  wwwroot
[root@localhost awstats]# cd /usr/local/awstats/tools/
[root@localhost tools]# ./awstats_configure.pl
 Config file path ('none' to skip web server setup):
> /usr/local/httpd/conf/httpd.conf

Do you want me to setup Apache to write 'combined' log files [y/N] ? y

Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ? y

Your web site, virtual server or profile name:
> www.51xit.top

Default: /etc/awstats
Directory path to store config file(s) (Enter for default):
>   //默认回车即可

/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue...   //这里继续回车

> perl awstats.pl -update -config=www.51xit.top   
You can also read your statistics for 'www.51xit.top' with URL:
> http://localhost/awstats/awstats.pl?config=www.51xit.top  //这个生成的用于登入Awstats日志分析系统的连接,将localhost改为自己的主机ip,复制下来后面测试用

Press ENTER to finish... //继续回车
[root@localhost tools]# vim /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  //去掉前面的”#”号

<Directory "/usr/local/awstats/wwwroot">   //主配置文件中自动生成了awstats模块
    Options None
     AllowOverride None
#    Order allow,deny   //注释掉
#    Allow from all      //注释掉
     Require all granted   //新增这条,允许所有访问
</Directory>
[root@localhost tools]# systemctl restart httpd

修改站点统计配置文件

root@localhost tools]# vim /etc/awstats/awstats.www.51xit.top.conf //编辑awstats配置文件

LogFile="/usr/local/httpd/logs/access_log"   //指定访问日志文件路径

DirData="/var/lib/awstats"           //设置awstats数据文件目录,默认为/var/lib/awstats
[root@localhost tools]# mkdir /var/lib/awstats  //创建awstats分析系统的数据文件目录
[root@localhost tools]# chmod +x awstats_updateall.pl 
[root@localhost tools]# ./awstats_updateall.pl  //启动更新(这条有时不管用,用下面这条)
#[root@localhost tools]# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.51xit.top 
                          //后面的跟监听的域名 

测试
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值