httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robert Moskowitz <...@htt-consult.com>
Subject Re: [users@httpd] vhosts are so simple !
Date Fri, 11 Jul 2003 13:08:25 GMT
At 10:46 AM 7/11/2003 +0200, stephane parenton wrote:
>Hi everyone,
>
>I've read things about vhosts and nothing seemed to be very hard.... it 
>was quite trivial.... so it's quite self-confident that i'm trying to set 
>virtual hosts on a server. It's a RH 7.3 with apache 1.3.27 compiled in 
>order to handle postgres and mysql db. I had only one domain to handle so 
>far, and everything was fine. Now i'm trying to put another domain, and 
>things are going wrong....

I am attaching an earlier message that helped me get it right....

Date: Tue, 08 Jul 2003 16:11:46 +0200
From: Patrick Donker <padonker@reporters.net>
To:  users@httpd.apache.org
References: <5.1.0.14.2.20030708095904.02e999b0@localhost>
In-Reply-To: <5.1.0.14.2.20030708095904.02e999b0@localhost>
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
Subject: Re: [users@httpd] Virtual host causing problems with default server

Below you'll find an excerpt from a document that helped me a whole 
bunch...read it and it probably will resolve your problem:

Robert Moskowitz wrote:
>At 03:47 PM 7/8/2003 +0200, Patrick Donker wrote:
>>read 
>><http://httpd.apache.org/docs-2.0/vhosts/>http://httpd.apache.org/docs-2.0/vhosts/

>>
>
>I have.  Many times.



Virtual Hosting with Apache

Apache supports running multiple virtual servers, commonly called virtual 
hosting. In English, this simply means that Apache can be configured to 
serve pages to more than one domain name from a single IP. This is achieved 
with the NameVirtualHost directive and one or more <VirtualHost> blocks 
specific to each domain. Virtual hosting has three general forms: 
Name-based, IP-based, and mixed Name/IP. Name-based is by far the most 
common, so we'll cover it first.


Name-based virtual hosting

Below is a segment from one author's actual virtual host section.

### Section 3: Virtual Hosts ###


NameVirtualHost 142.165.206.61


<VirtualHost 142.165.206.61>

         ServerAdmin 
<mailto:webmaster@syroidmanor.com>webmaster@syroidmanor.com

         ServerName <http://www.syroidmanor.com>www.syroidmanor.com

         DocumentRoot /home/tom/webs/syroidmanor

         ErrorLog /home/tom/webs/syroidmanor/logs/error_log

         CustomLog /home/tom/webs/syroidmanor/logs/access_log common

</VirtualHost>


<VirtualHost 142.165.206.61>

         ServerAdmin 
<mailto:webmaster@syroidmanor.com>webmaster@syroidmanor.com

         ServerName insights.syroidmanor.com

         DocumentRoot /home/tom/webs/insights

         ErrorLog /home/tom/webs/insights/logs/error_log

         CustomLog /home/tom/webs/insights/logs/access_log common

</VirtualHost>


<VirtualHost 142.165.206.61>

         ServerAdmin <mailto:webmaster@daynotes.com>webmaster@daynotes.com

         ServerName <http://www.daynotes.com>www.daynotes.com

         DocumentRoot /home/tom/webs/daynotes

         ErrorLog /home/tom/webs/daynotes/logs/error_log

         CustomLog /home/tom/webs/daynotes/logs/access_log common

</VirtualHost>


<VirtualHost _default_:*>

         ServerAdmin 
<mailto:webmaster@syroidmanor.com>webmaster@syroidmanor.com

         DocumentRoot /usr/local/apache/htdocs

</VirtualHost>



The key to the virtual hosts section is the NameVirtualHost statement. This 
tells Apache to subdivide requests to the IP 142.168.206.61 by domain name 
according to the information contained in the <VirtualHost> directive blocks.

DNS records for the IP 142.165.206.61 point to the domain syroidmanor.com, 
hosted by the server Hydras. This DNS record also contains several CNAME, 
or alias records, namely <http://www.syroidmanor.com>www.syroidmanor.com 
and insights.syroidmanor.com. A second DNS record exists for the domain 
daynotes.com, which is also assigned the IP 142.165.206.61.

When a client sends a request for the URL 
<http://www.daynotes.com>http://www.daynotes.com, the DNS record is 
resolved to 142.165.206.61 and the client is forwarded to the server at 
this location. Apache is listening on port 80, and when the request arrives 
the VirtualHost section is scanned for a ServerName match. If one is found, 
the directives in that section are executed, and the client is pointed to 
the document directory referenced by the DocumentRoot directive - in this 
case, /home/tom/webs/daynotes. Note that in this example, all the webs 
served from this machine have their own error and access logs. If the 
ErrorLog and CustomLog directives were not present for a given Virtual 
Host, then logging would still occur but the entries would be written to 
the default "common" log files (typically, in the directory /var/apache/logs).

