Apache 2.0: Setting up a Virtual Host or How to Setup Apache to Host Multiple Websites on a Single Server

Setting up virtual domains in Apache 2.0 is pretty darn easy. Easier then in IIS 6? Well, it requires more typing, but it takes literally less then 5 minutes to get a new domain up on a server.

Before I start, I am making a few assumptions about the operating environment:

  • Ubuntu server is being used, along with Apache 2.0.
  • The user has control over the targeted domain.
  • The user has ‘sudo’ or access to the ‘su’ account.

First, the user needs to point their domain to their server by adding an IP address entry for their domain. For example, this domain has a DNS entry for an IP address pointing to ‘www.alexkuo.info’. Once you have that setup, SSH into your web server.

The first thing I always do is setup a directory to store the files for the website (ex. ‘.html’, .’php’, ‘.jpg’ files). I usually setup a subdirectory in my home directory because I know I already have permissions to read,write, and execute files there.
To do this I type

mkdir websites
cd websites
mkdir www.alexkuo.info

sudo chown www-data:www-data /home/alex/websites

The first three lines create the subdirectory /home/alex/websites/www.alexkuo.info, while the last line sets directory /home/alex/websites and all other subdirectories below it to be readable by the default Apache user.

Next we need to add a virtual host to Apache. You can do this by typing

cd /etc/apache2/sites-available/
sudo touch www.alexkuo.info

The first line navigates to the sites-available directory. Files in here should contain information on the virtual hosts available. Virtual hosts correlate a domain received by Apache and the location of the files to be served for the received domain. The second line creates a file www.alexkuo.info. We will be using this file to configure the virtual host for the domain ‘www.alexkuo.info’.

I usually use ‘vi’ editor to edit text files on servers, and I know ‘vi’ is probably the most user-unfriendly text editor on earth. Instead of using ‘vi’, the user can use any text editor of their choice. In the ‘/etc/apache2/sites-available/’ directory, the user can setup the virtual host ‘www.alexkuo.info’ by typing:

sudo vi www.alexkuo.info

After opening the file, press ‘i’. This will set vi into text editing mode. Afterwards, copy and paste (or type) the following text into the text editor.

<VirtualHost *>
ServerName www.alexkuo.info

DocumentRoot /home/alex/websites/www.alexkuo.info/
ErrorLog /var/log/apache2/error.www.alexkuo.info.log

After the above has been entered, press the [ESC] key and type ‘ZZ’ (or [shift-z][shift-z] in rapid succession). This saves and exits the editor.

This is the most basic configuration you can have for a virtual host. The ‘DocumentRoot’ setting specifies the location of the files to be served when the domain, set by the ‘ServerName’ configuration, is received.

If you haven’t disable the default site, you will need to in order for the server to see the virtual host you created. You can do this by typing:

sudo a2dissite default

After saving the file, you will need to restart Apache2. You need to enable the site and restart the Apache. You can do this by typing the following:

sudo a2ensite www.alexkuo.info
/etc/init.d/apache2 reload

Your site is now setup and live. All that is left is to upload files to your website directory that was created at the beginning of this article. For this website, the directory is ‘/home/alex/websites/www.alexkuo.info/’.

If you would like to setup another site, just repeat the above procedures, except substitute the domain ‘www.alexkuo.info’ with another domain, like ‘subdomain.alexkuo.info’.

If you would like more information on the topic, please consult the following links:

Leave a Reply