Ubuntu Named Based Hosting
Server Training - Web Server

With Name Based Virtual Hosting, you can configure your server so that all web sites that it serves are accessed by only one IP Address. Name Based Hosting is extremely useful as you can save money by using one IP Address.  Now with the more modular approach of Apache 2.2.8 it is Name Based Hosting is even easier to configure as you are only modifying one file.

If you are working on server that is based on Centos, you should follow the Name Based Hosting example listed HERE.

If you look in the /etc/apache2 directory, you'll see two "sites" directories. One is "sites-available" and the other is "sites-enabled". The only one you'll have to worry about is the "sites-available". Change to the sites-available directory, and you'll only see one file there.

cd /etc/apache2/sites-available/


To set up for a new web site, just copy the "default" file to a new file. But, give the new file the name of the web site that you're setting up for.

So, if you're setting up a site for www.example.com, your command line would be:

sudo cp default www.example.com

Now, open the new file for editing.

What makes the configuration step so easy, is that you only have to deal with a few lines at the top of the configuration file.

Here are the lines you'll need to configure to enable your "www.example.com" site. Make sure that you 

modify the text in blue.  The default port for all virtual hosts with Name Based Hosting is port 80.

NameVirtualHost *:80

<VirtualHost *:80>

ServerAdmin webmaster@localhost

DocumentRoot /var/www/example

ServerName www.example.com

Create the website directory /var/www/example, and set the proper ownership and permissions settings for it.

sudo chown -R www-data /var/www/example


This command will change the permissions recursively for the directory that you will place your server files in. The user www-data is the default user for apache.

Place the appropriate files in it. For example an index.html page for testing.

You'll now use the "a2ensite" utility to enable the new site.

sudo a2ensite www.example.com

Site www.example.com installed; run /etc/init.d/apache2 reload to enable.

For the final step, reload the Apache configuration:

sudo /etc/init.d/apache2 force-reload

* Reloading web server config apache2

 

Add Additional Virtual Hosts

Add a second site and follow the same procedure.  This time copy the /etc/apache2/sites-available/default to a new file called 2qw (just a random example).

Edit your file.  Notice this site and all Name Based Hosting will be on port 80.

NameVirtualHost *:80

<VirtualHost *:80>

ServerAdmin webmaster@localhost

DocumentRoot /var/www/2qw

ServerName 2qw.com

Create the directory /var/www/2qw.  Now drop your index.html file in that file location and change permissions.

sudo chown -R www-data /var/www/2qw

To test out your new sites, you can just add new entries to your local client computer's /etc/hosts file. You'll have multiple entries for the same IP address, but that's fine. You won't need to mention anything about port numbers in the hosts file. The server will take care of that. Here is an example.


127.0.0.1 localhost

127.0.1.1 ub

192.168.5.33 www.example.com

192.168.5.33 2qw.com

Be sure to place the IP address for your server. 

Note: These /etc/hosts file changes are only for testing.  For Internet accessible servers you will need to modify DNS.

Name-based virtual hosting works because the client's browser will send host header information that will reach the server. Since some operating systems and some router equipment won't allow that information to get through, this technique may or may not work on your organization's LAN.

 


Copyright CyberMontana Inc. and BeginLinux.com
All rights reserved. Cannot be reproduced without written permission. Box 1262 Trout Creek, MT 59874