CentOS 7 to construct virtual host (test piece)

Virtual Web Host

  • Run multiple Web sites on the same physical server, each site does not occupy a separate real computer

httpd supported virtual host type

  • Based virtual hosting
  • IP address-based virtual hosts
  • Based on the virtual host port

    Set up experiments

    Based virtual hosting

    
    [root@localhost ~]# yum install bind httpd -y        //在服务器上安装DNS与HTTP服务
    [root@localhost ~]# cd /etc/                         //进入etc目录
    [root@localhost etc]# vim named.conf                 //进入编辑DNS服务主配置文件
    ...//省略部分内容...
    options {
        listen-on port 53 { any; };                    //将监听IP地址更改为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; };                      //主机名更改为any,允许所有主机通过解析
    ...//省略部分内容...
    :wq                                                        //保存退出
    [root@localhost etc]# vim named.rfc1912.zones        //进入编辑区域配置文件
    ...//省略部分内容...
    zone "kgc.com" IN {                                  //更改域名
        type master;
        file "kgc.com.zone";                         //更改数据文件名称
        allow-update { none; };
    };

zone "aaa.com" IN {// change the domain
type Master;
File "aaa.com.zone"; // change the data file name
the allow-Update {none;};
};
... // omitted part .. .
[root @ localhost etc] # cd / var / named / // enter the zone data file storage directory
[root @ localhost named] # cp -p named.localhost kgc.com.zone // copy the zone data file template
[root @ localhost named] # vim kgc.com.zone // enter the edit template
$ the TTL 1D
@ @ the IN rname.invalid the SOA (.
0; Serial
1D; Refresh
IH; the retry
1W; The expire
3H); Minimum
the NS @
A 127.0.0.1
WWW iN a 192.168.144.133 // delete the last line, change to this line
: wq // save and exit
[root @ localhost named] # cp -p kgc.com.zone aaa.com.zone // copy the data files just changed named aaa.com.zone, do not need to change the content
[root @ localhost named] # systemctl start named // Start the DNS service
[root @ localhost named] # systemctl stop firewalld.service // disable the firewall
[root @ localhost named] # setenforce 0 // Close Enhanced security features

[root @ localhost html] # cd / etc / httpd / conf / // http enter service configuration file directory
[root @ localhost conf] # mkdir extra // Create a folder
[root @ localhost conf] # ls // View catalog
extra Magic the httpd.conf
[the root @ localhost Extra] // # Vim editing the files vhost.conf subprofile
<VirtualHost *: 80>
the DocumentRoot "/ var / WWW / HTML / AAA /"
ServerName www.aaa.com
the ErrorLog "logs / WWW .aaa.com.error_log "
the CustomLog" logs / www.aaa.com.access_log "Common
<Directory" / var / WWW / HTML ">
the Require All granted edit the virtual host configuration entries //
</ Directory>
</ VirtualHost>

<VirtualHost *: 80>
the DocumentRoot "/ var / WWW / HTML / KGC /"
ServerName www.kgc.com
the ErrorLog "logs / www.kgc.com.error_log"
the CustomLog "logs / www.kgc.com.access_log" Common
< Directory "/ var / the WWW / HTML">
the Require All granted
</ Directory>
</ VirtualHost>
~
: // WQ save and exit
[root @ localhost extra] # cd / var / www / html / // http service into the web site
[root @ localhost html] # mkdir aaa kgc // create a directory file
[root @ localhost html] # cd aaa / // enter the directory
[root @ localhost aaa] # vim index.html // edit the default home page
<h1> this is aaa web </ h1> // write the contents of
~
: // WQ save and exit
[root @ localhost aaa] # ls // View directory
index.html
[root @ localhost aaa] # cd ../ Kgc / // return to the previous directory and enter kgc
[root @ localhost kgc] # vim index.html // edit the default page
<h1> this is kgc web < / h1> // edits
~
: // WQ save and exit
[root @ localhost kgc] # ls // View catalog
index.html
[root @ localhost KGC] # vim /etc/httpd/conf/httpd.conf // http service into the main editing configuration files
... // ... omitted part

prevent Apache from glomming onto all bound IP addresses.

#
The Listen 192.168.144.137:80 // Open ipv4 listen address, and change the IP address of the machine-
#Listen 80 // closed ipv6 listen address
... // part omitted ...

Load config files in the "/etc/httpd/conf.d" directory, if any.

Conf.d IncludeOptional / .conf
the Include conf / Extra / vhost.conf // add configuration files contain sub-directory entry in the last line
: wq // save and exit
[root @ localhost kgc] # systemctl start httpd // http service turned on
[root @localhost kgc] # netstat -ntap | grep 80 // Check whether the port is open 80
tcp6 0 0 ::: 80 :::
LISTEN 2450 / httpd


**打开一台win10客户机,更改DNS服务器地址,打开网页,测试基于不同域名构建虚拟主机是否成功**

![](https://s1.51cto.com/images/blog/201910/30/8d15701bb483d3506fa530a9c2b68584.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)![](https://s1.51cto.com/images/blog/201910/30/6e3724159dcbc19ba4c4c3ccb223fdb4.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)![](https://s1.51cto.com/images/blog/201910/30/ccc10004f7f949d910fc0d6eb01f5826.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

### 基于端口建立虚拟主机

**此处实验就直接在上面的实验中更改配置,不重新操作**

[root@localhost named]# vim /etc/httpd/conf/extra/vhost.conf
...//省略部分内容...
<VirtualHost *:80>
DocumentRoot "/var/www/html/kgc/"
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 *: 8080>
the DocumentRoot "/ var / WWW / HTML / kgc02 /"
ServerName www.kgc.com
the ErrorLog "logs / www.kgc02.com.error_log"
the CustomLog "logs / www.kgc02.com.access_log" Common
< directory "/ var / www / html "> // copy the above configuration file entry, and change the listening port 8080, and site directory, log file name is changed to kgc02
the Require All granted
</ directory>
</ VirtualHost>
: WQ // save and exit
[root @ localhost named] # cd / var / the WWW / HTML /
[root @ localhost HTML] # mkdir kgc02
[root @ localhost HTML] # cd kgc02 /
[root @ localhost kgc02] # vim index.html
< h1 of> IS kgc02 the this Web </ h1 of>
~
: WQ
[kgc02 the root @ localhost] # Vim /etc/httpd/conf/httpd.conf
... // ... omitted part

prevent Apache from glomming onto all bound IP addresses.

#
The Listen 192.168.144.137:80
the Listen 192.168.144.137:8080
#Listen 80
... // part omitted ...
: WQ
[root @ localhost kgc02] # systemctl restart httpd


**在win10客户端验证基于端口的虚拟主机配置**

![](https://s1.51cto.com/images/blog/201910/30/440fb558e72104a84c2409ba8232396b.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)![](https://s1.51cto.com/images/blog/201910/30/7e3e16de5bb33a620b49e80bda76fa82.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

### 基于IP地址建立虚拟主机

**在这里先给Linux服务器虚拟机添加一张网卡,获取另一个IP地址**

![](https://s1.51cto.com/images/blog/201910/30/502e78433b4b3c63a1d7bcda851892cf.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

**在虚拟机中获取IP地址**

[root@localhost ~]# ifconfig //查看网卡信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.144.137 netmask 255.255.255.0 broadcast 192.168.144.255
inet6 fe80::a85a:c203:e2e:3f3c prefixlen 64 scopeid 0x20<link>
inet6 fe80::ad78:663f:1f02:22e4 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:72:65:cb txqueuelen 1000 (Ethernet)
RX packets 14117 bytes 10290025 (9.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6337 bytes 767788 (749.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

ens36: the flags = 4163 <the UP, BROADCAST, the RUNNING, the MULTICAST> 1500 MTU
inet Netmask 255.255.255.0 192.168.144.143 192.168.144.255 // Broadcast obtain an IP address
inet6 fe80 :: d65e: 47b1: 916d : de6c prefixlen 64 scopeid 0x20 < Link>
ether 00: 0c: 29: 72: 65: d5 of txqueuelen 1000 (Ethernet)
the RX packets 115 bytes 20495 (20.0 KiB)
the RX errors 0 Dropped 0 overruns 0 Frame 0
the TX packets 79 bytes 17837 (17.4 KiB)
the TX errors 0 Dropped Carrier overruns 0 0 Collisions 0 0
... // omitted part of ...
[root @ localhost ~] # vim /etc/httpd/conf/extra/vhost.conf enter edit http // sub-service configuration file
<VirtualHost 192.168 .144.137: 80> // change to a fixed IP address
DocumentRoot "/ var / the 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.144.143:80> // Copy the bar above configuration, change the IP address
DocumentRoot "/ var / www / html / aaa02 /" // Change site file
ServerName www.naaa.com // change the domain name
ErrorLog "logs / www .aaa02.com.error_log "// change the error log file name
CustomLog" logs / www.aaa02.com.access_log "common // change the log file name
<Directory" / var / the WWW / HTML ">
the Require All granted
</ Directory >
</ VirtualHost>
: // WQ save and exit
[root @ localhost ~] # vim /etc/named.rfc1912.zones // edit DNS zone configuration file
... // omitted part of ...
Zone "aaa.com "{the IN
type Master;
File" aaa.com.zone ";
the allow-Update {none;};
};

Zone "naaa.com" {the IN
type Master; // Add a new zone profile
File "naaa.com.zone";
the allow-Update {none;};
};
... // ... omitted part
: wq // save and exit
[root @ localhost ~] # cd / var / named / // enter the zone data file storage directory
[root @ localhost named] # cp -p aaa.com.zone naaa.com.zone // copy area data file
[root @ localhost named] # vim naaa.com.zone // enter new editing area data file
$ the TTL 1D
@ @ the iN rname.invalid the SOA (.
0; Serial
1D; Refresh
IH; the retry
1W; the expire
3H) ; Minimum
NS @
A 127.0.0.1
the WWW // change the IN A 192.168.144.143 IP address
~
: // WQ save and exit
[root @ localhost named] # cd / var / www / html / // http entering service sites
[root @ localhost html] # mkdir aaa02 // create a new site directory
[root @ localhost html] # cd aaa02 / // enter the directory
[root @ localhost aaa02] # vim index.html // edit the default home page file
<h1> this is 143 aaa02 web </ h1> // edit Web page content
: wq // save and exit
[root @ localhost aaa02] # vim /etc/httpd/conf/httpd.conf // http edit the service master configuration file
... // part omitted ...

prevent Apache from glomming onto all bound IP addresses.

#
The Listen 192.168.144.137:80
the Listen 192.168.144.143:80 // edit Monitoring the new IP address
#Listen 80
... // part omitted ...
: WQ // save and exit
[root @ localhost aaa02] # systemctl restart httpd // http restart the service
[root @ localhost aaa02] # systemctl restart named // restart the DNS service



**在win10客户机中验证基于IP地址建立的虚拟主机**

![](https://s1.51cto.com/images/blog/201910/30/982c238da37556a1d40fa918e6de92ea.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)![](https://s1.51cto.com/images/blog/201910/30/2b052b5d4498e8249cd8763c68acff67.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

### 配置成功

Guess you like

Origin blog.51cto.com/14473285/2446637