Building virtual host in CentOS 7 (Experiment)

Virtual Web host

  • Running multiple Web sites on the same physical server, each of which does not occupy a real computer independently

Types of virtual hosts supported by httpd

  • Domain name based virtual host
  • Virtual host based on IP address
  • Port based virtual host

Set up experiments

Domain name based virtual host

[root@localhost ~]# yum install bind httpd -y / / install DNS and HTTP services on the server.
[root@localhost ~]# cd /etc / / / enter etc directory
[root@localhost etc]# vim named.conf / / enter and edit the DNS service master configuration file.
...//Omit parts...
options {
        listen-on port 53 { any; };                    //Change the listening IP address to any and listen to all addresses
        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; };                      //Host name changed to any, allowing all hosts to pass resolution
...//Omit parts...
:wq                                                        //Save exit
[root@localhost etc]# vim named.rfc1912.zones / / enter the edit zone configuration file.
...//Omit parts...
zone "kgc.com" IN {                                  //rename domain
        type master;
        file "kgc.com.zone";                         //Change data file name
        allow-update { none; };
};

zone "aaa.com" IN {                                 //rename domain
        type master;
        file "aaa.com.zone";                      //Change data file name
        allow-update { none; };
};
...//Omit parts...
[root@localhost etc]# cd /var/named / / / enter the region 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 edit template
$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.144.133                        //Delete the last line and change it to this line
:wq                                                 //Save exit
[root@localhost named]# cp -p kgc.com.zone aaa.com.zone / / copy the data file you just changed and name it aaa.com.zone. You don't need to change the content.
[root@localhost named]# systemctl start named / / start DNS Service
[root@localhost named]# systemctl stop firewalld.service / / turn off the firewall
[root@localhost named]# setenforce 0 / / turn off enhanced security
[root@localhost html]# cd /etc/httpd/conf / / / enter the http service configuration file directory
[root@localhost conf]# mkdir extra / / create folder
[root@localhost conf]# ls / / view directory
extra  httpd.conf  magic
[root@localhost extra]# vim vhost.conf / / edit the sub configuration file
<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                                   //Edit virtual host configuration entry
  </Directory>
</VirtualHost>

<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>
~
:wq                                                 //Save exit
[root@localhost extra]# cd /var/www/html / / / enter http service website
[root@localhost html]# mkdir aaa kgc / / create directory file
[root@localhost html]# cd aaa / / / enter the directory
[root@localhost aaa]# vim index.html / / edit default home page
<h1>this is aaa web</h1>                        //Compiling content
~        
:wq                                              //Save exit
[root@localhost aaa]# ls / / view directory
index.html
[root@localhost aaa]# CD.. / kgc / / / return to the previous layer and enter the kgc directory
[root@localhost kgc]# vim index.html / / edit default web page
<h1>this is kgc web</h1>                    //Editorial content
~   
:wq                                          //Save exit
[root@localhost kgc]# ls / / view directory
index.html 
[root@localhost kgc]# vim /etc/httpd/conf/httpd.conf / / enter and edit the http service main configuration file.
...//Omit parts...
# prevent Apache from glomming onto all bound IP addresses.
#
Listen 192.168.144.137:80                                   //Turn on the ipv4 listening address and change it to the local IP address
#Listen 80 / / disable ipv6 listening address
...//Omit parts...
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
Include conf/extra/vhost.conf                             //Add the include sub profile directory entry on the last line
:wq                                                    //Save exit
[root@localhost kgc]# systemctl start httpd / / start http service
[root@localhost kgc]# netstat -ntap | grep 80 / / check whether port 80 is enabled.
tcp6       0      0 :::80                  :::*               LISTEN      2450/httpd 

Open a win10 client, change the DNS server address, open a web page, and test whether the virtual host based on different domain names is successful.

Build virtual host based on port

In this experiment, you can directly change the configuration in the above experiment without re operation.

[root@localhost named]# vim /etc/httpd/conf/extra/vhost.conf
...//Omit parts...
<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>
  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">   //Copy the above configuration file entry, change the listening port to 8080, and change the site directory and log file name to kgc02
    Require all granted
  </Directory>
</VirtualHost>
:wq                       //Save exit
[root@localhost named]# cd /var/www/html/
[root@localhost html]# mkdir kgc02
[root@localhost html]# cd kgc02/
[root@localhost kgc02]# vim index.html
<h1>this is kgc02 web</h1>
~
:wq
[root@localhost kgc02]# vim /etc/httpd/conf/httpd.conf
...//Omit parts...
# prevent Apache from glomming onto all bound IP addresses.
#
Listen 192.168.144.137:80
Listen 192.168.144.137:8080
#Listen 80
...//Omit parts...
:wq
[root@localhost kgc02]# systemctl restart httpd

Verifying port based virtual host configuration on win10 client

Establishing virtual host based on IP address

First, add a network card to the Linux server virtual machine to obtain another IP address.

Get IP address in virtual machine

[root@localhost ~]# ifconfig / / view the network card information
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: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.144.143  netmask 255.255.255.0  broadcast 192.168.144.255  //IP address obtained successfully
        inet6 fe80::d65e:47b1:916d:de6c  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:72:65:d5  txqueuelen 1000  (Ethernet)
        RX packets 115  bytes 20495 (20.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 79  bytes 17837 (17.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
...//Omit parts...
[root@localhost ~]# vim /etc/httpd/conf/extra/vhost.conf / / enter and edit the http service sub configuration file.
<VirtualHost 192.168.144.137:80>         //Change to fixed IP address
  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.144.143:80>           //Copy the configuration bar above and change the IP address
  DocumentRoot "/var/www/html/aaa02/"      //Change site file
  ServerName www.naaa.com                  //rename domain
  ErrorLog "logs/www.aaa02.com.error_log"  //Change error log file name
  CustomLog "logs/www.aaa02.com.access_log" common   //Change log file name
  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>
:wq                                       //Save exit
[root@localhost ~]# vim /etc/named.rfc1912.zones / / edit the DNS zone configuration file
...//Omit parts...
zone "aaa.com" IN {
        type master;
        file "aaa.com.zone";
        allow-update { none; };
};

zone "naaa.com" IN {
        type master;             //Add a new zone profile
        file "naaa.com.zone";
        allow-update { none; };
};
...//Omit parts...
:wq                               //Save exit
[root@localhost ~]# cd /var/named / / / enter the region data file storage directory
[root@localhost named]# cp -p aaa.com.zone naaa.com.zone / / copy the zone data file
[root@localhost named]# vim naaa.com.zone / / enter and edit the new zone data file
$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.144.143                //Change IP address
~
:wq                            //Save exit
[root@localhost named]# cd /var/www/html / / / enter http service site
[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 page content
:wq                                    //Save exit
[root@localhost aaa02]# vim /etc/httpd/conf/httpd.conf / / edit the http service main configuration file
...//Omit parts...
# prevent Apache from glomming onto all bound IP addresses.
#
Listen 192.168.144.137:80
Listen 192.168.144.143:80                       //Edit listen to new IP address
#Listen 80
...//Omit parts...
:wq                                          //Save exit
[root@localhost aaa02]# systemctl restart httpd / / restart the http service
[root@localhost aaa02]# systemctl restart named / / restart DNS Service

Verifying the virtual host based on IP address in win10 client

Configuration success

Tags: Linux vim DNS Apache network

Posted on Sat, 02 Nov 2019 05:26:27 -0700 by stan801003