This document describes scenarios to setup multiple web sites running on a single Apache server, via name-based or IP-based virtual hosts. The term Virtual Host refers to practice of maintaining more than one server on one machine, as differentiated by their apparent hostname.
Running several name-based web sites on a single IP address
You have a single IP address, and multiple DNS aliases (CNAMES) point to this IP address. For example, you want to run a web server for www.domein1.com and www.domain2.org on single machine.
# Ensure that Apache listens on port 80 and name-based virtual hosting is enabled.
Listen 80
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.domain1.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.domain2.org
# Other directives here
</VirtualHost>
The asterisks match all IP addresses, so the main server serves no requests. It is possible to serve different content based on differing IP addresses or ports.
Directives of VirtualHost are used to enclosure a group of directives that will appy only to a particular virtual host.
Serving the same content on different IP addresses
The server can be made to respond to internal and external requests with the same content, with just one VirtualHost section.
<VirtualHost 192.168.1.1 172.20.30.40>
DocumentRoot /www/example1
ServerName www.domain1.com
# Other directives here
</VirtualHost>
Running different sites on different ports
You have multiple domains going to the same IP and also want to serve multiple ports.
Listen 80
Listen 8080#Mixed port-based and ip-based virtual hosts
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
<VirtualHost *:80>
DocumentRoot /www/example80
ServerName www.domain.com
# Other directives here
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot /www/example8080
ServerName www.domain.com
# Other directives here
</VirtualHost>
Catch unknown requests using _default_ vhosts
The first VirtualHost is also be seen as the default or primary server. That means that if a request does not match one of the specified ServerName directives in any virtualhost, it will be served from the primary name-based vhost (appearing first in the configuration file).A default vhost never serves a request that was sent to an address/port that is used for name-based vhosts.
To catch a request to any unspecified IP address and port, i.e., an address/port combination that is not used for any other virtual host, include the following directives:
<VirtualHost _default_:80>
DocumentRoot /www/default
</VirtualHost>
Attention! Using such a default vhost with a wildcard port(in stead of port 80) prevents any request going to the main server.