I am trying to host two websites using Apache from the same Ubuntu server. I have one ip address, and I only have one domain (which resolves to the ip address). So I want requests to the domain name to give one website, and requests to the ip address to give the other.
I have symlinks in /etc/apache2/sites-enabled to two files, pointing to the config for my two sites.
One contains:
<VirtualHost 1.2.3.4:80>
ServerName 1.2.3.4
stuff
</VirtualHost>
while the other contains
<VirtualHost domain.net:80>
ServerName domain.net
stuff
</VirtualHost>
However, when I start Apache, I get the following message:
[warn] VirtualHost 1.2.3.4:80 overlaps with VirtualHost domain.net:80, the first has precedence, perhaps you need a NameVirtualHost directive
and when I point my browser at either domain.net or 1.2.3.4 I get the site that I want associated with the ip address.
If I delete either symlink, then pointing a browser at either the domain name or the ip address gives the only enabled website. (As you would hope.)
As I understand it, both config files in sites-enabled are being loaded at once, and the one containing the ip address trumps the one containing the domain name. The warning suggests looking at the NameVirtualHost directive, but all the help I can find online refers to cases where you have two domain names pointing to the same ip address.
As always, and help or advice would be much appreciated.
(For what it’s worth, the websites are both Rails applications, and I’m deploying using Passenger, but I don’t think that’s important here.)
This is how I do it:
Apache looks for a suitable virtualhost for every request. If it doesn’t find one that matches the
ServerNameor any of theServerAliasesthen it takes the first one. It doesn’t really matter what you use for theServerNamein the firstVirtualHostas it will always be used if none of the otherVirtualHostsmatch.