Apache web server lab

configure apache for 2 name-based virtual hosts


Functionality of the server

Verify functionality of the default installation of apache on your machine, by turning it on and off to observe its effects. Turn it on:

systemctl  start  httpd

Verify that it's running. A couple commands that return feedback:

verification 1:  systemctl  status  httpd

verification 2:  ps -ef | grep httpd [as what user(s) are the returned processes running?]

And the acid test, browse port 80 on your machine from a graphical browser (enter http://127.0.0.1 for the address) or the lynx text browser:

verification 3:  lynx 127.0.0.1

How many copies of apache appear to be running:

ps -ef | grep sbin/httpd | grep -v grep | wc -l

Now turn apache back off:

service httpd stop

and rerun the above 3 verifications to satisfy yourself apache isn't running anymore.

Now you will control a few operations of the web server by manipulating certain files.


Role and operation of the config file

Learn the effects of directives in the configuration file, /etc/httpd/conf/httpd.conf. Find the line with the "Listen" port directive that tells apache to run on port 80. Change it to some other number, say 79, and save the file. Now start apache, and run the above 3 verifications. The first two do demonstrate apache is running, but the 3rd makes it look as if apache is unresponsive. To contact apache you have to do so on the port number it’s listening to. Enter http://127.0.0.1:79 in your graphical browser, or in lynx:

lynx 127.0.0.1:79

Run W/ireshark or tcpdump while doing this to see what port numbers are used. Now set apache back to port 80. Next find a line that contains "StartServers" followed by a number. It might be 5::

StartServers 5

If it's there change the number to 15. If it's not insert it with that number:

StartServers 15

Restart apache. Now how many copies of apache are running? Change the number back to 8 and restart again.

Implementing two name-based virtual hosts

If instructor supplies a nameserver - you will change /etc/resolv.conf

Your instructor may have set up the nameserver to map 2 different names into your address, for example:

hostq A 222.33.44.55
qhost A 222.33.44.55

With 2 names to work with, you can have apache offer different, per-name virtual hosts (provided you point your resolver to the instructor's nameserver with a "nameserver" directive in /etc/resolv.conf to take advantage of instructor's name service).

If instructor does not supply a nameserver - you will change /etc/hosts

Alternatively, you'll have to give definition to those names locally in your /etc/hosts file. For this exercise, it's important for others who will contact you, not you yourself, to define these names that refer to you. So they are the ones who need to configure their /etc/hosts files. If you will be contacting another student, find his address and put it in your /etc/hosts. If another student will contact you, give him your address so he can put it in his /etc/hosts. There, place lines like the following, but use whatever the actual target IP address is for the machine running the web server:

222.33.44.55   hostq
222.33.44.55   qhost

Now, one way or the other, you and your friends can use these names and they will successfully resolve to addresses.

This does nothing unless apache cares. Make apache care. Here's the model, for you to edit your config file (use your actual IP address):

NameVirtualHost 111.22.33.44

<VirtualHost 111.22.33.44>
ServerName 111.22.33.44
DocumentRoot /var/www/html
</VirtualHost>

<VirtualHost 111.22.33.44>
ServerName hostq
DocumentRoot /var/www/website-first
</VirtualHost>

<VirtualHost 111.22.33.44>
ServerName qhost
DocumentRoot /var/www/website-second
</VirtualHost>


Additional to config file additions similar to these, you'll need to create 2 new directories somewhere corresponding to the above "DocumentRoot" references. In each, place a webpage file, that is, a file containing displayable html. Name the files index.html in both cases. Make them distinguishably different, so you know which is which when they show up in the browser. At the simplest, the content for one could be:

<h1>One</h1>

and for the other:

<h1>And another</h1>

for example.

Restart apache, then view your site as http://hostq and alternatively as http://qhost (or whichever hostnames are your machine's). Different pages should come up. Invite your neighboring students to test your site from their browsers as well. Test theirs.