summaryrefslogtreecommitdiff
path: root/docs/manual/vhosts/virtual-host.html
blob: aa81fad91e37a91b26a1638a5ec6e2837033d730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>Apache Server Virtual Host Support</TITLE>
</HEAD>

<!-- Background white, links blue (unvisited), navy (visited), red (active) -->
<BODY
 BGCOLOR="#FFFFFF"
 TEXT="#000000"
 LINK="#0000FF"
 VLINK="#000080"
 ALINK="#FF0000"
>
<!--#include virtual="header.html" -->
<H1 ALIGN="CENTER">Virtual Host Support</H1>

<STRONG>See Also:</STRONG>
<A HREF="host.html">Non-IP based virtual hosts</A>

<H2>What are virtual hosts?</H2>
This is the ability of a single machine to be a web server for multiple
domains. For example, an Internet service provider might have a machine
called <CODE>www.serve.com</CODE> which provides Web space for several
organizations including, say, <EM>smallco</EM> and <EM>baygroup</EM>.
Ordinarily, these groups would be given parts of the Web tree on www.serve.com.
So smallco's home page would have the URL
<BLOCKQUOTE>
http://www.serve.com/smallco/
</BLOCKQUOTE>
and baygroup's home page would have the URL
<BLOCKQUOTE>
http://www.serve.com/baygroup/
</BLOCKQUOTE>
<P>
For esthetic reasons, however, both organizations would rather their home
pages appeared under their own names rather than that of the service
provider's; but they do not want to set up their own Internet links and
servers.
<P>
Virtual hosts are the solution to this problem. smallco and baygroup would
have their own Internet name registrations, <CODE>www.smallco.com</CODE> and
<CODE>www.baygroup.org</CODE> respectively. These hostnames would both
correspond to the service provider's machine (www.serve.com). Thus
smallco's home page would now have the URL
<BLOCKQUOTE>
http://www.smallco.com/
</BLOCKQUOTE>
and baygroup's home page would would have the URL
<BLOCKQUOTE>
http://www.baygroup.org/
</BLOCKQUOTE>

<H2>System requirements</H2>
Due to limitations in the HTTP/1.0 protocol, the web server <STRONG>must have a
different IP address for each virtual host</STRONG>. This can be achieved
by the machine having several physical network connections, or by use
of a <A HREF="../misc/vif-info.html">virtual interface</A> on some operating
systems.

<H2>How to set up Apache</H2>
There are two ways of configuring apache to support multiple hosts.
Either by running a separate httpd daemon for each hostname, or by running a
single daemon which supports all the virtual hosts.
<P>
Use multiple daemons when:
<UL>
<LI>The different virtual hosts need very different httpd configurations, such
   as different values for:
   <A HREF="../mod/core.html#servertype">ServerType</A>,
   <A HREF="../mod/core.html#user">User</A>,
   <A HREF="../mod/core.html#group">Group</A>,
   <A HREF="../mod/mod_mime.html#typesconfig">TypesConfig</A> or
   <A HREF="../mod/core.html#serverroot">ServerRoot</A>.
<LI>The machine does not process a very high request rate.
</UL>
Use a single daemon when:
<UL>
<LI>Sharing of the httpd configuration between virtual hosts is acceptable.
<LI>The machine services a large number of requests, and so the performance
   loss in running separate daemons may be significant.
</UL>

<H2>Setting up multiple daemons</H2>
Create a separate httpd installation for each virtual host.
For each installation, use the
<A HREF="../mod/core.html#bindaddress">BindAddress</A> directive in the
configuration
file to select which IP address (or virtual host) that daemon services.
<EM>E.g.</EM>,
<BLOCKQUOTE><CODE>BindAddress www.smallco.com</CODE></BLOCKQUOTE>
This hostname can also be given as an IP address.

<H2>Setting up a single daemon</H2>
For this case, a single httpd will service requests for all the virtual hosts.
The <A HREF="../mod/core.html#virtualhost">VirtualHost</A> directive in the
 configuration file is used to set the values of
