Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to host multiple sites with single apache

Status
Not open for further replies.

maryam421

MIS
Apr 5, 2004
102
US
Hi,
I am already hosting one website. I am trying to host another website using apache. I know you can do it with virtual hosting. But I am not sure how to set it up http.config file? I am using apache 2.x on windows 2000. Please let me know the step by step process as I am very new to apache.
Also is there a limit that you can host x number of sites? Can I use two apache server on the same box? And if I can then how do I know which apache server servers the perticular site as both of them are running on different port?
Please help!!
 
There is no reason to run two separate Apache installations on the same machine. It's possible but not necessary and difficult to maintain.

In your Apache configuration, you define multiple individual web sites through the use of the <VirtualHost> tag:

<VirtualHost 10.0.0.2:80>
ServerName DocumentRoot /some/filesystem/path
</VirtualHost>

Apache has two ways of determining which virtual host should respond to a particular client connection.

The first way is by different IP addresses or ports. If you have two sites that are on different IP addresses, or on the same IP address on different ports:

<VirtualHost 10.0.0.2:80>
ServerName DocumentRoot /some/filesystem/path
</VirtualHost>

<VirtualHost 10.0.0.3:80>
ServerName DocumentRoot /some/other/filesystem/path
</VirtualHost>

Then if a client connects to 10.0.0.2 Apache will send files for and if a client connectsion to 10.0.0.3 Apache will send files for
Apache can differentiate between two virtual hosts on the same IP address and port:

<VirtualHost 10.0.0.2:80>
ServerName DocumentRoot /some/filesystem/path
</VirtualHost>

<VirtualHost 10.0.0.2:80>
ServerName DocumentRoot /some/other/filesystem/path
</VirtualHost>

by differences in the "ServerName" values. If a user points his browser to when the browser connects to Apache, one piece of information the browser passes to Apache is the name of the site to which it wants to connect. Since in the above example the two sites are on the same IP and port but have different "ServerName" directives, Apache will match the name provided by the browser to figure out which virtual host to use.

NOTE: You can have two HTTP sites on the same IP address and port. You cannnot have two HTTPS sites on the same address and port. This is because the SSL handshaking and server connection happens before the browser can specify a host name.



Two virtual hosts can be configured largely independent of one another. The CustomLog and ErrorLog directives, for example, can be used inside a <VirtualHost...>...</VirtualHost> tagset, allowing each virtual host's logging to go to different files.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks for your reply. What about my current website? Do I need to move that to virtual host?
Also how I am going to test that both the sites are up and working?
With one site I can use localhost to check the website. But how will I troubleshoot the two websites? Localhost will take me to which website first or second?
Also I would like to create two seperate directories for two sites.Is this possible?
I am running this server behind the router with portforwarding. With same ip/port can I use virtual hosting?
Once again your posting helped me alot. Thanks
 
I always put Apache web sites in a VirtualHost setup, even if there is only one site. But a web site does not have to have a unique document root, so you can create a virtual host that contains the site.

You can't use "localhost" for testing multiple web sites. It doesn't give you the resolution you need to see both sites.

Set up the sites to use the configured IP address on your NIC, or use "*:80" in place of "10.0.0.2:80" in your VirtualHost configuration directives ("*" binds the site to all configured IP addresses). Then edit your hosts file to map the NIC's IP address to both domain names.

You can then start a browser and point it at either domain name, seeing whether you get each site.

There is in-depth documentation on VirtualHost in the Apache online manual



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I did the changes you told me. But it is still showing the one site not the second site.
Here is what I have done in config file:

<VirtualHost 10.0.0.2:80>
ServerName DocumentRoot /company
</VirtualHost>

