Construction of web Virtual Host Based on Apache

Keywords: Linux vim

Virtual web host refers to running multiple web sites in the same server, each of which actually does not occupy the entire server independently, so it is called "virtual" web host. The virtual web host can make full use of the hardware resources of the server, thus greatly reducing the cost of website construction and operation.
It is very convenient to build a virtual host server with httpd. Only one httpd service can support a large number of web sites at the same time.
httpd supports three types of virtual hosts:
Domain Name: Same IP, Same Port, Different Domain Names
Based on IP Address: Different IP, Same Port
Port-based: same IP, different ports
In fact, we use different domain names to visit websites most often.
1. Domain name-based virtual hosts:
First, we need to provide domain name resolution for virtual hosts:

[root@www /]# vim /etc/named.conf 
......       //
zone "test1.com" in {
        type master;
        file "test1.com.zone";
};

zone "test2.com" in {
        type master;
        file "test2.com.zone";
};
[root@www /]# vim /var/named/test1.com.zone 
......       //
        in      ns      www.test1.com.
www     in      a       192.168.1.10
[root@www /]# vim /var/named/test2.com.zone
......      //
        in      ns      www.test2.com.
www     in      a       192.168.1.10

Prepare web documents for virtual hosts:

[root@www /]# mkdir -p /var/www/html/test1com
[root@www /]# mkdir -p /var/www/html/test2com
[root@www /]# echo "<h1>www.test1.com</h1>" > /var/www/html/test1com/index.html
[root@www /]# echo "<h1>www.test2.com</h1>" > /var/www/html/test2com/index.html

Add virtual host configuration:

[root@www /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 

//Configure the virtual site area of test1:
 ......        // Omit part of content
<VirtualHost *:80>             # Configure the listener address and port "*" to represent any address
    ServerAdmin webmaster@test.com     # Setting up administrator's mailbox can be deleted
    DocumentRoot "/var/www/html/test1com"    # Specify the site root directory
    ServerName www.test1.com                 # Configure domain name
    ServerAlias www.dummy-host.example.com     # Configure aliases
    ErrorLog "logs/www.test1.com-error_log"   # Logging errors
    CustomLog "logs/www.test1.com-access_log" common    # Record access log
        <Directory "/var/www/html">     # Setting directory access rights
        Require all granted    # Allow all
        </Directory>
</VirtualHost>
//Configure the virtual site area of test2:
<VirtualHost *:80>
    ServerAdmin webmaster@test.com
    DocumentRoot "/var/www/html/test2com"
    ServerName www.test2.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/www.test2.com-error_log"
    CustomLog "logs/www.test2.com-access_log" common
        <Directory "/var/www/html">
        Require all granted
        </Directory>
</VirtualHost>
[root@www /]# vim /usr/local/httpd/conf/httpd.conf   # Enter the main configuration file
......
Include conf/extra/httpd-vhosts.conf         # We'll start when we find our bank. # Remove
[root@www /]# System CTL restart httpd restart service to make configuration effective

Client access web validation:


2. IP address-based virtual host:
There is no connection between each method. Don't confuse IP address-based virtual hosts with domain name-based virtual hosts.

[root@www /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 
......
<VirtualHost 192.168.1.10:80>             # Configuration listener address 192.168.1.10
    ServerAdmin webmaster@test.com
    DocumentRoot "/var/www/html/test1com"
    ServerName www.test1.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/www.test1.com-error_log"
    CustomLog "logs/www.test1.com-access_log" common
        <Directory "/var/www/html">
        Require all granted
        </Directory>
</VirtualHost>
<VirtualHost 192.168.1.20:80>             # Configuration listener address 192.168.1.20
    ServerAdmin webmaster@test.com
    DocumentRoot "/var/www/html/test2com"
    ServerName www.test2.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/www.test2.com-error_log"
    CustomLog "logs/www.test2.com-access_log" common
        <Directory "/var/www/html">
        Require all granted
        </Directory>
</VirtualHost>



3. Port-based virtual host:

[root@www /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 
......
<VirtualHost 192.168.1.10:80>
    ServerAdmin webmaster@test.com
    DocumentRoot "/var/www/html/test1com"
    ServerName www.test1.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/www.test1.com-error_log"
    CustomLog "logs/www.test1.com-access_log" common
        <Directory "/var/www/html">
        Require all granted
        </Directory>
</VirtualHost>
<VirtualHost 192.168.1.10:8080>               # Modify port number
    ServerAdmin webmaster@test.com
    DocumentRoot "/var/www/html/test2com"
    ServerName www.test2.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/www.test2.com-error_log"
    CustomLog "logs/www.test2.com-access_log" common
        <Directory "/var/www/html">
        Require all granted
        </Directory>
</VirtualHost>
listen 80                      # Listen on port 80 (default 80 does not need to write this line)
listen 8080                  # Listen on port 8080

Note: Service reboot is required after each configuration is completed

Posted by PrinceOfDragons on Mon, 29 Jul 2019 20:18:29 -0700