Apache and Virtual Hosts

Exercises

GHNOG

Cape Coast, Ghana

July, 2009

 

Install Apache version 2

 

Before installing we need to point our machines to our FreeBSD repository which is on our local NOC machine. This will speed up the time to install considerably. To do this do the following (you can use the ee editor instead of vi if you wish):

 

         # vi /etc/make.conf

 

In this file you need to add a line at the bottom and enter the following line:

 

MASTER_SITE_OVERRIDE=ftp://noc.ghe0.dns.gh/pub/FreeBSD/releases/i386/7.2-RELEASE/packages/All/

 

This is a single line. Do not split it across two lines.

 

For this exercise we will use the ports collection. Apache is already installed on your machine, so we will simulate the installation. Apache, however, is not configured to run.

 

To run the Apache installation do this:

 

    # cd /usr/ports/www/apache22

    # make

 

This will build apache version apache-2.2.11_4 (as of May 13, 2009).

 

After you type ÒmakeÓ you will be presented with an options screen. There are five pages of options you can set for Apache. For most people the default option settings are acceptable, so select ÒOKÓ to continue.

 

Make will take a few minutes to complete. Once it is done please do not type Òmake installÓ.  Now we will configure Apache to run on your machines.

 

Configure Apache version 2

 

Apache is a Third Pary software package. The configuration files for Apache reside in the /usr/local/etc directory. In order for Apache to run you must first do the following:

 

    # vi /etc/rc.conf

 

Add a line to the bottom of this file and enter:

 

    apache22_enable=ÓYESÓ

 

Now exit and save the file.

 

If you install Third Party software and need to know what to enter in the /etc/rc.conf file to enable it, read the initialization script for that software. For instance, for Apache you can do:

 

    # less /usr/local/etc/rc.d/apache22

 

You will see comments at the top of the file describing the options you can place in the /etc/rc.conf file to run the service. Press ÒqÓ to exit from the less command.

 

 

Start Apache version 2

 

This is quite simple. Now that the service has been enabled in /etc/rc.conf type:

 

    # /usr/local/etc/rc.d/apache22 start

 

If you would like to prove that Apache is working on your computer open a web browser and enter in the address:

 

         http://localhost/

 

You should see a page that says, ÒIt works!Ó

 

The file where this text resides is:

 

         /usr/local/www/apache22/data/index.html

 

Configure Virtual Hosts in Apache 2

 

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:

 

    /usr/local/www/share

 

All of your PCs have multiple entries for their assigned IP addresses. At the very least you have:

 

         pcX.ghe0.dns.gh ==> 41.218.234.1xx

         wwwzone-nameÓ.ghe0.dns.gh ==> 41.218.234.1xx

 

We will take advantage of this for the exercise.

 

Let's create three web directories:

 

    # cd /usr/local/www/

    # mkdir share

    # cd share

    # mkdir default

    # mkdir mtg

    # mkdir ws

 

Now let's copy the original index.html file to our default directory, and the mtg and ws directories as well..

 

         # cp ../apache22/data/index.html default/.

    # cp ../apache22/data/index.html mtg/.

    # cp ../apache22/data/index.html ws/.

 

Edit the index.html files in mtg and ws.

 

    # vi mtg/index.html

 

Change the text ÒIt works!Ó to ÒMTG Virtual HostÓ. Close and save the file.

 

    # vi ws/index.html

 

Change the text ÒIt works!Ó to ÒWS Virtual HostÓ. Close and save the file.

 

Next we need to edit the Apache configuration files to adjust for our new directory settings.

 

    # cd /usr/local/etc/apache22

    # vi httpd.conf

 

In the file httpd.conf we are looking first for the following line:

 

    DocumentRoot "/usr/local/www/apache22/dataÓ

 

Change this line to read:

 

         DocumentRoot "/usr/local/www/share/defaultÓ

 

Then look for the line that reads:

 

    <Directory "/usr/local/www/apache22/data">

 

And change this to:

 

    <Directory "/usr/local/www/share">

 

Now find the line that says:

 

         #Include etc/apache22/extra/httpd-vhosts.conf

 

And change this to:

 

         Include etc/apache22/extra/httpd-vhosts.conf

 

Now let's verify that Apache is still working.

 

         # /usr/local/etc/rc.d/apache22 restart

 

In a web browser (on your machine) open the URL Òhttp://localhost/Ó once again. It should still read, ÒIt works!Ó.

 

Now we will add the configuration for our two virtual hosts:

 

    # cd /usr/local/etc/apache22/extra

    # vi httpd-vhosts.conf

 

Find the line that says:

 

         NameVirtualHost *:80

 

And delete everything after that line. Now let's configure your two virtual hosts. Add the following to the file below the ÒNameVirtualHost *:80Ó line:

 

<VirtualHost *:80>

   ServerAdmin inst@localhost

   DocumentRoot "/usr/local/www/share/default"

   ServerName 41.218.234.112

   ErrorLog "/var/log/httpd-error_log"

   CustomLog "/var/log/httpd-access_log" common

</VirtualHost>

 

<VirtualHost *:80>

   ServerAdmin inst@localhost

   DocumentRoot "/usr/local/www/share/mtg"

   ServerName www.Ózone-nameÓ.ghe0.dns.gh

   ErrorLog "/var/log/mtg-error_log"

   CustomLog "/var/log/zone-access_log" common

</VirtualHost>

 

<VirtualHost *:80>

   ServerAdmin inst@localhost

   DocumentRoot "/usr/local/www/share/ws"

   ServerName pc12.ghe0.dns.gh

   ErrorLog "/var/log/ws-error_log"

   CustomLog "/var/log/ws-access_log" common

</VirtualHost>

 

Once you are done save the file and quit. Now we must restart Apache to load the new configuration. You can do this two different ways:

 

Either:

 

         # /usr/local/etc/rc.d/apache22 restart

 

Or:

 

         # apachectl restart

 

After restarting prove to yourself that virtual hosting is working. To do this let's install a text-based browser to make things simpler. This browser is called lynx and is an excellent tool to have if you are working on a machine with no graphical interface. To install the lynx text-based web browser do:

 

    # pkg_add -r lynx

 

This might take just a second. Once installation completes type:

 

    # rehash

 

To view a page in lynx you simply type:

 

    # lynx url

 

So, for example, to check that virtual hosting is working on your machine you could use lynx and type:

 

    # lynx localhost

    # lynx 127.0.0.1

    # lynx 41.218.234.1xx

    # lynx pcX.ghe0.dns.gh

    # lynx www.Ózone-nameÓ.ghe0.dns.gh

 

Do you see what you expect? Remember to replace the ÒXÓ and ÒxxÓ with the appropriate numbers. For example, for pc10.ghe0.dns.gh the urls would be:

 

á      localhost

á      127.0.0.1

á      41.218.234.110

á      pc10.ghe0.dns.gh

á      wwwzone-nameÓ.ghe0.dns.gh

 

If you have defined additional hosts for your IP address feel free to add them in to your /usr/local/etc/apache22/extra/httpd-vhosts.conf file. Remember to create a directory for their DocumentRoot and to place an html file in that directory.