13

I have a server with multiple websites hosted and distinguishable using name-based virtual hosting of apache.

How do I set it so that a specific website is hosted when the ip of my server is entered in the address bar?

tread
  • 10,133
  • 17
  • 95
  • 170
  • Did you ever work this out? I have just upgraded a server to ubuntu 14.04 and the existing 000-default.conf VirtualHost has simply stopped being invoked when no domain name matches – instead the first alphabetical VirtualHost with a ServerName directive is used, for some reason. – Benji XVI May 06 '15 at 00:09
  • 1
    Ah, I just needed to specify a IP address in the default VH, instead of using `*:80` or `_default_:80`. I’m not sure if that’s an apache bug. – Benji XVI May 06 '15 at 00:18

4 Answers4

20

What you want to use is the _default_ VirtualHost.

<VirtualHost _default_:80>
    DocumentRoot /www/default80
    # ...
</VirtualHost>

It's described here. Basically if nothing else match the request the _default_ host will be used.

EDIT
This could also be written as:

<VirtualHost *>
    DocumentRoot /www/default
    # ...
</VirtualHost>

Is is important that this is the first VirtualHost in the configuration since Apache will start matching them from top to bottom, selecting the one that fit the best based on ServerName and ServerAlias.

This post might also be of interest: Apache default VirtualHost

Community
  • 1
  • 1
Qben
  • 2,617
  • 2
  • 24
  • 36
  • I am using debian and I have created the vhost file as you specified in sites-available, there was already a `_default` in there so I modified it and created a new `_default_` with the same configuration...I then linked these files with `ln -s source dest` into sites-enabled...as `a2ensite` said there was no site found matching...it still does not work – tread Oct 08 '13 at 14:16
  • Did you make sure it's the first `VirtualHost` in the config? Meaning it has a name that appear before the other hosts. I think normally you use like `010-default` `020-www` `030-forum` as link names in `site-enabled`. – Qben Oct 08 '13 at 17:32
  • If your `apache2.conf` has 0 `VirtualHost` but you `Include sites-enabled/` for all your vhost configs, then, the default site has to appear before the other files. For example, a default vhost file named `aaaa.default` will work but a `zzzz.default` will not work. [Unless `zzzz.default` is your only config] – Yeow_Meng Jun 16 '14 at 17:52
  • In `sites-enabled` there is `_000-default`, `_default` and `_default_`, however going to the server ip goes to the first virtual host after `_default_` – tread Sep 11 '14 at 07:43
7

just find the Include sites-enabled/ line in your apache2.conf file and add the path to the conf file you want to be site default above it. from:

Include sites-enabled/

to

Include sites-enabled/mydefault.conf
Include sites-enabled/
mouseware
  • 71
  • 1
  • 1
  • 3
    This is most likely the answer you're looking for, the configs are loaded in alphabetical order, the option I went with was to rename my default config from mysite.conf to 00-mysite.conf (remember to a2dissite a2ensite & reload) – lsl May 31 '17 at 01:31
  • This is one way. Another is to replace the `000-default.conf` link to point to the file of yours that you want to be loaded first. That way potential updates to the `apache2.conf` file continue to happen when the package gets updated on your system. – Alexis Wilke Jan 15 '22 at 06:08
1

When you first install apache2, there is a site configuration file named 000-default.conf. This is going to be the default because it is very likely to appear first in the list of files under /etc/apache2/sites-enabled.

To have your own file as the default, you can either replace the file under /etc/apache2/sites-available/000-default.conf with your own, or replace the link like so:

sudo rm /etc/apache2/sites-enabled/000-default.conf
sudo ln -s ../sites-available/my-site-setup.conf /etc/apache2/sites-enabled/000-default.conf

Then restart apache2 (or just reload).

The _default_ as mentioned by the other answer is for defining a virtual host which can be found with the default IP address. It's not the default virtual host.

<VirtualHost _default_:80>
...

is equivalent to

<VirtualHost *:80>
...

The * is a globing pattern which matches any IP addresses.

Note:

Replacing the 000-default.conf file is okay, but in most cases the installation package is going to view that as a modified file and manage it in some weird way which is why I think it's cleaner to only change the soft link.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
1

Keep it clean, don't delete or edit anything in /etc/apache2/sites-available/.

Create all new site configurations in /etc/apache2/sites-available/. Copy whatever site configuration you want enabled to /etc/apache2/sites-enabled/. Only make sure /etc/apache2/sites-enabled/ has only one configuration file.

Sample format for new apache site configurations in Ubuntu 20.04 LTS is

<VirtualHost *:80>
    ServerName http://localhost
    ServerAdmin admin@mysite.com
    DocumentRoot /var/www/html/mysiteroot/public
    <Directory /var/www/html/mysiteroot>
        AllowOverride All
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Notice that 000-default.conf is by default in both directories mentioned above and should only be replaced by your new configuration in /etc/apache2/sites-enabled/ so that it can be restored anytime you need it.

Restart Apache2 service after you make any configuration changes.

Udo E.
  • 2,665
  • 2
  • 21
  • 33