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:

/var/www/VirtualHostName1
/var/www/VirtualHostName2
/var/www/etc…

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 10.10.0.254. Thus, you can use both these names:

pcX.ws.nsrc.org ==> 10.10.0.X
wwwX.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 wwwX.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 wwwX.ws.nsrc.org directory as well:

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

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

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

Change the text “It works!” to “pcX.ws.nsrc.org Virtual Host”. Close and save the file.

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

Change the text “This is the default web page for this server.” to “wwwX.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 000-default.conf

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/html

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:

pc36.ws.nsrc.org Virtual Host
This is the default welcome page used to test the correct operation of the Apache2 server after installation on Ubuntu systems.

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

1.3 Enabling A Second Host

Now let’s enable our other host, wwwX.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 000-default.conf wwwX.ws.nsrc.org.conf
# vi wwwX.ws.nsrc.org.conf

Find the line that says:

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

And, change that line to read:

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

And, underneath that line add a line that says:

ServerName wwwX.ws.nsrc.org

Now exit and save the file.

To enable the virtual host do:

# a2ensite wwwX.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://wwwX.ws.nsrc.org/ and you should see the page indicate you are looking at the index.html file that resides in /var/www/wwwX.ws.nsrc.org – i.e., the page will say: ~ It works! www32.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 wwwX.ws.nsrc.org you would do:

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

And look for the line that says:

ErrorLog ${APACHE_LOG_DIR}/error.log

And change the line to read:

ErrorLog ${APACHE_LOG_DIR}/wwwX.error.log

Now look for the line that says:

CustomLog ${APACHE_LOG_DIR}/access.log combined

And change this to read:

CustomLog ${APACHE_LOG_DIR}/wwwX.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:

wwwX.access.log  wwwX.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.