Apache虚拟主机的访问方式(基于域名、端口、IP)

构建虚拟主机

虚拟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
<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:基于域名访问虚拟主机

在虚拟机设置中添加网卡第二张网卡,并显示:

Apache虚拟主机的访问方式(基于域名、端口、IP)_第1张图片

[root@localhost ~]# 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

安装软件包:

[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 vhost.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保存退出
[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 1022 22:36 conf
drwxr-xr-x. 2 root root  82 1022 21:53 conf.d
drwxr-xr-x. 2 root root 146 1022 21:53 conf.modules.d
lrwxrwxrwx. 1 root root  19 1022 21:53 logs -> ../../var/log/httpd	//软链接在此目录下
lrwxrwxrwx. 1 root root  29 1022 21:53 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx. 1 root root  10 1022 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
<h1>this is accp web</h1>
输入完成后按Esc,输入:wq保存退出
[root@localhost accp]# cd ../kgc/
[root@localhost kgc]# vim index.html
<h1>this is kgc web</h1>
输入完成后按Esc,输入:wq保存退出
 
在主配置文件中的包含语句需要进行检查,因为之前的写入内容未在主配置文件中进行声明:
[root@localhost kgc]# cd /etc/httpd/
[root@localhost httpd]# cd conf/extra/
[root@localhost extra]# ls
vhost.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/vhost.conf
输入完成后按Esc,输入:wq保存退出 
[root@localhost extra]# systemctl start httpd

验证:用win7的浏览器,输入:www.accp.com / www.kgc.com

Apache虚拟主机的访问方式(基于域名、端口、IP)_第2张图片
Apache虚拟主机的访问方式(基于域名、端口、IP)_第3张图片

以上就是基于域名的方式来访问虚拟主机

——————————————————————————————————————————————————

二.构建虚拟主机 一 基于端口

应用示例:

1.构建两个个虚拟Web站点
www.kcce.com, IP地址、端口为173.17.17.11:80
www.kcce.com, IP地址、端口为173.17.17.11:8353
2.在浏览器中访问这两个端口时,分别显示不同的内容

[root@www ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
< VirtualHost 173.1 7.17.11:80>
	DocumentRoot "/var/www/htm/kccecom"
	ServerName www.kcce.com
</VirtualHost>
<VirtualHost 173.17.17.11:8353>
	DocumentRoot "/var/www/htm/kccepad"
	ServerName www.kcce.com
</virtualHost>

Listen 80
Listen 8353
#监听端口写在主配置文件中

Demo2:基于不同的端口进行访问

[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>
输入完成后按Esc,输入:wq保存退出

[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
<h1>this is kgc02 web</h1>
输入完成后按Esc,输入:wq保存退出

[root@localhost kgc02]# vim /etc/httpd/conf/httpd.conf
输入:/Listen查到到监听端口位置
Listen 192.168.56.131:80
Listen 192.168.56.131:8080
#Listen 80
此处需要做的更改的是把IPv6的监听给注释关闭,同时把IPv4的监听地址更换为自己Linux的IP地址,使用yy复制,在下行粘贴,将端口改为8080
#修改完成后按Esc,输入:wq保存退出
    
[root@localhost kgc02]# systemctl restart httpd			//重启服务
[root@localhost kgc02]# netstat -ntap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.56.131:8080     0.0.0.0:*               LISTEN      52732/httpd         
tcp        0      0 192.168.56.131:80       0.0.0.0:*               LISTEN      52732/httpd    
#此时可以看到两个端口都处于监听状态

在这里插入图片描述

验证:用win7的浏览器,输入: www.kgc.com:8080

此时显示的就是我们预先写好的8080端口的kgc02的网页主页
如果输入:www.kgc.com:80,那就还显示this is kgc web的界面
Apache虚拟主机的访问方式(基于域名、端口、IP)_第4张图片

以上就是基于端口的方式来访问虚拟主机

——————————————————————————————————————————————————

Demo3:基于不同的IP进行访问

此前我们有两张网卡地址分别为:192.168.56.131和192.168.56.134,此处需要用上

[root@localhost kgc02]# cd /etc/httpd/conf/extra/
[root@localhost extra]# ls
vhost.conf
[root@localhost extra]# vim vhost.conf
//11,29 s/^/#/g		//全局把11行到29行进行注释,此处不需要使用
<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,并将下面的accp改为accp02以作区分
  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>
#修改完成后按Esc,输入:wq保存退出

创建目录站点:

[root@localhost extra]# cd /var/www/html/
[root@localhost html]# mkdir accp02
[root@localhost html]# cd accp02/
[root@localhost accp02]# vim index.html
<h1>this is 134 accp02 web</h1>
#输入完成后按Esc,输入:wq保存退出

[root@localhost accp02]# vim ../accp/index.html
<h1>this is 131 accp web</h1>
#修改完成后按Esc,输入:wq保存退出

修改监听地址:

[root@localhost accp02]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.56.131:80
Listen 192.168.56.134:80
#Listen 192.168.56.131:80808080端口进行注释,并添加一行192.168.56.13480端口监听
#修改完成后按Esc,输入:wq保存退出
 
[root@localhost accp02]# systemctl restart httpd

验证:用win7的浏览器,输入: 192.168.56.131 / 192.168.56.134

Apache虚拟主机的访问方式(基于域名、端口、IP)_第5张图片
Apache虚拟主机的访问方式(基于域名、端口、IP)_第6张图片

修改DNS:

[root@localhost accp02]# cd /etc/httpd/conf/extra/
[root@localhost extra]# vim vhost.conf 
<VirtualHost 192.168.56.131: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>
 
<VirtualHost 192.168.56.134: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/">
    Require all granted
  </Directory>
</VirtualHost>
#以上两段需要添加ServerName

添加新区域:

[root@localhost extra]# vim /etc/named.rfc1912.zones
zone "naccp.com" IN {
        type master;
        file "naccp.com.zone";
        allow-update { none; };
};
#添加以上内容
#修改完成后按Esc,输入:wq保存退出

[root@localhost extra]# cd /var/named/
[root@localhost named]# cp -p accp.com.zone naccp.com.zone
[root@localhost named]# vim naccp.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.134		//IP地址改为二网卡的地址
#修改完成后按Esc,输入:wq保存退出

[root@localhost named]# systemctl restart named
[root@localhost named]# systemctl restart httpd			//重启服务

验证:用win7的浏览器,输入: www.accp.com / www.naccp.com

Apache虚拟主机的访问方式(基于域名、端口、IP)_第7张图片
Apache虚拟主机的访问方式(基于域名、端口、IP)_第8张图片

以上就是基于不同IP的方式访问虚拟主机

你可能感兴趣的:(基于IP,基于域名,基于端口,网络,Linux)