I am attempting to host multiple sites on a single server by using virtual hosts however I am having a bit of an issue.

A bit of info before I begin,

-I am using Ubuntu Server 12.04
-I am using Apache2
-I have two domains and two sites, stored in /var/www/site1 and /var/www/site2

I have put this in my httpd.conf file and from what I understand it should have worked, allowing domain1 to go to site1 and domain2 to go to site2 but instead domain1 and domain2 go to site1 only.

NameVirtualHost *80

<VirtualHost *:80>

    DocumentRoot "/var/www/site1"
    ServerName domain1.com
    ServerAlias www.domain1.com

</VirtualHost>

<VirtualHost *:80>

    DocumentRoot "/var/www/site2"
    ServerName domain2.com
    ServerAlias www.domain2.com

</VirtualHost>

As I say, it just loads up site1 regardless of which domain I put in. If I swap them around so domain2 is the first virtual host then it shall load up that one only.

What am I doing wrong?
Thank you in advance!

Recommended Answers

All 6 Replies

The only thing I see out of place is the quote marks arount the Document Root Normally I use the path /var/www/html/domainname.com. Below is the first entry from one of the servers I manage.

<VirtualHost *:80>
       ServerAdmin webmaster@chartwriter.com
       DocumentRoot /var/www/html/chartwriter.com
       ServerName chartwriter.com
       ErrorLog logs/chartwriter.com-error_log
       CustomLog logs/chartwriter.com-access_log common
</VirtualHost>

Thanks, I have tried it with and without quotes, have tried using the default one and editing ... I've tried everything but I just cannot get it to work...

What I think I am going to have to do is manually get the URL and redirect it with a PHP script.

how many files does ls /etc/apache2/sites-enabled output? I am wondering if you have more than one site-enabled with some conflicting configurations.

It has two, one for each of the two sites I have?

Can you post the contents of ~/hosts.txt after running sudo cat /etc/apache2/sites-enabled/site1 > ~/hosts.txt && cat /etc/apache2/sites-enabled/site2 >> ~/hosts.txt

replacing site1 and site2 with the actual names of your sites-enabled files.

hosts.txt will be in your home directory.

Thank you for your time androtheos however I have come up with another solution to solve the problem.
123-Reg which I purchased the domain from change the headers so I don't think it would work anyway, instead I have created a PHP script which takes the HTTP referrer and redirects depending on that.

Thanks for the help though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.