Jump to content
The Uniform Server Community

Virtual Hosts - a little different twist


Keethr
 Share

Recommended Posts

I have read the posts about setting up Virtual Hosts, and I have it going just fine. However, I want to be able to run my development sites from several different computers using my USB drive.

 

As I understand the virtual hosts, the virtual sites need to be included in the computer's hosts file. The problem I'm having is that when I take my USB drive to another computer the Apache configuration file is set up OK, but the computer's hosts file doesn't have the mapping.

 

Is there any way to make Virtual Hosts portable so that I don't have to make any changes to each computer I want to use? (i.e., editing the hosts file)

Link to comment
Share on other sites

For a test server you do indeed need the local domain name/IP address pair in you local host file otherwise your browser will swan off to the Internet in order to resolve the name.

 

One way to resolve your problem is to write two batch files, one to back up the host file and append your name/IP address pairs to the original host file. While the other batch file simply restores the original host file when you are finished.

 

After you have tested these files call them from server_start and stop batch files just saves a few mouse clicks.

 

The only minor irritation comes when running them on different OSs or if the host file does not exist. Requires a little more thought and extra code but not impossible.

 

If there is a simpler way I am sure some one will post a solution.

Link to comment
Share on other sites

My last post was a bit of a copout so I thought I better give a few real pointers.

 

Depending on the OS the host file is located in:

 

Windows 95/98/Me c:\windows\hosts

Windows NT/2000/XP Pro c:\winnt\system32\drivers\etc\hosts

Windows XP Home c:\windows\system32\drivers\etc\hosts

 

You need to automatically determine which OS is being used and then select the correct path to use. The follow provides a template to use not tested thats upto you to check out.

 

Create these three files in the folder where server_start bat is located

 

hosts_to_be_added.txt

 

# list of virtual hosts to add

127.0.0.1 test1

127.0.0.1 test2

127.0.0.1 test3

127.0.0.1 test4

 

add_hosts.bat

 

rem How to append virtual host list to local host file

 

rem set potential host paths

 

set host_path1="c:\winnt\system32\drivers\etc"

set host_path2="c:\windows\system32\drivers\etc"

set host_path3="c:\windows"

 

rem for Windows NT/2000/XP Pro

IF EXIST "%host_path1%" (

copy %host_path1%\hosts hosts

type hosts_to_be_added.txt >> %host_path1%\hosts

GOTO END

)

 

rem Windows XP Home

IF EXIST "%host_path2%" (

copy %host_path2%\hosts hosts

type hosts_to_be_added.txt >> %host_path2%\hosts

GOTO END

)

 

rem for Windows 95/98/Me

IF EXIST "%host_path3%" (

copy %host_path3%\hosts hosts

type hosts_to_be_added.txt >> %host_path3%\hosts

GOTO END

)

 

:END

ECHO DONE

Pause

 

 

restore_hosts.bat

 

rem When finished with servers restore local host file

 

rem set potential host paths

 

set host_path1="c:\winnt\system32\drivers\etc"

set host_path2="c:\windows\system32\drivers\etc"

set host_path3="c:\windows"

 

rem for Windows NT/2000/XP Pro

IF EXIST "%host_path1%" (

copy hosts %host_path1%\hosts

GOTO END

)

 

rem Windows XP Home

IF EXIST "%host_path2%" (

copy hosts %host_path2%\hosts

GOTO END

)

 

rem for Windows 95/98/Me

IF EXIST "%host_path3%" (

copy hosts %host_path3%\hosts

GOTO END

)

 

:END

ECHO DONE

pause

 

 

 

With these three files in place before starting your servers run add_hosts.bat, when you have finished with the servers run restore.bat with minor changes you can tailor this outline solution to your needs.

 

Hope that was a little more informative than my last post

:rolleyes:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...