CentOS7 builds virtual Web host (based on domain name, port, IP address)

Keywords: Linux vim DNS firewall

Virtual Web host

Running multiple Web sites in the same physical server, each site 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

Build virtual host -- Based on domain name

(1) install bind and httpd services.

(2) enter the main configuration file of the named service, and change the following two locations to "any".

[root@localhost ~]# vim /etc/named.conf

(3) enter the region configuration file of the named service and add the region information of the two domain names.

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

(4) enter "/ var/named /" directory, and copy a "named.localhost" area data configuration file with permission, named "aaa.com.zone", and then modify it.

[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  aaa.com.zone
[root@localhost named]# 

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

(5) reserve the right to copy a "aaa.com.zone" file, named "bbb.com.zone", without modification. Then turn on the named service, turn off the firewall and enhanced security features.

[root@localhost named]# cp -p aaa.com.zone bbb.com.zone
[root@localhost named]# systemctl start named
[root@localhost named]# 
[root@localhost named]# systemctl stop firewalld.service 
[root@localhost named]# setenforce 0
[root@localhost named]# 

(6) open another win10 virtual machine and set the IP address of its DNS server to the IP address of the Linux system just now.

(7) use win10 host to test whether DNS service can resolve successfully.

(8) enter the "/ etc/httpd/conf" directory, create an "extra /" directory, then enter the "extra /" directory, use vim editor, create a new configuration file "vhost.conf", and enter the following contents in the configuration file.

[root@localhost named]# cd /etc/httpd/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/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>

(9) enter "/ var/www/html /" to create two directories: "aaa /", "bbb /".

[root@localhost extra]# 
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# mkdir aaa bbb
[root@localhost html]# ls
aaa  bbb
[root@localhost html]# 

(10) enter the "aaa /" directory, and create a new website homepage file, with the contents as follows:

[root@localhost html]# cd aaa
[root@localhost aaa]# ls
[root@localhost aaa]# vim index.html

<h1>this is aaa web</h1>

(11) enter the "bbb /" directory, and create a new website homepage file, with the contents as follows:

[root@localhost aaa]# cd ../bbb
[root@localhost bbb]# ls
[root@localhost bbb]# vim index.html

<h1>this is bbb web</h1>

(12) enter the main configuration file of httpd service, write our new configuration file into the main configuration file at the end line, and then start the httpd service.

[root@localhost bbb]# vim /etc/httpd/conf/httpd.conf 

Include conf/extra/vhost.conf

[root@localhost bbb]# systemctl start httpd
[root@localhost bbb]# 

(13) using win10 host to access two domain names respectively can be accessed successfully.

Build virtual host based on port

(1) on the basis of the previous experiment, enter the configuration file "vhost.conf" and add a port 8080 of "www.aaa.com" domain name.

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

(2) enter the "/ var/www/html" directory, create a new "aaa02" directory, enter the "aaa02" directory, and create a new website homepage file, with the contents as follows:

[root@localhost bbb]# cd ../
[root@localhost html]# mkdir aaa02
[root@localhost html]# cd aaa02/
[root@localhost aaa02]# vim index.html

<h1>this is aaa02 web</h1>

(3) enter the httpd service main configuration file, add the listening port, and log off the IPv6 port listening. Restart the httpd service.

[root@localhost aaa02]# vim /etc/httpd/conf/httpd.conf

Listen 192.168.52.133:80
Listen 192.168.52.133:8080
#Listen 80

[root@localhost aaa02]# systemctl restart httpd
[root@localhost aaa02]# 

(4) use win10 host again to access different domain names of two ports, and the access is successful.

Build virtual host based on IP

(1) add a network card to the Linux host and check the IP address.

(2) enter the configuration file "vhost.conf" and input as follows:

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

(3) the home page files of "AAA" site and "aaa02" site are modified as follows:

[root@localhost aaa02]# cd ../aaa
[root@localhost aaa]# vim index.html 

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

[root@localhost aaa]# cd ../aaa02
[root@localhost aaa02]# vim index.html 

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

(4) enter the httpd main configuration file to add and comment the port. Then restart the httpd service.

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

[root@localhost aaa02]# systemctl restart httpd
[root@localhost aaa02]# 

(5) using win10 host to visit two sites with different IP addresses, the visit is successful. But it can only be accessed by IP address. Generally, the domain name is used to access the website. Next, we configure the domain name to access different IP address sites.

(6) first add the domain name "ServerName" in the configuration file "vhost.conf".

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

(7) enter the region configuration file of the named service, and add a region information of "aaa02".

[root@localhost 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; };
};

(8) enter the "/ var/named /" directory, and copy a "aaa.com.zone" file named "aaa02.com.zone" with permission, and modify it as follows:

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

(9) use win10 host again to visit two sites with different IP addresses through domain name, and the visit is successful.

Posted by *Lynette on Tue, 22 Oct 2019 23:10:24 -0700