<VirtualHost 10.0.0.2:80>
ServerName DocumentRoot c:/byankle
</VirtualHost>
Both the sites are pointing to same website(The second site directory( is empty. Please let me know what I am doing wrong here.
Thanks for all your help.
 
You've got them pointing to the same IP, that's not correct. Different IPs are required.

As an alternative, please take a look at the mod_vhosts modules which can allow you to host multiple domains under a single IP by parsing for the domain itself and mapping to a distinct or common file path for the document root.


Surfinbox.com Business Internet Services - National Dialup, DSL, T-1 and more.
 
When using named virtual hosts, don't use ServerName, use ServerAlias.

NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /home/brunson/ ServerAlias ServerAlias brunson.com
ErrorLog /home/brunson/ TransferLog /home/brunson/</VirtualHost>

<VirtualHost *:80>
DocumentRoot /home/ipgeeks/ ServerAlias ServerAlias ipgeeks.net
ErrorLog /home/ipgeeks/ TransferLog /home/ipgeeks/</VirtualHost>
 
That's not necessary. ServerName provides the site name differentiation necessary for Apache to serve different sites on a single IP.

ServerAlias is used when a single site can respond to multiple host names -- for example, when a site can be reached either by or foo.bar.

ServerAlias exists because Apache will only allow one ServerName entry in any <VirtualHost>...</VirtualHost> definition.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 

I could be wrong (it wouldn't be the first time), but I had to take the multiple ServerName directives out of my virt_hosts, otherwise it was causing strange results when trying to resolve the virt host.

It was almost like the virt host ServerNames were overriding the one in the main httpd.conf and interfering with each other.

I'm running 2.0.46, so it may be version dependent.
 
Correction.

It looks like Apache 2 will allow multiple ServerName entries per virtual host. I remember Apache 1 complaining about the presence of multiple ServerName entries.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
ericbrunson:
I have the same problem. If I have more than one ServerName directive in a VirtualHost tag, then servername/site mappings get screwed up. This does not happen with ServerAlias.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I am preparing to run Apache Webserver on a Windows 2003 Server. Below is my current network setup. I would like to be able to host numerous differnt websites.

Apache will be running on a Windows 2003 Enterprise Server. The server is behind a Windows 2000 Advanced Server running ISA Server 2000 as the proxy/firewall. ISA redirects web requests to the Windows 2003 Server to a different port numbers. Ex. Incoming port 80 to port 100 then 100 back to 80. Can I do this setup? I'll use Eric's....

NameVirtualHost *:100
<VirtualHost *:100>
DocumentRoot /home/brunson/ ServerAlias ServerAlias brunson.com
ErrorLog /home/brunson/ TransferLog /home/brunson/</VirtualHost>

<VirtualHost *:110>
DocumentRoot /home/ipgeeks/ ServerAlias ServerAlias ipgeeks.net
ErrorLog /home/ipgeeks/ TransferLog /home/ipgeeks/</VirtualHost>
 
If I have more than one ServerName directive in a VirtualHost tag, then servername/site mappings get screwed up. This does not happen with ServerAlias."

I had no problem with ServerName and Apache 1.3.x.x with multiple name-based domains. Maybe you're doing something wrong.

Newposter
&quot;Good judgment comes from experience. Experience comes from bad judgment.&quot;
 
Newposter:
You don't understand. We are not talking about having multiple virtual hosts sharing an IP address. We're talking about multiple uses of the ServerName directive inside a <VirtualHost> definition.


This works:

NameVirtualHost [the IP/port spec]
<VirtualHost [the IP/port spec]>
ServerName [blue]ServerAlias foo.bar[/blue]
DocumentRoot [some pathname]
{other directives}
</VirtualHost>

<VirtualHost [the IP/port spec]>
ServerName [blue]ServerAlias bar.baz[/blue]
DocumentRoot [some pathname]
{other directives}
</VirtualHost>


This does not:

NameVirtualHost [the IP/port spec]
<VirtualHost [the IP/port spec]>
ServerName [blue]ServerName foo.bar[/blue]
DocumentRoot [some pathname]
{other directives}
</VirtualHost>

<VirtualHost [the IP/port spec]>
ServerName [blue]ServerName bar.baz[/blue]
DocumentRoot [some pathname]
{other directives}
</VirtualHost>


The Apache online manual does not explicitly state that you cannot have multiple ServerName directives for a given VirtualHost definition. The for Apache2 are slightly more clear, stating "Many servers want to be accessible by more than one name. This is possible with the ServerAlias directive, placed inside the <VirtualHost> section."



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sleipnir214, I DO UNDERSTAND. I'm saying that I DO have Apache 1.3.xx with multiple ServerNames in the Virtual Host, with NO problems, 3years and about 20,000 hits later:

<VirtualHost *>
UseCanonicalName Off
ServerAdmin webmaster@domain1.com
ServerName ServerAlias domain1.com
DocumentRoot "D:/ScriptAlias /cgi-bin/ "D:/
</VirtualHost>

<VirtualHost *>
UseCanonicalName Off
ServerAdmin bob@domain2.com
ServerName ServerAlias domain2.com
DocumentRoot "D:/ScriptAlias /cgi-bin/ "D:/
</VirtualHost>

<VirtualHost *>
UseCanonicalName Off
ServerAdmin webmaster@domain3.com
ServerName ServerAlias domain3.com
DocumentRoot "D:/ScriptAlias /cgi-bin/ "D:/</VirtualHost>

I have 6 domains in my Virtual Host, no issues. Maybe you're doing something different. Use the asterisk instead of the IP address in the first line of each block.

Newposter
&quot;Good judgment comes from experience. Experience comes from bad judgment.&quot;
 
And I'm saying that on both Apache 1.3.x and Apache 2.0.x, I've had problems using ServerName multiple times in a single virtual host definition.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
ok, after reading all the replays i rewrote again and again my virtualhosts but i'm still having a problem. Let me explain the config. first:
I am behind a router, that asigns IPs (current IP is 192.168.2.88)
I am Runninf FreeBSD and just installed apache server. I used to host two websites on my XP maschine but now i am trying to do it on the BSD and it looks like that:

<VirtualHost * >
ServerAdmin japple91@hotmail.com
ServerName japple.no-ip.org
ServerAlias japple.no-ip.org
DocumentRoot /usr/local/</VirtualHost>
#
<VirtualHost * >
ServerAdmin admin@drd-84.com
ServerName ServerAlias drd-84.com
DocumentRoot /usr/local/</VirtualHost>

after all that i only get the website in the /usr/local/ folder even when i type . I tried to change the ports (opened up port 81 on the Router) and configure one of the sites on that port, but that did not work. What have i done wrong?What should i do? How would i get two IPs for every Virtual Host? help pls :[
 
Do you have a NameVirtualHost directive? Also see faq65-1831.

If apache doesn't understand your virutual host configuration or can't understand which virtual host is being requested, it will serve up the first virtual host listed in httpd.conf. This is the behavior that your are seeing. Trying to run on different ports will only confuse matters. This is strictly a configuration problem - the question is where?

And FWIW, having ServerName & ServerAlias exactly the same serves no purpose.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top