Share three ways Apache can configure a virtual host

  • 2020-05-10 23:21:00
  • OfStack

1. Based on IP

  1. Suppose the server has an IP address of 192.168.1.10 and USES ifconfig to bind 3 IP to the same network interface eth0:

[root@localhost root]# ifconfig eth0:1 192.168.1.11
[root@localhost root]# ifconfig eth0:2 192.168.1.12
[root@localhost root]# ifconfig eth0:3 192.168.1.13

  2. Modify the hosts file and add 3 domain names corresponding to it:

192.168.1.11     www.test1.com
192.168.1.12     www.test2.com
192.168.1.13     www.test3.com
  3. Set up a web host to store the web page root directory, such as /www directory to set up test1, test2, test3 folders, which respectively store 1.html, 2.html, 3.html

/www/test1/1.html
/www/test2/2.html
/www/test3/3.html  

  4. Include the additional configuration file httpd-vhosts.conf in httpd.conf, and then write the following configuration in httpd-vhosts.conf:


<VirtualHost 192.168.1.11:80>
  ServerName www.test1.com
  DocumentRoot /www/test1/
  <Directory "/www/test1">
     Options Indexes FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow From All
   </Directory>
</VirtualHost>

<VirtualHost 192.168.1.12:80>
  ServerName www.test1.com
  DocumentRoot /www/test2/
  <Directory "/www/test2">
     Options Indexes FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow From All
   </Directory>
</VirtualHost>

<VirtualHost 192.168.1.13:80>
  ServerName www.test1.com
  DocumentRoot /www/test3/
  <Directory "/www/test3">
     Options Indexes FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow From All
   </Directory>
</VirtualHost>

  5. Test each virtual host and access www.test1.com, www.test2.com, www.test3.com  

2. Based on hostname

1. Set the domain name mapping to 1 IP, modify hosts:

192.168.1.10   www.test1.com
192.168.1.10   www.test2.com
192.168.1.10   www.test3.com

2. With the above 1, establish a virtual host to store the web pages of the root directory

/www/test1/1.html
/www/test2/2.html
/www/test3/3.html  

  3. Include the additional configuration file httpd-vhosts.conf in httpd.conf, and then write the following configuration in httpd-vhosts.conf:  

In order to use a domain-based virtual host, you must specify the server IP address (and possible ports) for the host to accept the request. This can be configured using the NameVirtualHost directive. If all the IP addresses on the server are used, you can use * as a parameter for NameVirtualHost. Specifying an IP address in the NameVirtualHost directive does not cause the server to automatically listen for that IP address. The IP address set here must correspond to a network interface on the server.
The next step is to set up the Settings for each virtual host you set up < VirtualHost > The configuration, < VirtualHost > The parameter of NameVirtualHost is the same as that of NameVirtualHost. each < VirtualHost > In the definition block, there will be at least one ServerName directive specifying which host to serve and one DocumentRoot directive specifying where the contents of this host reside on the file system.

If you add a virtual host to an existing web server, you must also build one for the existing host < VirtualHost > Block. The contents of ServerName and DocumentRoot should be consistent with the global hold 1, and should be placed at the top of the configuration file, playing the role of default host.


NameVirtualHost *:80
<VirtualHost *:80> 
  ServerName *

  DocumentRoot /www/

</VirtualHost>

<VirtualHost *:80>

  ServerName www.test1.com

  DocumentRoot /www/test1/

  <Directory "/www/test1">

    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

  </Directory>

</VirtualHost>

 

<VirtualHost *:80>

  ServerName www.test2.com

  DocumentRoot /www/test2/

  <Directory "/www/test2">

    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

  </Directory>

</VirtualHost>
<VirtualHost *:80>

  ServerName www.test3.com

  DocumentRoot /www/test3/

  <Directory "/www/test3">

    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

  </Directory>

</VirtualHost>

  4. Test each virtual host and access www.test1.com, www.test2.com, www.test3.com

  3. Port-based

1.   modify the configuration file

Change the original Listen 80 to Listen 80, Listen 8080

2. Change the virtual host Settings:


<VirtualHost 192.168.1.10:80>
  DocumentRoot /var/www/test1/
  ServerName www.test1.com
</VirtualHost>

<VirtualHost 192.168.1.10:8080>
  DocumentRoot /var/www/test2
  ServerName www.test2.com
</VirtualHost>

Above is the Apache virtual host configuration method, I hope to help you learn.


Related articles: