使用Require配置项实现访问控制,按先后顺序限制
可用,,,配置段中
Require配置项的常见语法
Require all granted
Require all denied
Require local
Require [not] host <主机名或域名列表>
Require [not] ip
'//使用not禁止访问时要将其置于 容器中,并在容器中指定相对应的限制策略'
举例
[root@localhost ~]# cd /etc/httpd/conf
[root@localhost conf]# ls
httpd.conf magic
[root@localhost conf]# mkdir abc
[root@localhost conf]# ls
abc httpd.conf magic
[root@localhost conf]# cd abc
[root@localhost abc]# vim vhost.conf
:80>
...省略内容
"/var/www/html">
Require not ip 192.168.100.100 '//表示不允许ip192.168.100.100访问'
Require all granted
...省略内容
创建用户认证数据库
命令基本格式
'//htpasswd命令是httpd自带的'
htpasswd -c /etc/httpd/conf/abc webadmin '//为用户webadmin创建密码文件'
New password:'//输入密码'
Re-type new password:'//重复输入密码'
cat /etc/httpd/conf/abc
webadmin:加密密码
举例
[root@localhost abc]# htpasswd -c /etc/httpd/conf/aaa ddd
New password:
Re-type new password:
Adding password for user ddd
[root@localhost abc]# cat /etc/httpd/conf/aaa
ddd:$apr1$zwIklPGy$ZINaIkXsSq36oIk2lAiiJ1
'//添加第二个用户'
[root@localhost abc]# htpasswd /etc/httpd/conf/aaa bbb
New password:
Re-type new password:
Adding password for user bbb
[root@localhost abc]# cat /etc/httpd/conf/aaa
ddd:$apr1$zwIklPGy$ZINaIkXsSq36oIk2lAiiJ1
bbb:$apr1$q7OdS8z1$ELGg2a1AhHP.QfXJXfQTQ1
'//配置基本格式'
"/var/www/html">
AuthName "DocumentRoot" '//受保护的领域名称'
AuthType Basic '//认证类型'
AuthUserFile /etc/httpd/conf/qwe '//用户认证账号文件'
Require valid-user '//要求通过认证才能访问'
举例
[root@localhost abc]# vim vhost.conf
...省略内容
:80>
DocumentRoot "/var/www/html/ccc"
ServerName www.ccc.com
Errorlog "logs/www.ccc.com.error_log"
Customlog "logs/www.ccc.com.custom_log" common
"/var/www/html">
AuthName "DocumentRoot"
AuthType Basic
AuthUserFile /etc/httpd/conf/aaa
Require valid-user
...省略内容
配置网站的日志文件转交给rotatelogs分割处理
配置格式为
ErrorLog "| rotatelogs 命令的绝对路径 -l 日志文件路径/网站名-error_%Y%m%d.log 86400" '//which rotatelogs命令查看绝对路径,%Y%m%d表示年月日,86400表示一天的秒数'
CustomLog "| rotatelogs 命令的绝对路径 -l 日志文件路径/网站名-access_%Y%m%d.log 86400" combined
例如
[root@localhost logs]vim /etc/httpd/conf/httpd.conf
ErrorLog "| /usr/sbin/rotatelogs -l logs/error_%Y%m%d.log 86400"
CustomLog "| /usr/sbin/rotatelogs -l logs/access_%Y%m%d.log 86400" combined
环境部署
VMware软件
一台centos7虚拟机
一台Windows虚拟机
环境准备
WindowsDNS解析地址指向centos7
centos7安装bind和httpd
配置dns服务
[root@localhost opt]# vim /etc/named.conf
options {
listen-on port 53 { any; }; '//修改成any'
...省略内容
allow-query { any; }; '//修改成any'
[root@localhost ~]# vim /etc/named.rfc1912.zones
zone "dabao.com" IN {
type master;
file "dabao.com.zone";
allow-update { none; };
};
[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost dabao.com.zone
[root@localhost named]# vim dabao.com.zone
'//尾行修改为下面内容'
www IN A 192.168.197.143
[root@localhost named]# systemctl start named
配置httpd
[root@localhost named]# vim /etc/httpd/conf/httpd.conf
'//修改此两行内容'
Listen 192.168.197.143:80
#Listen 80
'//修改为指定域名'
ServerName www.dabao.com:80
[root@localhost named]# systemctl stop firewalld.service
[root@localhost named]# setenforce 0
[root@localhost named]# systemctl start httpd.service
安装AWStats
[root@localhost ~]# mount.cifs //192.168.100.3/ccc /opt
Password for root@//192.168.100.3/ccc:
[root@localhost ~]# df -h
[root@localhost ~]# cd /opt
[root@localhost opt]# tar zxvf awstats-7.6.tar.gz -C /mnt
[root@localhost opt]# cd /mnt
[root@localhost mnt]# ls
awstats-7.6
[root@localhost mnt]# mv awstats-7.6/ /usr/local/awstats
[root@localhost mnt]# ls
[root@localhost mnt]# cd /usr/local
[root@localhost local]# ls
awstats bin etc games include lib lib64 libexec sbin share src
[root@localhost local]# cd awstats/
[root@localhost awstats]# ls
docs README.md tools wwwroot
[root@localhost awstats]# cd tools/
[root@localhost tools]# ls
[root@localhost tools]# ./awstats_configure.pl
> /etc/httpd/conf/httpd.conf '//第一次交互输入此路径'
y '//第二次交互输入y'
www.dabao.com '//第三次交互输入指定域名'
'//第四五六次交互直接回车'
[root@localhost tools]# cd /etc/httpd/conf
[root@localhost conf]# vim httpd.conf
'//省略内容,直接到文件末尾修改此配置'
"/usr/local/awstats/wwwroot">
Options None
AllowOverride None
# Order allow,deny
# Allow from all
Require all granted
[root@localhost conf]# cd /etc/awstats/
[root@localhost awstats]# ls
awstats.www.dabao.com.conf
[root@localhost awstats]# vim awstats.www.dabao.com.conf
...省略内容
'//将下面两行内容修改'
LogFile="/var/log/httpd/access_log"
...省略内容
DirData="/var/lib/awstats"
[root@localhost awstats]# cd /var/lib
[root@localhost lib]# ls
[root@localhost lib]# mkdir awstats
[root@localhost lib]# systemctl restart httpd.service
尝试访问awstats网页
使用刚刚交互生成的网址访问,将其中的localhost修改为指定域名
更新网页访问数据的统计
[root@localhost lib]# cd /usr/local/awstats/
[root@localhost awstats]# ls
docs README.md tools wwwroot
[root@localhost awstats]# cd tools/
[root@localhost tools]# ls
...省略内容
[root@localhost tools]# ./awstats_updateall.pl now
...省略内容
再次访问网站,数据统计成功
[root@localhost tools]# crontab -e
'//添加一下内容'
*/3 * * * * /usr/local/awstats/tools/awstats_updateall.pl now
[root@localhost tools]# systemctl start crond
You have new mail in /var/spool/mail/root
[root@localhost tools]# cd /var/www/html
[root@localhost html]# vim aws.html
"0;url=http://www.dabao.com/awstats/awstats.pl?config=www.dabao.com">
此时可以直接使用www.dabao.com/aws.html访问网页
[root@localhost ~]# vim /etc/http/conf/xxx/vhost.conf
1 :80> '//针对本服务器所有IP地址'
2 DocumentRoot "/var/www/html/xxx" '//指定站点目录'
3 ServerName www.xxx.com '//指定域名'
4 Errorlog "logs/www.xxx.com.error_log" '//指定错误日志路径'
5 Customlog "logs/www.xxx.com.custom_log" common '//指定访问日志路径'
6 "/var/www/html">
7 Require all granted '//设置网页允许所有主机访问,也可以通过denied设置拒绝访问'
8
9
VMware软件
一台Windows虚拟机,IP地址为192.168.197.133
一台centos7虚拟机,IP地址为192.168.197.141
centos7安装bind和httpd服务
[root@localhost ~]# yum install bind httpd -y
关闭防火墙,编辑主配置文件
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/named.conf
...省略内容
options {
listen-on port 53 { any; }; '//修改为any'
listen-on-v6 port 53 { ::1; };
...省略内容
allow-query { any; }; '//修改为any'
...省略内容
编辑区域配置文件
[root@localhost ~]# vim /etc/named.rfc1912.zones
'//添加一下两端内容'
...省略内容
zone "dabao.com" IN {
type master;
file "dabao.com.zone";
allow-update { none; };
};
zone "erbao.com" IN {
type master;
file "erbao.com.zone";
allow-update { none; };
};
...省略内容
编辑区域数据文件
[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@localhost named]# cp -p named.localhost dabao.com.zone
[root@localhost named]# vim dabao.com.zone
...省略内容
'//末尾添加内容'
www IN A 192.168.197.141
[root@localhost named]# cp -p dabao.com.zone erbao.com.zone
开启服务并测试
[root@localhost named]# systemctl start named
创建站点,并添加虚拟主机配置
[root@localhost named]# cd /etc/httpd/conf
[root@localhost conf]# ls
httpd.conf magic
[root@localhost conf]# mkdir baobao
[root@localhost conf]# ls
baobao httpd.conf magic
[root@localhost conf]# cd baobao
[root@localhost baobao]# ls
[root@localhost baobao]# vim vhost.conf
1 :80>
2 DocumentRoot "/var/www/html/dabao"
3 ServerName www.dabao.com
4 Errorlog "logs/www.dabao.com.error_log"
5 Customlog "logs/www.dabao.com.custom_log" common
6 "/var/www/html">
7 Require all granted
8
9
10
11 :80>
12 DocumentRoot "/var/www/html/erbao"
13 ServerName www.erbao.com
14 Errorlog "logs/www.erbao.com.error_log"
15 Customlog "logs/www.erbao.com.custom_log" common
16 "/var/www/html">
17 Require all granted
18
19
编辑虚拟机网页文档
[root@localhost baobao]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# mkdir dabao erbao
[root@localhost html]# ls
dabao erbao
[root@localhost html]# echo "this is dabao" > dabao/index.html
[root@localhost html]# echo "this is erbao" > erbao/index.html
[root@localhost html]# yum install tree
[root@localhost html]# tree /var/www/html/
/var/www/html/
├── dabao
│ └── index.html
└── erbao
└── index.html
2 directories, 2 files
编辑httpd主配置文件,添加虚拟主机站点
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
'//找到此行'
IncludeOptional conf.d/*.conf
Include conf/baobao/vhost.conf '//添加此行内容'
开启httpd服务,并测试
[root@localhost html]# systemctl start httpd
配置虚拟主机配置
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# systemctl start httpd
[root@localhost html]# cd /etc/httpd/conf
[root@localhost conf]# ls
baobao httpd.conf magic
[root@localhost conf]# cd baobao
[root@localhost baobao]# ls
vhost.conf
[root@localhost baobao]# vim vhost.conf
'//在第二个主机配置后添加80'
:8080> '//多写一个80'
设置监听地址
[root@localhost baobao]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.197.141:80
Listen 192.168.197.141:8080 '//找到监听地址,开启IPv4监听,设置监听地址,添加一个8080端口的监听地址'
#Listen 80 '//关闭IPv6监听'
重启服务,验证
[root@localhost baobao]# systemctl restart httpd.service
虚拟主机配置
[root@localhost baobao]# vim vhost.conf
:80> '//将*修改为第一张网卡IP地址'
:80> '//将*修改为第二张网卡IP地址'
'//将他们的域名都注释掉'
设置监听地址
[root@localhost baobao]# vim /etc/httpd/conf/httpd.conf
...省略内容
Listen 192.168.197.141:80
Listen 192.168.197.180:80
#Listen 80
...省略内容
重启服务,验证
[root@localhost baobao]# systemctl restart httpd