Multiple web sites behind one domain

Edit the site config of the Apache web server. You can find them under /etc/apache2/sites-available/ (If they are not there, I am sorry, but Google might help you.).

cd /etc/apache2/site-available/

Then open your default site 000-default:

sudo vi 000-default

As far as I know, this file will always exist, if no one deleted it beforehand.

You will the see a quite boring text file:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this 
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

The configuration in this state does not help you a lot. You can show any web page capable code that is placed in /var/www, but that’s it already.

Since I store everything web related in /var/www, I like to change the DocumentRoot to the default web site which should be display when somebody visits the server via a web browser.

Let’s change the value of DocumentRoot to /var/www/myDefaultSite. Now the contents behind “myDefaultSite” (e.g. a WordPress site) are displayed if nothing else is given.

To put other sites behind this exact domain – as you cannot have more than one subdomains in this scenario – you will have to use Alias. The Alias will allow you, to link a “directory” – this is how it looks to you visitors – to a different part of your filesystem.

In the configuration file, this will look like following:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this 
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/myDefaultSite

        Alias /subSite /var/www/subSite

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Now you will have to reload the apache:

sudo service apache2 reload

You must not get any error messages. If you do, please check you configuration!

If you access you server and add the first part of the Alias behind the domain (www.example.com/subSite), you can access you other web site on this server.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close Menu