Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have several Virtual Hosts on my Apache2 server. What I'm trying to achieve is to set one of these hosts as the primary one. So that whenever I type in my IP address in the browser, it brings that one specific host up. How can I do this?

share|improve this question

2 Answers 2

<VirtualHost _default_:80>
...
site details
...
</VirtualHost>

That will capture all IP's that hit port 80 (or whatever port you tell it) that aren't configured elsewhere.

http://httpd.apache.org/docs/1.3/mod/core.html#virtualhost

share|improve this answer
    
Thanks chunky. Maybe I didn't imply my question clearly. How come default virtual host has <VirtualHost *:80> and is the default one? I want the browser to open a specific host when I open http://127.0.0.1/ but it opens default. –  Jon Doe Oct 7 '10 at 0:01
    
* is a special case in that it matches all IP's and hostnames. So if you want it to open a specific file when you go to 127.0.0.1 <VirtualHost 127.0.0.1:80> is what you should set. *:80 should only be left in cases when you want all other possibilities matched with a response. i.e. it's not required. –  chunkyb2002 Oct 8 '10 at 19:46

Apache layout is designed so that the 1st virtual host in your conf file is the default host.

If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.

Let's go with a simple example:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName siteA
    DocumentRoot /www/siteA
</VirtualHost>

<VirtualHost *:80>
    ServerName siteB
    DocumentRoot /www/siteB
</VirtualHost>

In the above case whenever you type any ip it will lead to siteA.

Second case:

NameVirtualHost 10.0.0.1:80
NameVirtualHost 10.0.0.2:80

<VirtualHost 10.0.0.2:80>
    ServerName siteB
    DocumentRoot /www/siteB
</VirtualHost>

<VirtualHost 10.0.0.1:80>
    ServerName siteC
    DocumentRoot /www/siteC
</VirtualHost>

<VirtualHost 10.0.0.1:80>
    ServerName siteA
    DocumentRoot /www/siteA
</VirtualHost>

In the above case, siteC will be displayed when 10.0.0.1 is used because it comes first.

share|improve this answer
    
Thanks. So, now the question is how can I change this order? as I have separate files for each host. –  Jon Doe Oct 7 '10 at 0:28
    
@Jon Change the name of the file, as the files are loaded in alphabetical order. –  Darth Android Oct 7 '10 at 0:33
    
@Jon post an example on how your conf is and we will be able to guide you better –  Prix Oct 7 '10 at 1:26
    
@Prix Cheers mate, I'm quite new to this stuff. Where does this conf file rest? –  Jon Doe Oct 7 '10 at 1:27
    
@Jon it is a file called httpd.conf and maybe depending on how your server si configured it could be inside vhosts.conf. On a default installation they are usually at /etc/httpd folder but if it was a personalized installation it could be somewhere else. You also dont state if you are using *nix or windows so it is hard to guide you. Considering you are on linux system type locate httpd.conf and it should do the job, the vhosts.conf should be near it so no worry to search for both. –  Prix Oct 7 '10 at 2:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.