Solved

Setting virtual hosts for local computer

Posted on 2002-06-27
2
150 Views
Last Modified: 2013-11-18
I am a developer working on several projects.  Each of these is separate, and I'll likely have more in the future.  Each project has it's own domain and may be hosted in various places.  When I'm developing I like to have all the files on my local computer.  So I have several sub-directories, each one containing the files needed for that project.
/var/www/html/project1
/var/www/html/project2
/var/www/html/project3
etc

If I want to view the files in a browser I do something like
http://localhost/project1/afile.html
or
http://localhost/project2/thisfile.php

That is fine, except now my designer is using some include files like this
/includedfile.html

So no matter where he wants that file included (meaning on a page that may be 2 sub-directories lower than the root of the web) it will grab includedfile.html from the root directory of the web.

This works fine for each projects live domain that is hosted on a production server.
So www.project1.com/somethings/directory/testfile.html
may have the include of /includedfile.html.
includedfile.html is located at www.project1.com/includedfile.html

That all works fine, but I now need to get that same functionality locally.
If I type in localhost/somethings/directory/testfile.html I don't get the included file because apache has /var/www/html as the document root for all on the server.

What I am trying to do is set up virtual hosting for each project on my local machine.  I would like to be able to use something like http://project1.localhost and get that directory of files.  So then I can type http://project1.localhost/somethings/directory/testfile.html and the includedfile.html will be there properly because project1.localhost has the document root set at /var/www/html/project1.

Then the different project files in each sub-directory won't interfere with each other.

Is there a way to accomplish this locally and what do I alter and set up?

Mindarch
0
Comment
Question by:mindarch
2 Comments
 
LVL 15

Accepted Solution

by:
samri earned 200 total points
Comment Utility
Mindarch,

it is possible.

What platform is your local workstation.

One possibilities is to have the www.project1.com, www.project2.com, etc. to be defined in the local hosts file.  In Unix, check the file /etc/hosts/, or if you are running Win2K (or NT) chech the file in c:\winnt\system32\drivers\etc\hosts

For example Ihad this in my c:\winnt\system32\drivers\hosts file;

127.0.0.1     www.a.com
127.0.0.1     www.b.com
127.0.0.1     www.c.com
127.0.0.1     www.d.com

Next if to configure apache to do VirtualHosting.

Apache has a great docs site for virtual hosting;


http://httpd.apache.org/docs-2.0/vhosts/
http://httpd.apache.org/docs-2.0/vhosts/examples.html

--some simple example cut and paste from that site.

Running several name-based web sites on a single IP address.

Setup:

Your server has a single IP address, and multiple aliases (CNAMES) point to this machine in DNS. You want to run a web server for www.example1.com and www.example2.org on
this machine.

      Note: Creating virtual host configurations on your Apache server does not magically cause DNS entries to be created for those host names. You must have the names in
      DNS, resolving to your IP address, or nobody else will be able to see your web site. You can put entries in your hosts file for local testing, but that will work only from
      the machine with those hosts entries.


Server configuration:

           # Ensure that Apache listens on port 80
           Listen 80

           # Listen for virtual host requests on all IP addresses
           NameVirtualHost *

           <VirtualHost *>
               DocumentRoot /www/example1
               ServerName www.example1.com

               # Other directives here

           </VirtualHost>
           
           <VirtualHost *>
               DocumentRoot /www/example2
               ServerName www.example2.org
           
               # Other directives here

           </VirtualHost>
0
 

Author Comment

by:mindarch
Comment Utility
Thanks.  Gave me what I needed.  
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now