1 Apache and Virtual Hosts Exercises

1.1 Install Apache version 2

Apache is already installed on your machines, but if it was not you would simply do:

apt-get install apache2

As the root user. Once Apache is installed it is up and running. The startup defintion for Apache is /etc/init.d/apache2. You can stop, start, restart, reload Apache by doing:

# service apache2 stop
# service apache2 start
# service apache2 restart
# service apache2 reload

1.2 Configure Virtual Hosts in Apache 2

1.2.1 Directory Structure

There are several ways to approach virtual hosting. The first thing you must determine is your directory structure where each web site will reside. For our purposes we will use the standard Linux convention of:


1.2.2 Multiple Names

All of your PCs have multiple entries for their assigned IP addresses on the Authoritative nameserver for the zone ws.nsrc.org – that is on Thus, you can use both these names:

pcX.ws.nsrc.org ==> 10.10.0.X
tzX.ws.nsrc..org ==> 10.10.0.X

to access your machines. We will take advantage of this for the exercise.

1.2.3 Web Directories

Let's create the web directories that we’ll need:

# cd /var/www/
# mkdir pcX.ws.nsrc.org
# mkdir tzX.ws.nsrc.org

Be sure you replace “X” with the number of your PC.

1.2.4 Index Files

Now let's copy the original index.html file to our pcX.ws.nsrc.org directory, and the tzX.ws.nsrc.org directory as well:

# cp index.html pcX.ws.nsrc.org/.
# cp index.html tzX.ws.nsrc.org/.

Edit the index.html files in the pcX and tzX directories.

# vi pcX.ws.nsrc.org/index.html

Change the text “This is the default web page for this server.” to “pcX.ws.nsrc.org Virtual Host”. Close and save the file.

# vi tzX.ws.nsrc.org/index.html

Change the text “This is the default web page for this server.” to “tzX.ws.nsrc.org Virtual Host”. Close and save the file.

1.2.5 Apache Configuration Files

Now we need to adjust the Apache configuration files for our two new sites. We will treat the pcX.ws.nsrc.org site as our default site – that is, where people arrive if, for some reason, the come to our box without specifying a host name (for instance, if they use an IP address instead):

# cd /etc/apache2/sites-available
# vi default

In the file default we will change the “DocumentRoot” setting to point to the directory for pcX.ws.nsrc.org. Look for this line:

DocumentRoot /var/www

Change this line to read:

DocumentRoot /var/www/pcX.ws.nsrc.org

Save the file and exit. Now let’s restart Apache and see if the server is still working:

# service apache2 restart 

In a web browser (on your machine) open the URL “http://pcX.ws.nsrc.org/” and you should see:

It works!
pc32.ws.nsrc.org Virtual Host
The web server software is running but no content has been added, yet.

To verify that you only have one host at this time open the URL http://tzX.ws.nsrc.org/ and you should see the same thing.

1.3 Enabling A Second Host

Now let’s enable our other host, tzX.ws.nsrc.org. We need to create a new file in /etc/apache2/sites-available and call it something to indicate the name of the virtual host. The easiest thing to do is just copy the default config file and update the DocumentRoot statement in that file to point to our other host.

# cd /etc/apache2/sites-available
# cp default tzX.ws.nsrc.org
# vi tzX.ws.nsrc.org

Find the line that says:

DocumentRoot /var/www/pcX.ws.nsrc.org

And, change that line to read:

DocumentRoot /var/www/tzX.ws.nsrc.org

And, underneath that line add a line that says:

ServerName tzX.ws.nsrc.org

Now exit and save the file.

To enable the virtual host do:

# a2ensite tzX.ws.nsrc.org
# service apache2 reload

1.3.1 Check the Second Host

After restarting prove to yourself that virtual hosting is working. In a web browser go to http://tzX.ws.nsrc.org/ and you should see the page indicate you are looking at the index.html file that resides in /var/www/tzX.ws.nsrc.org – i.e., the page will say: ~ It works! tz32.ws.nsrc.org Virtual Host The web server software is running but no content has been added, yet. ~

That’s it! You are now running Apache with virtual hosts on your machine. Ubuntu / Debian Linux make this very simple as the default configuration is already built assuming you wish to use virtual hosting.

1.4 Log Files

On item you can update is the name of the log files that are being written for each host. To do for tzX.ws.nsrc.org you would do:

# vi /etc/apache2/sites-available/tzX.ws.nsrc.org

And look for the line that says:

ErrorLog /var/log/apache2/error.log

And change the line to read:

ErrorLog /var/log/apache2/tzX.error.log

Now look for the line that says:

CustomLog /var/log/apache2/access.log combined

And change this to read:

CustomLog /var/log/apache2/tzX.access.log combined

Now save and exit from the file and reload the Apache web server:

# service apache2 reload

To verify that your changes are working look in the Apache log directory for the new log file:

# ls /var/log/apache2

And, you should see the new log files:

tzX.access.log  tzX.error.log

That’s it. This is very useful for all sorts of reasons and is standard if you are providing virtual hosts to other users.