构建虚拟主机
虚拟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.在浏览器中访问这两个域名时,分别显示不同的内容
为虚拟主机提供域名解析:
[[email protected] ~]# 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
[[email protected] ~]# 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
为虚拟主机准备网页文档:
[[email protected] ~]# mkdir -p /var/www/html/kgccom
[[email protected] ~]# mkdir -p /var/www/html/kccecom
[[email protected] ~]# echo "<h1 > www.kgc.com</h1>" >
/var/www/html/kgccom/index.html
[[email protected] ~]# echo "<h1 > www.kcce.com </h1>" >
/var/www/html/kccecom/index.html
添加虚拟主机配置:
[[email protected] ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
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
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/kccecom"
ServerName www.kcce.com
<VirtualHost>
在客户机分别访问虚拟Web主机
Demo1:
在虚拟机设置中添加网卡第二张网卡,并显示:
[[email protected] ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> 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<link>
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<UP,BROADCAST,RUNNING,MULTICAST> 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<link>
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
安装软件包:
[[email protected] ~]# yum install bind httpd -y
修改DNS的配置文件:
[[email protected] ~]# 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保存退出
修改区域配置文件:
[[email protected] ~]# 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保存退出
修改区域数据配置文件:
[[email protected] ~]# cd /var/named/
[[email protected] named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[[email protected] named]# cp -p named.localhost accp.com.zone
[[email protected] 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保存退出
[[email protected] named]# cp -p accp.com.zone kgc.com.zone
[[email protected] 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退出
//为方便后面测试解析,接下来我们启动服务,并关闭防火墙及安全功能
[[email protected] named]# systemctl start named
[[email protected] named]# systemctl stop firewalld.service
[[email protected] 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的解析是没有问题的,接下来我们就可以做网站了
做虚拟主机配置文件:
[[email protected] named]# cd /etc/httpd
[[email protected] httpd]# ls
conf conf.d conf.modules.d logs modules run
[[email protected] httpd]# cd conf
[[email protected] conf]# ls
httpd.conf magic
[[email protected] conf]# mkdir extra
[[email protected] conf]# ls
extra httpd.conf magic
[[email protected] conf]# cd extra/
[[email protected] extra]# vim host.conf
<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>
按Esc退出插入模式,输入:11,19 s/accp/kgc/g //把从11行到19行把其中的accp改成kgc,做全局替换
11 <VirtualHost *:80>
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 <Directory "/var/www/html/">
17 Require all granted
18 </Directory>
19 </VirtualHost>
修改完成后按Esc,输入:wq保存退出
[[email protected] extra]# cd ../../
[[email protected] httpd]# ls
conf conf.d conf.modules.d logs modules run
[[email protected] httpd]# pwd
/etc/httpd //参考点,以工作目录为参考
[[email protected] 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
[[email protected] httpd]# cd /var/log/httpd/
[[email protected] httpd]# ls
//此时是空的,因为没有启动,所以没有日志文件
创建站点:
[[email protected] httpd]# cd /var/www/html/
[[email protected] html]# ls
[[email protected] html]# mkdir accp kgc
[[email protected] html]# ls
accp kgc
[[email protected] html]# cd accp/
[[email protected] accp]# ls
[[email protected] accp]# vim index.html
<h1>this is accp web</h1>
输入完成后按Esc,输入:wq保存退出
[[email protected] accp]# cd ../kgc/
[[email protected] kgc]# vim index.html
<h1>this is kgc web</h1>
输入完成后按Esc,输入:wq保存退出
在主配置文件中的包含语句需要进行检查,因为之前的写入内容未在主配置文件中进行声明:
[[email protected] kgc]# cd /etc/httpd/
[[email protected] httpd]# cd conf/extra/
[[email protected] extra]# ls
host.conf
[[email protected] extra]# pwd
/etc/httpd/conf/extra
[[email protected] extra]# cd ../../
[[email protected] httpd]# ls
conf conf.d conf.modules.d logs modules run
[[email protected] httpd]# pwd
/etc/httpd
[[email protected] httpd]# cd conf
[[email protected] conf]# ls
extra httpd.conf magic
[[email protected] conf]# vim httpd.conf
//按大写字母G到末行,按o在下行插入
Include conf/extra/host.conf
输入完成后按Esc,输入:wq保存退出
[[email protected] extra]# systemctl start httpd
验证,用win7的浏览器,输入:www.accp.com / www.kgc.com
以上就是基于域名的方式来创建虚拟主机
原文地址:https://blog.51cto.com/14464303/2444614
时间: 2024-10-29 10:01:51