Apache配置与应用

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


HTTP协议是基于TCP协议之上的,在进行HTTP连接之前,要先进行TCP连接,每个TCP连接都要进行三次握手与四次挥手。建立与关闭连接对于HTTP协议而言会消耗很多的内存与CPU资源。
解决办法是HTTP连接保持。就是尽量地保持客户端的连接,通过一个TCP连接传送多个HTTP请求响应,对于客户端可以提高50%以上的响应时间,对于服务器可以降低建立与关闭连接时的资源消耗。

保持连接的方式

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

Apache访问控制

Apache访问控制概述

作用

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

常用访问控制方式

    客户机地址限制
    用户授权限制

基于客户端地址的访问控制
使用Require配置项实现访问控制,按先后顺序限制
可用、、、配置段中

Require配置项的常见语法

Require all granted
Require all denied
Require local
Require [not] host <主机名或域名列表>
Require [not] ip <IP地址或网段列表>
使用not禁止访问时要将其置于容器中,并在容器中指定相对应的限制策略

用户授权限制–创建用户认证数据库
创建用户认证数据库
命令基本格式
‘htpasswd命令是httpd自带的’
htpasswd -c /etc/httpd/conf/abc webadmin ‘为用户webadmin创建密码文件’
New password: ‘输入密码’
Re-type new password: ‘重复输入密码’
cat /etc/httpd/conf/abc
webadmin:加密密码

实验

[root@localhost extra]# vim vhost.conf
在这里插入图片描述

[root@localhost extra]# systemctl restart httpd.service

在这里插入图片描述

[root@localhost extra]# which htpasswd
/usr/bin/htpasswd
[root@localhost extra]# htpasswd -c /etc/httpd/conf/pwd huifang
New password:
Re-type new password:
Adding password for user huifang
[root@localhost extra]# cd /etc/httpd/conf/
[root@localhost conf]# cat pwd
huifang: a p r 1 apr1 apr1McmIYi/K$Y.gzEWM6I5DHFr58uY94.0
在这里插入图片描述

[root@localhost conf]# systemctl restart httpd.service
在这里插入图片描述
在这里插入图片描述

Apache日志管理

日志分割

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

rotatelogs分割工具

配置网站的日志文件转交给rotatelogs分割处理

实际生产环境中,一个服务器绝大多数对应N个子域名站点,为了方便统一管理,可以用虚拟主机的方式进行配置,并用网站名标识日志文件

日志文件的产生

服务安装后,不会生成日志文件不会产生
服务启动后,生成日志文件
访问服务后,日志文件会生成内容

实验

[root@localhost ~]# yum -y install httpd 
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf 
[root@localhost ~]# which rotatelogs 
/usr/sbin/rotatelogs
 41 Listen 192.168.20.20:80
 42 #Listen 80

 95 ServerName www.hui.com:80

182 ErrorLog "| /usr/sbin/rotatelogs -l logs/www.hui.com.error_%Y%m%d.log 86400"
217     CustomLog "| /usr/sbin/rotatelogs -l logs/www.hui.com.access_%Y%m%d.log 86400" combined
[root@localhost ~]# systemctl start httpd.service 
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# iptables -F
[root@localhost ~]# ls /var/log/httpd/
www.hui.com.error_20200805.log

在这里插入图片描述

[root@localhost ~]# ls /var/log/httpd/
access_log  error_log  www.hui.com.access_20200805.log  www.hui.com.error_20200805.log

在这里插入图片描述

[root@localhost ~]# date -s 09/05/20
20200905日 星期六 00:00:00 CST
[root@localhost ~]# systemctl restart httpd.service 
[root@localhost ~]# ls /var/log/httpd/
access_log  www.hui.com.access_20200805.log  www.hui.com.error_20200805.log
error_log   www.hui.com.access_20200905.log  www.hui.com.error_20200905.log

在这里插入图片描述

[root@localhost ~]# rm -rf /var/log/httpd/www*
[root@localhost ~]# cd /opt
cronolog-1.6.2-14.el7.x86_64.rpm
[root@localhost opt]# rpm -ivh cronolog-1.6.2-14.el7.x86_64.rpm 
[root@localhost opt]# which cronolog 
/usr/sbin/cronolog
[root@localhost opt]# vim /etc/httpd/conf/httpd.conf 
183 ErrorLog "| /usr/sbin/cronolog logs/www.hui.com.error_%Y%m%d.log"
217     CustomLog "| /usr/sbin/cronolog logs/www.hui.com.access_%Y%m%d.log" combined
[root@localhost opt]# systemctl restart httpd.service 
[root@localhost opt]# ls /var/log/httpd/
access_log  www.hui.com.access_20200905.log
error_log   www.hui.com.error_20200905.log

在这里插入图片描述

[root@localhost ~]# yum -y install httpd bind
[root@localhost ~]# vim /etc/named.conf 
13         listen-on port 53 { any; };
21         allow-query     { any; };

在这里插入图片描述

[root@localhost ~]# vim /etc/named.rfc1912.zones 
 25 zone "hui.com" IN {
 26         type master;
 27         file "hui.com.zone";
 28         allow-update { none; };
 29 };  

在这里插入图片描述

[root@localhost ~]# cp -p /var/named/named.localhost /var/named/hui.com.zone
[root@localhost ~]# vim /var/named/hui.com.zone
 10 www IN  A       192.168.20.20

在这里插入图片描述

[root@localhost ~]# systemctl start named
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0

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

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf 
 41 Listen 192.168.20.20:80
 42 #Listen 80

在这里插入图片描述

 95 ServerName www.hui.com:80

在这里插入图片描述

[root@localhost ~]# vim /var/www/html/index.html
<h1>this is hui</h1>

[root@localhost ~]# cd /opt
awstats-7.6.tar.gz
[root@localhost opt]# tar zvxf awstats-7.6.tar.gz 
[root@localhost opt]# mv awstats-7.6 /usr/local/awstats
[root@localhost opt]# cd /usr/local/awstats/tools/
[root@localhost tools]# ./awstats_configure.pl 
> /etc/httpd/conf/httpd.conf

在这里插入图片描述

file (required if first install) [y/N] ? y

在这里插入图片描述

> www.hui.com

在这里插入图片描述

[root@localhost tools]# vim /etc/httpd/conf/httpd.conf 
<Directory "/usr/local/awstats/wwwroot">
    Options None
    AllowOverride None
   #Order allow,deny
   #Allow from all
    Require all granted

在这里插入图片描述

[root@localhost tools]# cd /etc/awstats/
[root@localhost awstats]# ls
awstats.www.hui.com.conf
[root@localhost awstats]# vim awstats.www.hui.com.conf         
  50 LogFile="/var/log/httpd/access_log"

在这里插入图片描述

[root@localhost awstats]# cd /var/lib/
[root@localhost lib]# mkdir awstats
[root@localhost lib]# systemctl start httpd.service 

在这里插入图片描述

[root@localhost lib]# cd /usr/local/awstats/tools/
[root@localhost tools]# ./awstats_updateall.pl now

在这里插入图片描述

[root@localhost tools]# crontab -e
*/5 * * * * /usr/local/awstats/tools/awstats_updataeall.pl now
[root@localhost tools]# cd /var/www/html/
[root@localhost html]# vim aws.html
<html>
 <head>
  <meta http-equiv=refresh content="0;url=http://www.hui.com/awstats/awstats.pl?c
onfig=www.hui.com">
 </head>
 <body></body>
</html>

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

[root@localhost html]# systemctl restart httpd.service 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值