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

虚拟主机

虚拟主机(英语:virtual hosting)或称共享主机(shared web hosting),又称虚拟服务器,是一种在单一主机或主机群上,实现多网域服务的方法,可以运行多个网站或服务的技术。虚拟主机之间完全独立,并可由用户自行管理,虚拟并非指不存在,而是指空间是由实体的服务器延伸而来,其硬件系统可以是基于服务器群,或者单个服务器。

  • 虚拟Web主机
    在同一台服务器中运行多个Web站点,其中每一个站点并不独立占用一台真正的计算机
  • httpd支持的虚拟主机类型
    • 基于域名的虚拟主机
    • 基于IP地址的虚拟主机
    • 基于端口的虚拟主机

基于域名构建虚拟主机

构建2个虚拟Web站点
域名www.aaa.com,IP地址为192.168.110.5
域名www.bbb.com,IP地址为192.168.110.5
端口80

安装Apache和DNS软件包

[root@promote ~]# yum -y install httpd bind
配置DNS服务
[root@promote ~]# vim /etc/named.conf 
options {
        listen-on port 53 { 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; };
[root@promote ~]# 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; };
};
[root@promote ~]# cd /var/named/
[root@promote named]# cp -p named.localhost aaa.com.zone
[root@promote 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.110.5
[root@promote named]# cp -p aaa.com.zone bbb.com.zone
[root@promote named]# systemctl start named

打开一台win10,配置ip地址和DNS
在这里插入图片描述
在这里插入图片描述

配置httpd服务
[root@promote ~]# cd /etc/httpd/conf
[root@promote conf]# mkdir ab
[root@promote conf]# cd ab
[root@promotee ab]# 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>
[root@promote ab]# cd /var/www/html/
[root@promote html]# mkdir aaa bbb
[root@promote html]# vim aaa/index.html 
<h1>aaa</h1>
[root@promote html]# vim bbb/index.html 
<h1>bbb</h1>
[root@promote html]# vim /etc/httpd/conf/httpd.conf 
Include conf/ab/vhost.conf
[root@promote html]# systemctl restart httpd.service 

在这里插入图片描述
在这里插入图片描述

基于端口构建虚拟主机

www.bbb.com,IP地址、端口为192.168.110.5:80
www.bbb2.com,IP地址、端口为192.168.110.5:8080

[root@promote html]# cd /etc/httpd/conf
[root@promote conf]# vim ab/vhost.conf
<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/bbb2"
   ServerName www.bbb2.com
   ErrorLog "logs/www.bbb2.com.error_log"
   CustomLog "logs/www.bbb2.com.access_log" common
   <Directory "/var/www/html">
       Require all granted
   </Directory>
</VirtualHost>
[root@promote conf]# vim /etc/httpd/conf/httpd.conf 
Listen 192.168.110.5:80
Listen 192.168.110.5:8080
[root@promote conf]# cd /var/www/html/
[root@promote html]# mkdir bbb2
[root@promote html]# vim bbb2/index.html 
<h1>bbb2</h1>
[root@promote html]# systemctl restart httpd.service

在这里插入图片描述

基于IP地址构建虚拟主机

www.aaa.com,IP地址为192.168.110.5,网卡为ens33
www.bbb.com,IP地址为192.168.110.7,网卡为ens36
在这里插入图片描述

[root@promote html]# /etc/httpd/conf/kkc/vhost.conf 
 <VirtualHost 192.168.100.5: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.110.7: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>
[root@promote html]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.110.5:80
Listen 192.168.110.7:80
[root@promote html]# systemctl restart httpd

在这里插入图片描述
在这里插入图片描述

©️2020 CSDN 皮肤主题: 我行我“速” 设计师:Amelia_0503 返回首页