Thursday, February 3, 2011

Apache2 Primary VirtualHost

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?

  • <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

    Jon Doe : 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`.
    chunkyb2002 : * 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.
  • 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.

    Jon Doe : Thanks. So, now the question is how can I change this order? as I have separate files for each host.
    Darth Android : @Jon Change the name of the file, as the files are loaded in alphabetical order.
    Prix : @Jon post an example on how your conf is and we will be able to guide you better
    Jon Doe : @Prix Cheers mate, I'm quite new to this stuff. Where does this `conf` file rest?
    Prix : @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.
    Jon Doe : @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?
    Prix : 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/
    From Prix

0 comments:

Post a Comment