OpenSourceVarsity

pdf icons

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 Apache’s 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 when you test apache web server enter http://localhost in a Browser’s address bar and press Go or the Enter key your HTML page content 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 folder d:\mysite is where you’ve loaded all your website’s resources.

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

To create a Apache virtual domain you have to make changes within Apache’s httpd.conf file which is normally located at:

C:\Program Files\Apache Group\Apache2\conf\httpd.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.

To read the rest of this Tutorial you have to be logged in.

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, and 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.

NOTE: This is assuming that Apache was installed in C:\Program Files on your local computer.
Open httpd.conf file in any text (i.e. ASCII) editor of your choice.

Locate the code block contained within
<Directory “C:/Program File/Apache Software Foundation/Apache2.2/htdocs”> </Directory> enties as shown in Diagram 1.

diagram1 Diagram 1

Select this code block. Copy and Paste this code block immediately below the original.

NOTE: Please ensure that both
<Directory . . . .> and the </Directory>tags exist.

Change <Directory “C:/Program Files/Apache Software Foundation/Apache2.2/htdocs” > </Directory>.

To

<Directory “D:/public_html/virtual_host”> as shown in Diagram 2

NOTE: Please note the use of the forward slash above.

diagram2 Diagram 2

Then scroll down to the Supplemental configuration section at the end, and locate Virtual host the section (approx. around line 460) as shown in diagram 3:

diagram3 Diagram 3

Uncomment the Virtual host (Remove the # from the second line as shown above in diagram 3)
#Virtual hosts
Include conf/extra/httpd-vhosts.conf

Save httpd.conf and close it

These entries within Apache’s httpd.conf file ensure that Apache knows that D:\public_html\virtual_host exists and that directory permissions are set.

Once Apache knows the existence and location of D:\public_html\virtual_host appropriate entries in the httpd-vhost.conf must be made.

httpd-vhost.conf is normally located at
C:\Program Files\Apache Software Foundation\Apache2.2\conf\extra\httpd-vhosts.conf

Open the above conf file in any text (i.e. ASCII) editor of your choice. Scroll right down to the end of the httpd-vhost.conf file and you will notice Apache’s virtual domain example as shown in diagram 4.

diagram4 Diagram 4

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.
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot “C:/Program Files/Apache Software Foundation/Apache2.2/docs/dummy-host.localhost”
ServerName www.pma.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost >

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.

# 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.
<VirtualHost *:80 >
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
</VirtualHost >

Next:

Now let’s take a look at the <VirtualHost> </VirtualHost > XML segment as shown in diagram 5.

diagram5 Diagram 5

<VirtualHost *:80 >
ServerAdmin ivan@ivanbayross.com
DocumentRoot “D:/public_html/virtual_host”
ServerName www.virtualhost.com
ErrorLog “D:/public_html/virtual_host/error/errorlog.txt”
#CustomLog “logs/dummy-host2.ivan-access.log” common
</VirtualHost >

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:\public_html\virtual_host\errors. Then using Notepad create an empty file named errorlog.txt within this folder. If the errorlog.txt file is not created, when you restart Apache Web Server, Apache will throw an error.

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

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

diagram6 Diagram 6

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

diagram7 Diagram 7

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 navigates 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.1 www.virtual_host.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.virtual_host.com Apache will display the contents of the index.html file you’ve place in the folder D:\public_html\virtual_host

If all this works perfectly then you can build the rest of your website within the folder D:\public_html\virtual_host

April 15, 2016
Design by Ivan Bayross and Meher Bala © 2017 All Rights Reserved
X