CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

虚拟Web主机

在同一台物理服务器中运行多个Web站点,其中每一一个站点并不独立占用一台真正的计算机。

httpd支持的虚拟主机类型

  • 基于域名的虚拟主机
  • 基于IP地址的虚拟主机
  • 基于端口的虚拟主机

构建虚拟主机------基于域名

(1)安装bind、httpd服务。

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(2)进入named服务的主配置文件,将下图两个位置改为“any”。

[[email protected] ~]# vim /etc/named.conf

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(3)进入named服务的区域配置文件,添加两个域名的区域信息。

[[email protected] ~]# vim /etc/named.rfc1912.zones

zone "aaa.com" IN {
        type master;
        file "aaa.com.zone";
        allow-update { none; };
};

zone "bbb.com" IN {
        type master;
        file "bbb.com.zone";
        allow-update { none; };
};

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(4)进入“/var/named/”目录,递归复制一份“named.localhost”区域数据配置文件,命名为“aaa.com.zone”,然后对其进行修改。

[[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  aaa.com.zone
[[email protected] named]# 

[[email protected] named]# vim aaa.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.52.133

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(5)再复制一份“aaa.com.zone”文件,命名为“bbb.com.zone”,不用进行修改。然后开启named服务,关闭防火墙和增强性安全功能。

[[email protected] named]# cp -p aaa.com.zone bbb.com.zone
[[email protected] named]# systemctl start named
[[email protected] named]# 
[[email protected] named]# systemctl stop firewalld.service 
[[email protected] named]# setenforce 0
[[email protected] named]#

(6)再开一台win10虚拟机,将其DNS服务器的IP地址,设置为刚才Linux系统的IP地址。

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(7)用win10主机去测试DNS服务能否解析,解析成功。

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(8)进入“/etc/httpd/conf”目录,创建一个“extra/”目录,然后进入“extra/”目录,用vim编辑器,新建一个配置文件“vhost.conf”,在配置文件里输入以下内容。

[[email protected] named]# cd /etc/httpd/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 vhost.conf

<VirtualHost *:80>
  DocumentRoot "/var/www/html/aaa/"
  ServerName www.aaa.com
  ErrorLog "logs/www.aaa.com.error_log"
  CustomLog "logs/www.aaa.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot "/var/www/html/bbb/"
  ServerName www.bbb.com
  ErrorLog "logs/www.bbb.com.error_log"
  CustomLog "logs/www.bbb.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(9)进入“/var/www/html/”创建两个目录“aaa/”、“bbb/”。

[[email protected] extra]# 
[[email protected] extra]# cd /var/www/html/
[[email protected] html]# ls
[[email protected] html]# mkdir aaa bbb
[[email protected] html]# ls
aaa  bbb
[[email protected] html]#

(10)进入“aaa/”目录,新建一个站点首页文件,内容如下:

[[email protected] html]# cd aaa
[[email protected] aaa]# ls
[[email protected] aaa]# vim index.html

<h1>this is aaa web</h1>

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(11)进入“bbb/”目录,新建一个站点首页文件,内容如下:

[[email protected] aaa]# cd ../bbb
[[email protected] bbb]# ls
[[email protected] bbb]# vim index.html

<h1>this is bbb web</h1>

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(12)进入httpd服务的主配置文件,在末行将我们新建的配置文件写进主配置文件,然后启动httpd服务。

[[email protected] bbb]# vim /etc/httpd/conf/httpd.conf 

Include conf/extra/vhost.conf

[[email protected] bbb]# systemctl start httpd
[[email protected] bbb]#

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(13)用win10 主机去分别访问两个域名,都能访问成功。

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

构建虚拟主机------基于端口

(1)在上一个实验的基础上,进入配置文件“vhost.conf”,添加一个“www.aaa.com”域名的8080端口。

[[email protected] bbb]# vim /etc/httpd/conf/extra/vhost.conf 

<VirtualHost *:80>
  DocumentRoot "/var/www/html/aaa/"
  ServerName www.aaa.com
  ErrorLog "logs/www.aaa.com.error_log"
  CustomLog "logs/www.aaa.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot "/var/www/html/bbb/"
  ServerName www.bbb.com
  ErrorLog "logs/www.bbb.com.error_log"
  CustomLog "logs/www.bbb.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:8080>
  DocumentRoot "/var/www/html/aaa02/"
  ServerName www.aaa.com
  ErrorLog "logs/www.aaa02.com.error_log"
  CustomLog "logs/www.aaa02.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(2)进入“/var/www/html”目录,新建一个“aaa02”目录,进入“aaa02”目录,新建一个站点首页文件,内容如下:

[[email protected] bbb]# cd ../
[[email protected] html]# mkdir aaa02
[[email protected] html]# cd aaa02/
[[email protected] aaa02]# vim index.html

<h1>this is aaa02 web</h1>

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(3)进入httpd服务主配置文件,添加监听端口,同时将IPv6的端口的监听注销。重启httpd服务。

[[email protected] aaa02]# vim /etc/httpd/conf/httpd.conf

Listen 192.168.52.133:80
Listen 192.168.52.133:8080
#Listen 80

[[email protected] aaa02]# systemctl restart httpd
[[email protected] aaa02]#

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(4)再次用win10主机访问两个端口不同的域名,访问成功。

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

构建虚拟主机------基于IP

(1)给Linux主机添加一块网卡,查看IP地址。

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(2)进入配置文件“vhost.conf”,进行如下输入:

[[email protected] aaa02]# vim /etc/httpd/conf/extra/vhost.conf

<VirtualHost 192.168.52.133:80>
  DocumentRoot "/var/www/html/aaa/"
  ErrorLog "logs/www.aaa.com.error_log"
  CustomLog "logs/www.aaa.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost 192.168.52.139:80>
  DocumentRoot "/var/www/html/aaa02/"
  ErrorLog "logs/www.aaa02.com.error_log"
  CustomLog "logs/www.aaa02.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(3)分别对“aaa”站点与“aaa02”站点的主页文件进行如下修改:

[[email protected] aaa02]# cd ../aaa
[[email protected] aaa]# vim index.html 

<h1>this is 133 aaa web</h1>

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

[[email protected] aaa]# cd ../aaa02
[[email protected] aaa02]# vim index.html 

<h1>this is 139 aaa02 web</h1>

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(4)进入httpd主配置文件,进行端口的添加与注释。然后重启httpd服务。

[[email protected] aaa02]# vim /etc/httpd/conf/httpd.conf 

Listen 192.168.52.133:80
Listen 192.168.52.139:80
#Listen 192.168.52.133:8080
#Listen 80

[[email protected] aaa02]# systemctl restart httpd
[[email protected] aaa02]#

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(5)用win10主机去访问两个不同IP地址的站点,访问成功。但是只能用IP地址访问,一般情况访问网站用的是域名,接下来我们进行域名访问不同IP地址站点的配置。

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(6)首先在配置文件“vhost.conf”中添加,域名“ServerName”。

[[email protected] aaa02]# vim /etc/httpd/conf/extra/vhost.conf

<VirtualHost 192.168.52.133:80>
  DocumentRoot "/var/www/html/aaa/"
  ServerName www.aaa.com
  ErrorLog "logs/www.aaa.com.error_log"
  CustomLog "logs/www.aaa.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost 192.168.52.139:80>
  DocumentRoot "/var/www/html/aaa02/"
  ServerName www.aaa02.com
  ErrorLog "logs/www.aaa02.com.error_log"
  CustomLog "logs/www.aaa02.com.access_log" common
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(7)进入named服务的区域配置文件中,添加一个“aaa02”的区域信息。

[[email protected] aaa02]# vim /etc/named.rfc1912.zones 

zone "aaa.com" IN {
        type master;
        file "aaa.com.zone";
        allow-update { none; };
};

zone "aaa02.com" IN {
        type master;
        file "aaa02.com.zone";
        allow-update { none; };
};

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(8)进入“/var/named/”目录,递归复制一份“aaa.com.zone”文件,命名为“aaa02.com.zone”,同时对其进行如下修改:

[[email protected] aaa02]# cd /var/named/
[[email protected] named]# ls
aaa.com.zone  data     named.ca     named.localhost  slaves
bbb.com.zone  dynamic  named.empty  named.loopback
[[email protected] named]# cp -p aaa.com.zone aaa02.com.zone
[[email protected] named]# vim aaa02.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.52.139

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

(9)再次用win10主机,通过域名去访问两个不同IP地址的站点,访问成功。

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

CentOS7构建虚拟Web主机(基于域名、端口、IP地址)

相关文章
相关标签/搜索