构建虚拟主机
虚拟Web主机:
1.在同一-台物理服务器中运行多个Web站点,其中每一个站点并不独立占用一台真正的计算机
httpd支持的虚拟主机类型:
1.基于域名的虚拟主机(企业常用)
2.基于IP地址的虚拟主机
3.基于端口的虚拟主机
大型公司实例项目:
两台物理主机(A/B)
两台主机中分别有两个虚拟站点
主机A和B中都有accp网站和kgc网站
网站内容完全相同,对外提供业务时,既用到了合理分配,也起到了双机热备
学习一台主机架构两个网站
构建虚拟主机一 基于域名
应用示例:
1.构建2个虚拟Web站点
www.kgc.com, IP地址为173.17.17.11
www.kcce.com, IP地址为173.17.17.11
2.在浏览器中访问这两个域名时,分别显示不同的内容
为虚拟主机提供域名解析:
[root@dnssvr ~]# vim /var/named/chroot/var/named/kgc.com.zone
@ IN NS dnssvr.kgc.com.
dnssvr IN A 173.1 7.1 7.2
www IN A 173.17.17.11
[root@dnssvr ~]# vim /var/named/chroot/var/named/kcce.com.zone
@ IN NS dnssvr.kgc.com.
www IN A 1 73.1 7.1 7.1 1
为虚拟主机准备网页文档:
[root@www ~]# mkdir -p /var/www/html/kgccom
[root@www ~]# mkdir -p /var/www/html/kccecom
[root@www ~]# echo " www.kgc.com
" >
/var/www/html/kgccom/index.html
[root@www ~]# echo " www.kcce.com
" >
/var/www/html/kccecom/index.html
添加虚拟主机配置:
[root@www ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
DocumentRoot "/var/www/html/kgccom"
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
DocumentRoot "/var/www/html/kccecom"
ServerName www.kcce.com
在客户机分别访问虚拟Web主机
Demo1:
在虚拟机设置中添加网卡第二张网卡,并显示:
[root@localhost ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.56.131 netmask 255.255.255.0 broadcast 192.168.56.255
inet6 fe80::a7c6:cf70:62f2:faf0 prefixlen 64 scopeid 0x20
ether 00:0c:29:86:f9:b2 txqueuelen 1000 (Ethernet)
RX packets 667004 bytes 977833518 (932.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 165563 bytes 10169247 (9.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens36: flags=4163 mtu 1500
inet 192.168.56.134 netmask 255.255.255.0 broadcast 192.168.56.255
inet6 fe80::9ce4:351a:2c74:9ad5 prefixlen 64 scopeid 0x20
ether 00:0c:29:86:f9:bc txqueuelen 1000 (Ethernet)
RX packets 216 bytes 17516 (17.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 26 bytes 4391 (4.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
//目前主机中有两张网卡:ens33和ens36,地址分别为:192.168.56.131和 192.168.56.134
安装软件包:
[root@localhost ~]# yum install bind httpd -y
修改DNS的配置文件:
[root@localhost ~]# vim /etc/named.conf
options {
listen-on port 53 { any; }; //其中的127.0.0.1改为any
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; }; //其中的locahost改为any
修改完成后按Esc,输入:wq保存退出
修改区域配置文件:
[root@localhost ~]# vim /etc/named.rfc1912.zones
//找到以下格式,按5yy复制五行,在任意一个5行的最后一行按o在下一行插入,修改其中域名
zone "accp.com" IN {
type master;
file "accp.com.zone";
allow-update { none; };
};
zone "kgc.com" IN {
type master;
file "kgc.com.zone";
allow-update { none; };
};
修改完成后按Esc,输入:wq保存退出
修改区域数据配置文件:
[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 accp.com.zone
[root@localhost named]# vim accp.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.56.131 //此处为添加的主机A记录,地址指向自己
//需要删除IPv6的解析
修改完成后按Esc,输入:wq保存退出
[root@localhost named]# cp -p accp.com.zone kgc.com.zone
[root@localhost named]# vim kgc.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.56.131
//此处的文件内容不需要做任何修改
可直接输入:q退出
//为方便后面测试解析,接下来我们启动服务,并关闭防火墙及安全功能
[root@localhost named]# systemctl start named
[root@localhost named]# systemctl stop firewalld.service
[root@localhost named]# setenforce 0
此时可以测试是否可以进行DNS解析:
此处我们使用一台win7虚拟机进行DNS的解析:
先需要在网络的更改适配器设置中对IPv4的DNS地址进行修改:192.168.56.131
//此处修改的是DNS主机的地址
再使用管理员身份运行cmd命令:
C:\Windows\system32>nslookup www.accp.com
服务器: UnKnown
Address: 192.168.56.131
名称: www.accp.com
Address: 192.168.56.131
C:\Windows\system32>nslookup www.kgc.com
服务器: UnKnown
Address: 192.168.56.131
名称: www.kgc.com
Address: 192.168.56.131
//此时通过验证表明DNS的解析是没有问题的,接下来我们就可以做网站了
做虚拟主机配置文件:
[root@localhost named]# cd /etc/httpd
[root@localhost httpd]# ls
conf conf.d conf.modules.d logs modules run
[root@localhost httpd]# cd conf
[root@localhost conf]# ls
httpd.conf magic
[root@localhost conf]# mkdir extra
[root@localhost conf]# ls
extra httpd.conf magic
[root@localhost conf]# cd extra/
[root@localhost extra]# vim host.conf
DocumentRoot "/var/www/html/accp/" //站点目录
ServerName www.accp.com //域名
ErrorLog "logs/www.accp.com.error_log" //错误日志
CustomLog "logs/www.accp.com.access_log" common //访问日志
Require all granted //访问权限为允许所有
按Esc退出插入模式,输入:11,19 s/accp/kgc/g //把从11行到19行把其中的accp改成kgc,做全局替换
11
12 DocumentRoot "/var/www/html/kgc/"
13 ServerName www.kgc.com
14 ErrorLog "logs/www.kgc.com.error_log"
15 CustomLog "logs/www.kgc.com.access_log" common
16
17 Require all granted
18
19
修改完成后按Esc,输入:wq保存退出
[root@localhost extra]# cd ../../
[root@localhost httpd]# ls
conf conf.d conf.modules.d logs modules run
[root@localhost httpd]# pwd
/etc/httpd //参考点,以工作目录为参考
[root@localhost httpd]# ls -l
总用量 0
drwxr-xr-x. 3 root root 50 10月 22 22:36 conf
drwxr-xr-x. 2 root root 82 10月 22 21:53 conf.d
drwxr-xr-x. 2 root root 146 10月 22 21:53 conf.modules.d
lrwxrwxrwx. 1 root root 19 10月 22 21:53 logs -> ../../var/log/httpd //软链接在此目录下
lrwxrwxrwx. 1 root root 29 10月 22 21:53 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx. 1 root root 10 10月 22 21:53 run -> /run/httpd
[root@localhost httpd]# cd /var/log/httpd/
[root@localhost httpd]# ls
//此时是空的,因为没有启动,所以没有日志文件
创建站点:
[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]# ls
[root@localhost accp]# vim index.html
this is accp web
输入完成后按Esc,输入:wq保存退出
[root@localhost accp]# cd ../kgc/
[root@localhost kgc]# vim index.html
this is kgc web
输入完成后按Esc,输入:wq保存退出
在主配置文件中的包含语句需要进行检查,因为之前的写入内容未在主配置文件中进行声明:
[root@localhost kgc]# cd /etc/httpd/
[root@localhost httpd]# cd conf/extra/
[root@localhost extra]# ls
host.conf
[root@localhost extra]# pwd
/etc/httpd/conf/extra
[root@localhost extra]# cd ../../
[root@localhost httpd]# ls
conf conf.d conf.modules.d logs modules run
[root@localhost httpd]# pwd
/etc/httpd
[root@localhost httpd]# cd conf
[root@localhost conf]# ls
extra httpd.conf magic
[root@localhost conf]# vim httpd.conf
//按大写字母G到末行,按o在下行插入
Include conf/extra/host.conf
输入完成后按Esc,输入:wq保存退出
[root@localhost extra]# systemctl start httpd