1

Let assume that blah.com, blah2.com all point to the same server with IP=5.31.111.7.

I would like that:

  • accessing blah.com serves /var/www/site1

  • accessing blah2.com serves /var/www/site1

  • accessing 5.31.111.7 serves /var/www/site2

I tried

<VirtualHost *:80>
    DocumentRoot /var/www/site1
</VirtualHost>
<VirtualHost 5.31.111.7:80>
   DocumentRoot /var/www/site2
</VirtualHost>

but now everything goes to /var/www/site2, which is not what I wanted.

How to configure the VirtualHost, such that the served website depends on the URL ?

PS: why should I do this in /etc/apache2/sites-enabled/000-default instead of /etc/apache2/apache2.conf ? I don't understand this sites-enabled / sites-available/default naming... Why are there so many different config files by default on Debian, for such a simple thing?

Basj
  • 41,386
  • 99
  • 383
  • 673
  • possible duplicate of [apache: different virtualhosts in the same port](http://stackoverflow.com/questions/6069892/apache-different-virtualhosts-in-the-same-port) – Ulrich Schwarz Nov 04 '14 at 12:03

1 Answers1

4

What you want to do is called Name-Based Virtual Hosting, you'll need

NameVirtualHost *:80

to enable it on port 80, and for each VirtualHost, you need to give the name(s):

<VirtualHost *:80>
ServerName blah2.com
ServerAlias www.blah2.com

DocumentRoot /var/www/site1
</VirtualHost>

Note that there are limitations on SSL/TLS when doing name-based virtual hosting, but it's a bit of a moot point since post-POODLE, people start to require TLS anyway, so ancient browsers are out of luck anyway.

As to the config files, it's very very useful to have two classes of config files: the ones with defaults that a package update will overwrite, and your local ones that it will not touch, or better even, a directory full of the former and a directory full of the latter. (Because additional packages might want to make configuration settings, they'll all install in the former place, and you should only ever change/override config in the second place.)

Ulrich Schwarz
  • 7,598
  • 1
  • 36
  • 48
  • Thanks! It worked with your code, even without `NameVirtualHost *:80`. About config files, what are exactly sites-enabled / sites-available ? It is not very explicit... – Basj Nov 04 '14 at 12:05
  • @Basj: I would assume that the intention is that you put symlinks from `sites-enabled` into `sites-available` for those sites you want to use. That way, you can quickly switch something off by removing the symlink, and you can prepare things in `sites-available` and not have to worry immediately about typos. (Remember, your coworker could be restarting the server while you're editing the config file.) – Ulrich Schwarz Nov 04 '14 at 12:15