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?
Asked
Active
Viewed 447 times
2 Answers
1
<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.

chunkyb2002
- 688
- 3
- 9
-
Thanks chunky. Maybe I didn't imply my question clearly. How come `default` virtual host has `
` 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 07 '10 at 00: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 http://127.0.0.1
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 08 '10 at 19:46
1
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.

Prix
- 4,881
- 3
- 24
- 25
-
Thanks. So, now the question is how can I change this order? as I have separate files for each host. – Jon Doe Oct 07 '10 at 00:28
-
@Jon Change the name of the file, as the files are loaded in alphabetical order. – Darth Android Oct 07 '10 at 00:33
-
@Jon post an example on how your conf is and we will be able to guide you better – Prix Oct 07 '10 at 01:26
-
@Prix Cheers mate, I'm quite new to this stuff. Where does this `conf` file rest? – Jon Doe Oct 07 '10 at 01: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 07 '10 at 02:44
-
@Prix I'm using Ubuntu and Apache2, I found `httpd.conf`, it's under `/etc/apache2` but it's empty! There's no `vhosts.conf`! Should I create one? If I should, then what `vhosts.conf` content should be? – Jon Doe Oct 10 '10 at 23:37
-
Ubuntu has a different default place and names for apache configuration files https://help.ubuntu.com/6.10/ubuntu/serverguide/C/httpd.html so you are looking for apache2.conf and there will probably be a main folder for the domains conf /etc/apache2/sites-available/ – Prix Oct 10 '10 at 23:45