Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving two websites with Apache with single domain name and single IP address

Tags:

apache

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.)

like image 644
grifaton Avatar asked Feb 09 '10 20:02

grifaton


People also ask

Can you run multiple websites on a single IP address?

You can indeed host many many websites on the same IP address. Though, if you want your website to use secure HTTP (HTTPS), then you have to use a unique IP address for that web site only.

Can I use single domain for two websites?

One question people often ask is whether they can host more than one website on their domain name so this post will help to clear up this issue. The basic answer is a definite Yes.

Can you host two websites one server?

Virtual hosting allows you to host many domains on one server. A server may have extensive resources like HDD space, CPU, RAM, and so on. You can use the same server resources for different sites. It lets you host multiple websites on a single web server instance.


1 Answers

This is how I do it:

NameVirtualHost 1.2.3.4:80

<VirtualHost 1.2.3.4:80>
    ServerName localhost
    DocumentRoot /var/www/default
</VirtualHost>

<VirtualHost 1.2.3.4:80>
    ServerName mydomain.net
    DocumentRoot /var/www/mydomain
</VirtualHost>

Apache looks for a suitable virtualhost for every request. If it doesn't find one that matches the ServerName or any of the ServerAliases then it takes the first one. It doesn't really matter what you use for the ServerName in the first VirtualHost as it will always be used if none of the other VirtualHosts match.

like image 200
Tomas Markauskas Avatar answered Nov 10 '22 01:11

Tomas Markauskas