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.

Similar Posts

  • Let it Snow

    Well currently a snow storm is about to hit North Bay this Sunday evening. The buzz is that this storm will cause a Snow Day tomorrow which mean no buses which mean I get to sleep in 😀 I was talking in the #mybb IRC channel and few individuals didn’t know what snow was like….

  • New Theme

    Ok so I know I havn’t posted in over a month so I thought I would change the theme. 😛 Right now I’m awaiting my time table for college. I’m sure your all wondering what I have been doing all summer and it’s simple. I’m eather working, or siting around doing nothing. I hope when…

  • More… Snow

    Well, I hate winter. So I thought I would go out side and take some pictures of it. Here is one looking in my backyared. Here is the front of my house. (Be side the big tree) Here is the snow bank in front of my house. Here’s a picture of my girl friend standing…

  • First day back

    Well Tuesday was the first day back to school. It was nice to see all my friends again but man do I hate the work. I got Math first, Sysco second, Wood Shop third, Peer toutring fourth and MSIP last. Peer toutring is cool. I get to stay in a grade 10 tech class so…

  • Camping

    This year I was invited to Jessicas family reunion. Unfortunately we had to sleep in a tent. It wasn’t as bad as I though it would be. Her family is very nice but some of them do need some help, just like myn. 😛 We left on Friday and came back Sunday. It was a…