<A HREF="../mod/core.html#serveradmin">ServerAdmin</A>,
<A HREF="../mod/core.html#servername">ServerName</A>,
<A HREF="../mod/core.html#documentroot">DocumentRoot</A>,
<A HREF="../mod/core.html#errorlog">ErrorLog</A> and
<A HREF="../mod/mod_log_config.html#transferlog">TransferLog</A> configuration
directives to different values for each virtual host.
<EM>E.g.</EM>,
<BLOCKQUOTE><CODE>
&lt;VirtualHost www.smallco.com&gt;<BR>
ServerAdmin webmaster@mail.smallco.com<BR>
DocumentRoot /groups/smallco/www<BR>
ServerName www.smallco.com<BR>
ErrorLog /groups/smallco/logs/error_log<BR>
TransferLog /groups/smallco/logs/access_log<BR>
&lt;/VirtualHost&gt;<BR>
<BR>
&lt;VirtualHost www.baygroup.org&gt;<BR>
ServerAdmin webmaster@mail.baygroup.org<BR>
DocumentRoot /groups/baygroup/www<BR>
ServerName www.baygroup.org<BR>
ErrorLog /groups/baygroup/logs/error_log<BR>
TransferLog /groups/baygroup/logs/access_log<BR>
&lt;/VirtualHost&gt;<BR>
</CODE></BLOCKQUOTE>

This VirtualHost hostnames can also be given as IP addresses.

<P>

Almost <STRONG>ANY</STRONG> configuration directive can be put
in the VirtualHost directive, with the exception of
<A HREF="../mod/core.html#servertype">ServerType</A>,
<A HREF="../mod/core.html#user">User</A>,
<A HREF="../mod/core.html#group">Group</A>,
<A HREF="../mod/core.html#startservers">StartServers</A>,
<A HREF="../mod/core.html#maxspareservers">MaxSpareServers</A>,
<A HREF="../mod/core.html#minspareservers">MinSpareServers</A>,
<A HREF="../mod/core.html#maxrequestsperchild">MaxRequestsPerChild</A>,
<A HREF="../mod/core.html#bindaddress">BindAddress</A>,
<A HREF="../mod/core.html#pidfile">PidFile</A>,
<A HREF="../mod/mod_mime.html#typesconfig">TypesConfig</A>, and
<A HREF="../mod/core.html#serverroot">ServerRoot</A>.

<P>

<EM>SECURITY:</EM> When specifying where to write log files, be aware
of some security risks which are present if anyone other than the
user that starts Apache has write access to the directory where they
are written.  See the <A HREF="../misc/security_tips.html">security
tips</A> document for details.

<P>

<H2>File Handle/Resource Limits:</H2>
When using a large number of Virtual Hosts, Apache may run out of available
file descriptors if each Virtual Host specifies different log files.
The total number of file descriptors used by Apache is one for each distinct
error log file, one for every other log file directive, plus 10-20 for
internal use. Unix operating systems limit the number of file descriptors that
may be used by a process; the limit is typically 64, and may usually be
increased up to a large hard-limit.
<P>
Although Apache attempts to increase the limit as required, this
may not work if:
<OL>
<LI>Your system does not provide the setrlimit() system call.
<LI>The setrlimit(RLIMIT_NOFILE) call does not function on your system
 (such as Solaris 2.3)
<LI>The number of file descriptors required exceeds the hard limit.
<LI>Your system imposes other limits on file descriptors, such as a limit
on stdio streams only using file descriptors below 256. (Solaris 2)
</OL>

In the event of problems you can:
<UL>
<LI>Reduce the number of log files; don't specify log files in the VirtualHost
sections, but only log to the main log files.
<LI>If you system falls into 1 or 2 (above), then increase the file descriptor
limit before starting Apache, using a script like
<BLOCKQUOTE><CODE>
#!/bin/sh <BR>
ulimit -S -n 100 <BR>
exec httpd</CODE></BLOCKQUOTE>
</UL>

The have been reports that Apache may start running out of resources allocated
for the root process. This will exhibit itself as errors in the error log like
"unable to fork". There are two ways you can bump this up:

<OL>
<LI>Have a <CODE>csh</CODE> script wrapper around httpd which sets the
"rlimit" to some large number, like 512.
<LI>Edit http_main.c to add calls to setrlimit() from main(), along the lines
of
<PRE>
        struct rlimit rlp;

        rlp.rlim_cur = rlp.rlim_max = 512;
        if (setrlimit(RLIMIT_NPROC, &rlp)) {
            fprintf(stderr, "setrlimit(RLIMIT_NPROC) failed.\n");
            exit(1);
        }
</PRE>
(thanks to "Aaron Gifford &lt;agifford@InfoWest.COM&gt;" for the patch)
</OL>

The latter will probably manifest itself in a later version of Apache.

<!--#include virtual="footer.html" -->
</BODY>
</HTML>