Creating Virtual Domains Under Apache

When Apache is installed on a M.S. Windows XP / Vista computer the Apache installer places Apache’s file structure in:

C:\Program Files\Apache Group\Apache2

Apache’s index file, which is displayed when http://localhost is typed into a Browser window and the Go button or the Enter key is pressed, resides in:

C:\Program Files\Apache Group\Apache2\htdocs

If you do not want Apaches default index page to be displayed when http://localhost is typed in a Browser’s address bar and the Go button or the Enter key is pressed, simply create your own HTML file, name it index.html and place this in:

C:\Program Files\Apache Group\Apache2\htdocs

Now if you enter http://localhost in a Browser’s address bar and press Go or the Enter key your HTML page will be rendered in the Browser’s window.

This is an indication that Apache is working perfectly.

Having said that, it’s definitely not good practice to place all your website’s resources within the folder that Apache uses for its own resources i.e.:

C:\Program Files\Apache Group\Apache2\htdocs

It’s good programming practice to keep your website’s resources separate from Apache’s internal resources.

All that is required is that you create a separate folder, anywhere on your hard disk and place your website resources within that folder.

For example: d:\mysite

Now all that remains is that Apache needs to be informed that the folderd:\mysite is where you’ve loaded all your website’s resources.

You do this by creating a Virtual Domain under Apache and binding this domain to the folder where your website’s resources are placed.

To create a virtual domain you have to make changes within Apache’s httpd.conf file.

Apache’s httpd.conf file is resides in:

C:\Program Files\Apache Group\Apache2\conf

Navigate to that folder and the first thing to do is to make a copy of Apache’s httd.conf file and save this within Apache’s conf folder i.e. httpd_copy.conf.

This way if you’ve made some errors when adding a virtual domain to the httpd.conf file and Apache stops working, all you have to do is to overwrite Apache’s httpd.conf file with the copy you’ve made, then ensure that the file is re-named httpd.conf and Apache should start up again.

NOTE: In case you do not keep a copy of Apache’s httpd.conf and you’ve damaged the original httpd.conf file you have to re-install Apache from scratch, to get a working copy of Apache web server.

Now open the httpd.conf file in your favorite ASCII editor, (Notepad would do just fine). Scroll right down to the end of the httpd.conf file and you will notice Apache’s virtual domain example.

The code there should look something like:

#NameVirtualHost *:80

#VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
# ServerAdmin webmaster@dummy-host.example.com
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#

#

The NameVirtualHost and VirtualHost directives are what we are interested in.

Simply copy the whole lot from # NameVirtualHost to and paste this below the original code.

Then delete the # which appears before each entry as shown below.

Please notice the section where the # are not deleted.

NameVirtualHost *:80

# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.


ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /www/docs/dummy-host.example.com
ServerName dummy-host.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common

Next:

Change
NameVirtualHost *:80

To read as:
NameVirtualHost 127.0.0.1:80

Notice that the # has been deleted
And
The local host ip 127.0.0.1 has replaced the * in this entry

Now let’s take a look at the XML segment.


ServerAdmin ivan@ivanbayross.com
DocumentRoot d:\mysite
ServerName www.mysite.com
ErrorLog d:\mysite\errors\errorlog

Notice that the:
ServerAdmin has a legitimate Email ID bound to it.
DoumentRoot has the physical path to your websites resources
ServerName can have any name bound to it, I’ve chosen www.mysite.com
ErrorLog has the path to where Apache can keep its error log file

NOTE: Do not forget to create the folder d:\mysite\errors. Then using Notepad create an empty file named errorlog within this folder. Please notice the absence of any file extension.

Save the httpd.conf you’ve been editing and re-start Apache.

To restart Apache simply right click on the icon in the task bar and a popup window will appear as shown in diagram 1.

Diagram 1

Click on Open Apache Monitor and a popup window will appear as shown in diagram 2

Diagram 2

Simply click the Restart button. If all is well Apache will startup perfectly.

There’s a few more steps to be carried out to check is you’ve created the Virtual domain correctly, they are:
Making an entry in the hosts file.
Using the file Browser navigate to the folder C:\Windows\System32\drivers\etc
Within this folder there will be a file called hosts.

NOTE: If you have never used the hosts file ever before then you will find a file called hosts.sam or hosts.tmp in this folder. Copy this file to a file named hosts and you are done. Then edit the contents of hosts as indicated below.

# Copyright (c) 1993-2006 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a ‘#’ symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client

NOTE: Between the ip and the site name you must use the Tab key.

# For example:
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

127.0.0. www.mysite.com

Save hosts and exit.

Finally place a unique index.html file within the folder d:\mysite. Now when you enter the URL in the Browser address bar: http://www.mysite.com Apache will display the contents of the index.html file you’ve place in the folder d:\mysite.

If all this works perfectly then you can build the rest of your website within the folder d:\mysite

Share and Enjoy:
These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Webnews
  • Technorati
  • Facebook
  • Google Bookmarks
  • Squidoo
  • YahooMyWeb
  • Bloglines
  • Blogosphere News

Comments

Leave a Reply

You must be logged in to post a comment.


Google Talk