Apache Configuration Virtual Host three ways

Source: Internet
Author: User

Original address: http://www.cnblogs.com/hi-bazinga/archive/2012/04/23/2466605.html
first, based on IP

1. Assuming that the server has an IP address of 192.168.1.10, use Ifconfig to bind 3 IPs on the same network interface eth0: [root@localhost root]# ifconfig eth0:1
[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, add three domain names corresponding to each:
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 Virtual host Web page root directory, such as in the/www directory to establish Test1, Test2, test3 folder, which storage 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 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. Done, test the next each virtual host, respectively, access to www.test1.com, www.test2.com, www.test3.com

Second, based on domain name

1. Set the domain name mapping the same 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. As above, set up a virtual host 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 httpd-vhosts.conf: In order to use a domain-based virtual host, The server IP address (and possible ports) must be specified to allow the host to accept the request. Can be configured using the NAMEVIRTUALHOST directive. If all the IP addresses on the server are used, you can use * as a namevirtualhost parameter.  Specifying an IP address in the NAMEVIRTUALHOST directive does not cause the server to listen automatically to that IP address. The IP address set here must correspond to a network interface on the server.

The next step is to set up each of your virtual host settings <VirtualHost> configuration block,<virtualhost> parameters are the same as the namevirtualhost instructions. In each <VirtualHost> definition block, there is at least one servername instruction to specify which host and a documentroot instruction to indicate where the host's content exists in the filesystem.

If you add a virtual host to an existing Web server, you must also build a <VirtualHost> definition block for the existing host. The contents of servername and DocumentRoot should be consistent with the global, and should be placed at the front of the configuration file, playing 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. Done, test the next each virtual host, respectively, access to www.test1.com, www.test2.com, www.test3.com

three, based on the port
1. Modify the configuration file

will be 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>

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.