virtual hostsh apache2 ubuntu

After setting up and configuring an Apache installation on Ubuntu, it can be useful to set up virtual hosts for various different purposes. Virtual hosts allow you to have more than one host or domain name on a single server. Most tutorials explaining how this can be done are rather confusing and don’t really seem to follow any coherent path to the final outcome. This tutorial tries to explain how a virtual host can be set up in the simplest way possible, and in a way that is considerably easier to understand.

In Ubuntu 7.10, Apache stores information about any virtual hosts in two locations, namely /etc/apache2/sites-available and /etc/apache2/sites-enabled. The purpose of having two directories is to allow users to set up virtual hosts but not necessarily have them active or enabled. This also allows users to disable virtual hosts without losing their configuration settings (they will still be available in sites-available even if deleted from sites-enabled). The process involves creating an unique file for each virtual host within the sites-available directory and then creating a linked file which is an exact duplicate within the sites-enabled directory to ensure that the site is actually being served. This tutorial will involve creating a new virtual host configuration file, creating the linked active file and the modification of the hosts file on Windows and Ubuntu for virtual host usage. A new file must be created for each virtual host you require on the machine.

The first step involves navigating to the sites-available directory:

cd /etc/apache2/sites-available

The next step involves creating a new file within this directory to house the settings for your new virtual host (give the .conf file a name that fits the alias of the virtual host):

sudo gedit

The text editor will then open allowing you to add the settings of your virtual host (replace the IP address with the IP address of the machine followed by the port, replace the other details with the name and details of your new virtual host):

ServerAdmin [email protected]
DocumentRoot /var/www/filesforsite/
CustomLog /var/log/apache2/ combined

Save the file and close the editor. The settings of your new virtual host are now stored within the sites-available directory. The virtual host is not yet enabled however, this will be done using the following steps. Navigate to the sites-enabled directory:

cd /etc/apache2/sites-enabled/

Now create a file link to the file you created within the sites-available directory:

sudo ln -s /etc/apache2/sites-available/

You have now created the linked file which allows apache to recognize the virtual host and activate it. Before restarting Apache, modify your hosts file so that your machine doesnt look on the internet for your virtual host (this must be performed on any machines on the network that wish to view the virtual host). In linux:

sudo gedit /etc/hosts

Add the following next to (localhost) on the local machine, or next to the IP address of the server on a remote machine: newvirtualhost

This will allow the machine to recognize, as well as newvirtualhost.

Once you have done adding these values to the hosts files on any computers requiring the use of the virtual host, you can now restart apache on the web server:

sudo /etc/init.d/apache2 reload

Once you have restarted apache, you can now open a browser on any of the machines with new host files and navigate to the domain name of your new virtual host. You should then be taken to the document root of the virtual host, which you specified in that virtual host’s configuration file. If you have an index file in that directory, it should then display automatically.

Don’t Stop Here

More To Explore