The last directive block, <VirtualHost _default_:*> is present as a 
"fall-through". If none of the ServerName directives match the request, 
then the client is directed to the web pages found at 
/usr/local/apache/htdocs, which in this case, are simply Apache's default 
indexes supplied with the program. This block could easily be adapted to 
point to a directory containing an error document specific to the site.


IP-based hosting

IP-based hosting is very similar to name-based, except the server has more 
than one IP assigned to it. For example:

### Main Server Config ###


Port 80

ServerName server.domain.com

DocumentRoot /www/domainone/htdocs


### Virtual Hosts Section ###


NameVirtualHost 222.33.33.111

NameVirtualHost 222.33.33.222


<VirtualHost 222.33.33.111>

ServerName <http://www.domainone.com>www.domainone.com

DocumentRoot /www/domainone/htdocs

...

</VirtualHost>


<VirtualHost 222.33.33.222>

ServerName <http://www.domaintwo.com>www.domaintwo.com

DocumentRoot /www/domaintwo/htdocs

...

</VirtualHost>



Requests to the IP 222.33.33.111 are directed to the /www/domainone/htdocs 
directory, and requests to 222.33.33.222 are directed to /www/domaintwo/htdocs.
Caution
Pay close attention to your entries in the Virtual Hosts section. Any 
directories specified here must exist before Apache is started, or the 
daemon will refuse to run and quit (see the error log file for a possible 
explanation). Also, pay attention to spelling. Typing /homes instead of 
/home will not just disable the Virtual Host block containing the error - 
it will prevent Apache from starting.

As an alternative, you can redirect requests by to the server by IP and 
port number. Such a configuration is typically used when a second IP is not 
available or for testing a new web site before making it available to the 
public. We do not recommend using port-based redirection on a long-term 
basis - users tend to have a strong aversion to adding port numbers to a URL.

### Main Server Config ###


Port 80

Listen 222.33.33.111:80

Listen 222.33.33.111:8080

ServerName server.domain.com


### Virtual Hosts Section ###


<VirtualHost 222.33.33.111:80>

ServerName <http://www.domainone.com>www.domainone.com

DocumentRoot /www/domainone/htdocs

...

</VirtualHost>


<VirtualHost 222.33.33.111:8080>

ServerName <http://www.domaintwo.com>www.domaintwo.com

DocumentRoot /www/domaintwo/htdocs

...

</VirtualHost>





Mixed name/IP hosting

The third method of redirecting requests using Virtual Hosting is by using 
a combination of Name and IP. The key to employing this method is to tell 
Apache which IP (or, in a more complex scenario, IPs) to subdivide. In the 
example shown, two IPs are assigned to the server: 192.168.0.2 and 
192.168.0.3. The NameVirtualHost directive tells the daemon to subdivide 
requests for 192.168.0.2 only. In this example, both 
<http://www.domain.com>www.domain.com and 
<http://www.domaintwo.com>www.domaintwo.com are CNAME'd to 192.168.0.2; 
<http://www.domainthree.com>www.domainthree.com is assigned to 192.168.0.3.

NameVirtualHost 192.168.1.2


<VirtualHost 192.168.1.2>

ServerAdmin <mailto:webmaster@domain.com>webmaster@domain.com

DocumentRoot /www/domain/htdocs

...

</VirtualHost>


<VirtualHost 192.168.1.2>

ServerAdmin <mailto:webmaster@domaintwo.com>webmaster@domaintwo.com

DocumentRoot /www/domaintwo/htdocs

...

</VirtualHost>


<VirtualHost 192.168.1.3>

ServerAdmin <mailto:webmaster@domainthree.com>webmaster@domainthree.com

DocumentRoot /www/domainthree/htdocs

...

</VirtualHost>



When a request arrives on interface 192.168.0.2, Apache redirects the 
client according to the domain name provided. All requests to 192.168.1.3 
are automatically assumed to be for the domain associated with that IP, and 
sent to /www/domainthree/htdocs.

Finally, keep in mind most directive options found in httpd.conf's main 
server section can also be used within a virtual host block.

<VirtualHost 222.33.33.111>

ServerName <http://www.domain.com>www.domain.com

DocumentRoot /www/domain/htdocs

   <Directory /www/domain/htdocs/secrets>

    Order Deny,Allow

    Deny from all

    Allow from 222.33.33

   </Directory>



In the above example, only clients connecting from the 222.33.33.X address 
block are permitted access to the /www/domain/htdocs/secrets directory.
Hint
New syntax was introduced with version 1.3.14 that eliminates the need to 
specify a numeric IP for the NameVirtualHost directive. Please see the 
Apache Group's online documentation for specifics and more virtual host 
examples 
(<http://www.apache.org/docs/vhosts>http://www.apache.org/docs/vhosts).

Virtual hosting can get thorny in a hurry with complex configurations, 
especially for servers supporting a large number of sites. Our advice is to 
go slow, add and test one virtual host section at a time, and remain 
conscious of how fickle Apache is about spelling and punctuation.


Mime
View raw message