Apache VirtualHosts

The following tutorial will explain how to configure one Apache server to resolve for multiple domain names. We assume you already have install Apache on you Linux based server. Check your distribution for where the configuration files are stored. For this example I am using Ubuntu 12.04.1 LTS.

Make sure you have the DNS A records pointing to the external IP of your server.
We will setup the following domains:

  • example1.com
  • example2.ca

First step is to create separate directory’s for each website. Apaches default web folder is /var/www so we will make the folders in there.

cd /var/www
mkdir example1.com
mkdir example2.ca

Now that the directory’s are made you can upload the sites into each of them.

Next step is to make some configuration files for each of the websites. For that we are going to switch to the /etc/apache2/sites-enabled and create a new file. You can use another text editor if you wish.

cd /etc/apache2/sites-enabled
nano example1.com

Inside this file, you will want to insert the following config.

<VirtualHost *:80>
        ServerAdmin webmaster@example1.com
        ServerName example1.com
        ServerAlias www.example1.com

        DocumentRoot /var/www/example1.com

        <Directory /var/www/example1.com>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/example1.com-error.log

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

        CustomLog ${APACHE_LOG_DIR}/example1.com-access_log.log common

</VirtualHost>

You could keep the logs in there default directory, but I like to know if one site has an issue that others are not. In this configuration we have not included CGI for security reasons.

The next configuration file is simple. Just need to replace example1.com with example2.ca.

<VirtualHost *:80>
        ServerAdmin webmaster@example2.ca
        ServerName example2.ca
        ServerAlias www.example2.ca

        DocumentRoot /var/www/example2.ca

        <Directory /var/www/example2.ca>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/example2.ca-error.log

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

        CustomLog ${APACHE_LOG_DIR}/example2.ca-access_log.log common

</VirtualHost>

After you have created all your configs, it’s time to restart Apache.

sudo service apache2 reload

That is it. You have now configured multiple domains on Apache.