系统运维
实验需求:安装Apache与dns服务
给虚拟机增加两张网卡
[root@localhost ~]# yum install bind httpd -y
增加网卡
构建虚拟主机一 基于域名访问
1.为虚拟主机提供域名解析
[root@localhost ~]# vim /etc/named.conf //配置DNS主配置文件
[root@localhost ~]# vim /etc/named.rfc1912.zones //配置DNS区域配置文件
[root@localhost ~]# cd /var/named/
[root@ named]# cp -p named.localhost kgc.com.zone
[root@localhost named]# vim kgc.com.zone //配置DNS区域数据配置文件[root@localhost named]# cp -p kgc.com.zone accp.com.zone
//这里的DNS区域数据配置文件无需再修改
[root@localhost named]# systemctl start named //启动DNS服务
[root@localhost named]# systemctl stop firewalld.service //关闭防火墙
[root@localhost named]# setenforce 0
2、为虚拟主机准备网页文档
[root@localhost httpd]# cd /var/www/html
[root@localhost html]# ls
[root@localhost html]# mkdir accp kgc
[root@localhost html]# ls
accp kgc
[root@localhost html]# cd accp/
[root@localhost accp]# vim index.html
<h2>this is accp web</h2> //输入网页内容
[root@localhost accp]# cd ../kgc/
[root@localhost kgc]# vim index.htm
<h2>this is kgc web</h2> //输入网页内容
[root@localhost ~]# cd /etc/httpd/
[root@localhost httpd]# cd conf
[root@localhost conf]# vim httpd.conf
找到监听端口注释ipv6端口,开启ipv4端口,并将监听地址修改为本机地址。
Include conf/extra/vhost.conf //按大写字母G到末行,按o在下行插入此内容
3、添加虚拟主机配置
[root@localhost named]# cd /etc/httpd/
[root@localhost httpd]# cd conf
[root@localhost conf]# mkdir extra
[root@localhost conf]# ls
extra httpd.conf magic
[root@localhost conf]# cd extra/
[root@localhost extra]# vim vhost.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/kgc/" //站点目录
ServerName www.kgc.com //域名
ErrorLog "logs/www.kgc.com.error_log" //错误日志
CustomLog "logs/www.kgc.com.access_log" common //访问日志
<Directory "/var/www/html">
Require all granted //访问权限为允许所有
</Directory>
</VirtualHost><VirtualHost *:80>
DocumentRoot "/var/www/html/accp/"
ServerName www.accp.com
ErrorLog "logs/www.accp.com.error_log"
CustomLog "logs/www.accp.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
[root@localhost conf]# systemctl start httpd //启动服务
4、在客户机分别访问虚拟Web主机
在测试机win10分别输入网址:www.kgc.com www.accp.com
构建虚拟主机一 基于端口访问
1、添加虚拟主机配置
[root@localhost extra]# vim vhost.conf
<VirtualHost *:8080> //在虚拟主机配置文件添加以下内容
DocumentRoot "/var/www/html/kgc02/"
ServerName www.kgc.com
ErrorLog "logs/www.kgc02.com.error_log"
CustomLog "logs/www.kgc02.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
2、为虚拟主机准备网页文档
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# ls
accp kgc
[root@localhost html]# mkdir kgc02
[root@localhost html]# ls
accp kgc kgc02
[root@localhost html]# cd kgc02/
[root@localhost kgc02]# vim index.html
<h2>this is kgc02 web</h2> //输入网页内容
3、进入Apache配置文件添加端口
vim /etc/httpd/conf/httpd.conf
输入/Listen查找到配置文件中监听端口位置
输入:
Listen 192.168.131.129:80
Listen 192.168.131.129:8080[root@localhost extra]# systemctl restart httpd //重启服务
4、在客户机分别访问虚拟Web主机
在测试机win10输入www.kgc.com:8080 进行测试
在测试机win10输入www.kgc.com 则默认端口号为80
构建虚拟主机一 基于IP
1、添加虚拟主机配置
[root@localhost kgc02]# cd /etc/httpd/conf/extra/
[root@localhost extra]# ls
vhost.conf
[root@localhost extra]# vim vhost.conf //进入虚拟主机配置添加以下内容
VirtualHost 192.168.56.131:80> //基于ip访问,因此需要把改为本地地址
DocumentRoot "/var/www/html/accp/"
ErrorLog "logs/www.accp.com.error_log"
CustomLog "logs/www.accp.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.56.134:80> //把本行改为第二块网卡的IP地址
DocumentRoot "/var/www/html/accp02/"
ErrorLog "logs/www.accp02.com.error_log"
CustomLog "logs/www.accp02.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
2、为虚拟主机准备网页文档
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# mkdir accp02
[root@localhost html]# cd accp02/
[root@localhost accp02]# vim index.html
<h2>this is accp02 web</h2> //输入网页内容
[root@localhost extra]# systemctl restart httpd //重启服务3、在客户机分别访问虚拟Web主机
在测试机win10分别输入:192.168.131.129 /192.168.131.130进行测试
但在实际情况中,我们访问网站一般都是使用域名,所以我们还需要为新增的IP地址提供域名解析服务。
修改DNS区域配置文件
vim /etc/named.rfc1912.zones //在DNS区域配置文件添加新的内容
修改DNS数据配置文件
[root@localhost extra]# cd /var/named/
[root@localhost named]# cp -p accp.com.zone naccp.com.zone //复制区域数据配置文件模板
[root@localhost named]# vim naccp.com.zone //进入区域数据配置文件进行修改[root@localhost extra]# systemctl restart httpd //重启服务
在测试机win10分别输入:www.kgc.com /www.naccp.com 进行测试
构建虚拟主机一 基于权限
在虚拟主机配置中添加黑白名单
[root@localhost named]# cd /etc/httpd/conf/extra
[root@localhost extra]# ls
vhost.conf
[root@localhost extra]# vim vhost.conf
<VirtualHost 192.168.131.129:80>
DocumentRoot "/var/www/html/accp/"
ServerName www.accp.com
ErrorLog "logs/www.accp.com.error_log"
CustomLog "logs/www.accp.com.access_log" common
<Directory "/var/www/html/">
<RequireAll>
Require not ip 192.168.131.128 //添加测试机IP地址为不允许访问accp.com
Require all granted
</RequireAll>
</Directory>
</VirtualHost>[root@localhost extra]# systemctl restart httpd //重启服务
验证黑白名单
使用测试机win10分别访问:www.kgc.com / www.naccp.com
naccp.com可以正常访问,而kgc.com则无法访问,直接跳转到Apache默认网页
密码验证访问Apache网页
[root@localhost conf]# cd /etc/httpd/conf
[root@localhost conf]# htpasswd -c /etc/httpd/conf/pwd tese01 //创建访问用户
New //设置访密码
Re-type new //重复输入密码
Adding password for user tese01
[root@localhost conf]# ls
extra httpd.conf magic pwd
[root@localhost conf]# cat pwd
tese01:$apr1$.HU1/Ltw$Isp7l.Qhe9Zlh81OU8e7M1 //密码密文
[root@localhost conf]# cd extra/
[root@localhost extra]# ls
vhost.conf
[root@localhost extra]# vim vhost.conf //进入虚拟主机配置文件配置以下内容
<VirtualHost 192.168.131.130:80>
DocumentRoot "/var/www/html/accp02/"
ServerName www.naccp.com
ErrorLog "logs/www.accp02.com.error_log"
CustomLog "logs/www.accp02.com.access_log" common
<Directory "/var/www/html/">
AuthName "DocumentRoot"
AuthType Basic
AuthUserFile /etc/httpd/conf/pwd
Require valid-user
</Directory>
</VirtualHost>[root@localhost extra]# systemctl restart httpd //重启服务
使用测试机win10输入:www.naccp.com 进行测试
此时我们若再想访问naccp.com就需要输入密码进行验证了
更多关于云服务器,域名注册,虚拟主机的问题,请访问西部数码官网:www.west.cn