Ubuntu – How to setup “name based” virtual hosts using Ubuntu 12.04

Apache2

How do I setup “name based” virtual hosts using Ubuntu 12.04?

I have followed the instructions given at https://help.ubuntu.com/12.04/serverguide/httpd.html#http-configuration .

I've done the following…

  1. cp default newsite
  2. replace “/var/www” with “/var/www/newsite” in newsite
  3. add “ServerName newsite.example.com” to newsite

After some research, I found a blog entry that stated I needed to disable the default site using a2dissite default. After I did that, it worked. Is that correct? This is never mentioned in the Ubuntu server guide. The guide also includes this line…

“The default virtual host has no ServerName directive specified, so it
will respond to all requests that do not match a ServerName directive
in another virtual host.”

Which seems to imply that both the default site and others can co-exist.

I'm running a fresh install of 12.04 Server and have reloaded the apache config each time I made an adjustment.

In summary… after adding a new file under /etc/apache2/sites-available (an altered copy of the “default” file with the ServerName directive added)  and the corresponding symlink under /etc/apache2/sites-enabled, is it necessary to disable or rename the default site symlink in order for the new site to function? The documentation and one answer given below seems to infer that it is not necessary to do so, but if so, what have I done wrong? Using the config below, when trying to access newsite.example.com I get the default site.

/etc/apache2/sites-available$ cat default

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

/etc/apache2/sites-available$ cat newsite

<VirtualHost *:80>

        DocumentRoot /var/www/newsite
        ServerName newsite.example.com
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/newsite/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

/etc/apache2/sites-enabled# ls -l

root@sandbox:/etc/apache2/sites-enabled# ls -l
total 0
lrwxrwxrwx 1 root root 26 Mar 18 09:56 000-default -> ../sites-available/default
lrwxrwxrwx 1 root root 26 Mar  7 13:36 newsite -> ../sites-available/newsite

Best Answer

This is more of an apache configuration question, than a Ubuntu one.

Yes, you may run multiple virtual servers on one host, each serving separate content, provided that they all map (e.g. via DNS) to the same server.

The official documentation on how to create virtual servers (version 2.2 but this feature hasn't fundamentally changed between versions) can be found here:

httpd.apache.org/docs/2.2/vhosts/

The short answer is you need to:

  • define your virtual hosts
  • include some mapping between your host names and the content they serve

This is done by adding a virtual host clause to some apache config file, e.g. under /etc/apache2/sites-available/000-add-my-virtual-hosts (name designed specifically to precede the 000-default name in alphabetic order)

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName hostname1.mydomain.com
    DocumentRoot /home/www/hostname1
</VirtualHost>

<VirtualHost *:80>
    ServerName hostname2.mydomain.com
    DocumentRoot /home/www/hostname2
</VirtualHost>

Note that you may also need to add links from /etc/apache2/sites-enabled/ to /etc/apache2/sites-available if the site you need is already in the latter but not the former.

EDIT 1:
After reading the man page for a2dissite, it becomes clear that all it does is removing the symlink from /etc/apache2/sites-enabled/. The key is to understand that the order in which these configs are processed can affect the end result. The default site is called 000-default in order to be loaded first. If it matches all sites, i.e. acts as a 'anything else' wildcard, then you won't see the others. Try renaming the link to have a higher number like 999-default so it is loaded last (after the other sites matched).

EDIT 2: To your updated question: yes, it is necessary to rename or delete the default site because its config file name starts with '000' making it load first and 'take-over' due to the wildcard matching. I suppose the documentation can be improved on this point.

EDIT 3: The order in which server names appear, its importance and more is documented on this apache page in the section Name-based vhost One of the relevant sentences says:

The first vhost on this list (the first vhost in the config file with the
specified IP address) has the highest priority and catches any request to
an unknown server name or a request without a Host: header field.

and later under Observations:

... the ordering of name-based vhosts for a specific address set is significant.
The one name-based vhosts that comes first in the configuration file has the
highest priority for its corresponding address set.
Related Question