Apache configures virtual hosts in three ways: apache Virtual Hosts

Source: Internet
Author: User

Apache configures virtual hosts in three ways: apache Virtual Hosts

I. IP-based

1. Assume that the server has an IP address of 192.168.1.10, and use ifconfig to bind three IP addresses 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 one of the three domain names to the file:

192.168.1.11 www.test1.com
192.168.1.12 www.test2.com
192.168.1.13 www.test3.com

3. Create a root directory for storing web pages on the VM. For example, create a folder named test11_test21_test3 under the/wwwdirectory, and separate the folder named 1.html#2.html#3.html.

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

 

4. Include the additional profile httpd-vhosts.conf in httpd. conf, and then write the following configuration in the 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. As a result, each virtual host can access www.test1.com, www.test2.com, and www.test3.com respectively.

 

Ii. Based on Host Name

1. Set domain name ing to the same IP address and modify hosts:

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

2. As above, create a VM to store the root directory of the web page

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

 

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

 

To use a domain name-based virtual host, you must specify the Server IP address (and possible port) to make the host accept the request. You can use the NameVirtualHost command for configuration. If all IP addresses on the server are used, you can use * as the parameter of NameVirtualHost. Specifying the IP address in the NameVirtualHost command does not enable the server to automatically listen to that IP address. The IP address must correspond to a network interface on the server.

The next step is to set the <VirtualHost> Configuration block for each created virtual host. The parameters of <VirtualHost> are the same as those of the NameVirtualHost command. Each <VirtualHost> definition block contains at least one ServerName command to specify the servo host and one DocumentRoot command to indicate where the content of the host exists in the file system.

If you add a virtual host to an existing web server, you must also create a <VirtualHost> definition block for the existing host. Specifically, ServerName and DocumentRoot should contain the same content as the global one and be placed at the beginning of the configuration file to assume the role of the 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. As a result, each virtual host can access www.test1.com, www.test2.com, and www.test3.com respectively.

 

Iii. Port-based

1. modify the configuration file

Change the original

Listen 80
Change
Listen 80
Listen 8080.


2. Change the VM 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.