Keethr Posted October 5, 2006 Report Share Posted October 5, 2006 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) Quote Link to comment Share on other sites More sharing options...
Ric Posted October 6, 2006 Report Share Posted October 6, 2006 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. Quote Link to comment Share on other sites More sharing options...
Ric Posted October 7, 2006 Report Share Posted October 7, 2006 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\hostsWindows NT/2000/XP Pro c:\winnt\system32\drivers\etc\hostsWindows 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 add127.0.0.1 test1127.0.0.1 test2127.0.0.1 test3127.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 ProIF EXIST "%host_path1%" (copy %host_path1%\hosts hoststype hosts_to_be_added.txt >> %host_path1%\hostsGOTO END) rem Windows XP HomeIF EXIST "%host_path2%" (copy %host_path2%\hosts hoststype hosts_to_be_added.txt >> %host_path2%\hostsGOTO END) rem for Windows 95/98/MeIF EXIST "%host_path3%" (copy %host_path3%\hosts hoststype hosts_to_be_added.txt >> %host_path3%\hostsGOTO END) :ENDECHO DONEPause 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 ProIF EXIST "%host_path1%" (copy hosts %host_path1%\hostsGOTO END) rem Windows XP HomeIF EXIST "%host_path2%" (copy hosts %host_path2%\hostsGOTO END) rem for Windows 95/98/MeIF EXIST "%host_path3%" (copy hosts %host_path3%\hostsGOTO END) :ENDECHO DONEpause 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 Quote Link to comment Share on other sites More sharing options...
Keethr Posted October 11, 2006 Author Report Share Posted October 11, 2006 Thanks, That helps. I hadn't thought of using a batch file and your code looks like it should work. Vista has the hosts file in the same place as XP Pro, so it should work for Vista, also. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.