Thursday, February 3, 2011

How to configure multiple domains pointing to different directories on a server

I've been given a new domain www.mywebsite.com and I want to point it to a specific directory on my server: var/www/specific/path

What are the steps for doing it ? Could you suggest me a tutorial ?

I need to repeat the same operations for several domains.

thanks

Update:

NameVirtualHost a.b.c.d //I've hidden my real ip

<VirtualHost www.website.com:80>
    DocumentRoot /var/www/sites/website
    ServerName www.website.com
    CustomLog /var/log/apache2/website.com.access combined
</VirtualHost>
  • I'm assuming you want to host multiple sites on a single IP address and that you're using apache. If so, then essentially, the following lines of code in my httpd.conf do the job for me:

    NameVirtualHost a.b.c.d:80
    
    <VirtualHost site1.company.com:80>
        DocumentRoot /path/to/site1/documentroot
        ServerName site1.company.com
        CustomLog /usr/local/apache/logs/site1.company.com.access combined
    </VirtualHost>
    
    <VirtualHost site2.company.com:80>
        DocumentRoot /path/to/site2/documentroot
        ServerName www.brossi.net
        CustomLog /usr/local/apache/logs/site2.company.com.access combined
    </VirtualHost>
    

    Where a.b.c.d is your server's IP address. Repeat VirtualHost declarations as you need them. The CustomLog declaration isn't vital, but it helps me to keep my sites' logs separately from one another.

    You can find more documentation on apache at http://httpd.apache.org/docs/; click on the version of your apache server, then enter (eg) NameVirtualHost in the search box (though the doco for that can also be found directly at http://httpd.apache.org/docs/current/mod/core.html#namevirtualhost - it's not a feature that changes much between versions).

    MadHatter : Sorry, I should have added that the apache project has a very nice piece of doco on all of this at http://httpd.apache.org/docs/current/vhosts/
    Patrick : @MadHatter Sorry for the delay, they finally transfer the domain, so I can only test now. Shortly, I cannot make it work, the domain doesn't lead to my website. I've updated the question with my current configuration. What am I missing ?
    Patrick : Also, this is what I get when I do add the NameVirtualHost line: [Thu Oct 07 16:51:46 2010] [warn] NameVirtualHost a.b.c.d:80 has no VirtualHosts
    MadHatter : Could you confirm that, both on the machine in question, and on another random machine, when you nslookup www.website.com, you get a.b.c.d ?
    Patrick : @MadHatter I actually get another ip. Wow. But I don't get why, I bought a VPS slice and they assigned me a specific ip. Then I transferred the domain, and they just assigned it to another ip ?!
    Patrick : Is maybe some additional step I have to do before to configure Apache ?
    MadHatter : Get the DNS working. When apache starts up and sees VirtualHosts, it resolves each of the declared names to decide which IP to put it on. In your case, it'll be getting a different address from that declared in NameVirtualHosts (which probably explains the "NameVirtualHost a.b.c.d:80 has no VirtualHosts" error). You will not get this working without either self-consistent DNS or a hack; try for the former, it's less painful in the long run, and in any case you'll have to get it right before the rest of the world sees your site.
    From MadHatter

0 comments:

Post a Comment