Forum Moderators: phranque
I've setup a local apache web-server with php, mysql and perl... it's all working nicely and a very nice platform for local development.
However, I'd like to be able to point my browser to mydomain.com, and have it find the files on 127.0.0.1 ... Do i need some sort of local DNS for that? I looked into apache's VirtualServers, but that doesn't seem to be what I need.
Sorry for the simple question, I feel this is something I should know to do!
Cheers,
Alex
On windows, find a file named "hosts" in the "windows" directory and add your host name(s) with the localhost address 127.0.0.1, if you don't have a "hosts" file, then you will have to create one using a text editor. Use "HOSTS.SAM" also in the windows directory as an example (you can open it with notepad).
On *nix edit the file /etc/hosts and add "mydomain.com" with 127.0.0.1, leaving the other entries intact.
In case you decide to add other hosts to your set up:
With name based hosting in Apache you can have multiple domains all with 127.0.0.1, if that is the case, just keep adding them to the "hosts" file and Apache will present the appropriate site based on the domain name entered. So you could have mydomain.com, mydomain1.com, mydomain2.com, etc. defined in virtual host containers and as long as you have entries in DNS or a "hosts" file the correct sites are displayed.
Have fun!
Let's start back here:
First, create yourself a "apacheon.bat" and "apacheoff.bat" that will turn on and off apache. Make a icon on the desktop or start menu to them (change paths to suit your system):
--- ApacheOn.bat
@rem remove the old file
del c:\windows\hosts
@rem install new
copy c:\windows\hosts.loc c:\windows\hosts
@rem lets go apache
c:\apache\apache.exe
rem done
----ApacheOFF.bat
@rem remove the old file
cd c:\Apache
apache -k shutdown
del c:\windows\hosts
@rem install new
copy c:\windows\hosts.net c:\windows\hosts
rem del c:\Apache\logs\httpd.pid
----------------------
To explain: The apacheon.bat installs a new "hosts" file in your windows directory. The new hosts file will contain domain name to IP resolution that will point at your apache setup.
Create a file "hosts.loc" (which is short for hosts.local) that contains your domain names (we'll get to how to setup apache to deal with this in a minute).
First an example local hosts file:
# 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 localhost
127.0.0.15 www.webmasterworld.com
127.0.0.11 www.searchengineworld.com [searchengineworld.com]
127.0.0.9 www.team-mp3.com [teammp3.com]
127.0.0.22 phdss.com
127.0.0.2 showcase.netins.net
127.0.0.4 www.joefarmer.com
127.0.0.5 www.temp.com
127.0.0.10 www.cgi.com
127.0.0.12 www.newsraid.com [newsraid.com]
127.0.0.13 www.traxis.com
127.0.0.14 www.collective.com
127.0.0.20 www.netins.net
So, we kick on apache with the "apachon.bat", and it copies the "hosts.loc" file to "hosts" file. The hosts file is a core domain <-> ip translation that browsers look to first. How do we make apache recognize those as our domains and return content to the browser?
We do it by setting up virtual hosts in the apache httpd.conf file:
<VirtualHost 127.0.0.15>
DocumentRoot /ww/webmasterworld
ServerName www.webmasterworld.com
ServerAdmin foo@webmasterworld.com
ScriptAlias /cgi-bin /ww/webmasterworld/cgi-bin
TransferLog logs/WebmasterWorld-access.log
ScriptLog logs/WebmasterWorld-scripterror.log
LogLevel debug
</VirtualHost>
The first line tells apache on what IP it should respond. Look up at the hosts.loc file. Notice the IP number? Now when we connect to webmasterworld.com from the browser, it requests it from 127.0.0.15. Apache is listening on that ip, and responds with the website specified in the virtual host config.
Create a virtual host for each of your sites. Make your local directories, identical to your online directories - and you are in like flynn.
---
To turn if off, we need to put back a clean hosts file, and shut off Apache. The apacheoff.bat file, over writes your host file with a clean hosts file. Before you ever start, make a backup copy of your current \windows\hosts file to "hosts.net". That will be your "keeper" hosts file that will always be clean.
Caveats:
I've yet to figure out how to get IE to "uncache" the domain<->ip resolution. That means if you hit a website the ip is resolved until the next reboot.
However! Opera will resolve the ip<->domain name each time it is started. So you can run local site, turn off Apache, restart Opera - and your in like flynn (not that I actually know who Flynn was).
The big Recap:
Create the two batch files show above and make shortcuts from your desk top to them named apacheon and apacheoff.
Create a backup of your current \windows\hosts file to \windows\hosts.net
Create a new hosts file called "\windows\hosts.loc" that contains your virtual site ip to domain name resolution.
Create your Virtual Hosts in your apache httpd.conf file.
This is all good for windows 9X/ME.
Netscape 6.0 will not load on my windows machine if Apache is running. Its like it hangs there waiting for apache to go away. If I shut down apache Netscape 6 loads right up. If I then start apache both run together fine.
Anyway, this'll come in really handy when I actually own the domain :P I guess at this late stage of development, I should really register it!!
A single hosts file will do until then...
Alex ;)