--> \n"; ?>
Linux on servers

Apache Configuration - Virtual Hosts

Christian Kairies
Translated by
Jana Jaeger

Starting-Point

Testing of different configurations and a local search engine requires an (additional) web server. My normal Linux development environment includes the Apache web server with the standard configuration - which is unfortunately made for a stand-alone single server. This means all changes to this configuration would deeply impact my complete development environment - a thing I rather wanted to avoid.

OK, let's have short glance at the internet resources ... quite easy to do ... even SuSE's support database features a short article on this item. Time to get to business!

Caution: The author takes no responsibility for any consequences that my arise from the procedure described in this article. PLEASE, mind to keep a backup copy of any configuration file you change!!!

Some Initial Reflections

My test system will consist of a web server which is locally reachable and provides real name access. Setting up a DNS server is thus obsolete - a few changes to etc/hosts are all I need to do. Additionally, there is no need to bind a second IP address to my network device.

The configuration of a virtual web server means: Once virtual - always virtual! Therefore I also have to reconfigure the default web server of the test environment.

Down to business

Changing /etc/hosts

First of all, we're going to edit /etc/hosts - search for the line:

127.0.0.1       localhost
192.168.100.100 machinename.local    machinename
Below this line we add a new one:
192.168.100.100 newname.local       newname
After saving the changes we can test the new entry:
ping machinename
ping newname
Both machines should be reachable by now.

Changing /etc/httpd/httpd.conf

httpd.conf is the main configuration file of the apache web server. In most of the recent Linux distributions it is located in /etc/httpd/httpd.conf. Since we plan to reach our test system by name only, we have to tell the server to deal with name-based virtual hosts reachable by one common IP address. This done by adding this line at the end of httpd.conf:

NameVirtualHost 192.168.100.100
Now we set up the first virtual web server in the next lines (you need to modify the DocumentRoot parameter to match you systems local conditions):
<VirtualHost machinename.local>
ServerName machinename.local
ServerAlias machinename*
ServerAdmin [email protected]
DocumentRoot /usr/local/httpd/htdocs
</VirtualHost>
If we did everything correct, the web server should restart without any error messages and the existing pages should be delivered as usual. The fastest way to restart the apache server works like this:
killall -HUP httpd
This way we force the web server to re-read its configuration file and use the changed settings. The cleaner way for a SuSE-powered system is:
/etc/init.d/apache restart
or
rcapache restart
Everything perfect? No error messages? Your server can be reached by the browser? Congratulations, you're almost done. Any additional virtual host can be added in the same way. One thing remains to be done - the test server needs to be configured. Go for it!

Again we add a few lines to httpd.conf, now with slightly different names and paths:

<VirtualHost newname.local>
ServerName newname.local
ServerAlias newname*
ServerAdmin [email protected]
DocumentRoot /new/path/which/does/not/overlap
</VirtualHost>
After the next apache restart (as described above) you will reach the second virtual web server by typing http://nwname.

There is one little thing to be considered when using a SuSE-based system. Any time YaST/YaST2 or SuSEconfig are run (after the installation of some additional packages) your manual changes to /etc/hosts regarding the virtual servers are overwritten. There are to workarounds to cope with this: Keep a backup copy of your modified /etc/hosts and use it to restore your settings or change the CHECK_ETC_HOSTS parameter in /etc/rc.config to "no".

Caution/Hint

This described server is everything but ready configured to be used as a productive system. It is only for local testing. If you need a secure web server, please use additional information. Also, we didn't discuss the legions of possible apache configurations.

Conclusion:

Like always in life it's very easy to do once you know how :)
In the next part we will configure an IP-based web server as it is used by web hosting services and we will go into the nitty gritty of configuration.

Links

[1] Apache Virtual Host Documentation
[2] SDB:onfiguring virtual Web servers

\n"; ?>