I wanted to be able to access my development environment from within Parallels, which is running Windows XP. Since requests in the Parallels machine web browser don’t reach apache running on my Mac, I had to find some way to to access it. If Parallels is set to “Shared Networking,” it will be included in a “simulated network” with the Mac, and as such you can just treat it like any other networked computer. So the question becomes, how do you connect to a virtual host from a computer in the same LAN?
Step 1: Create the Virtual Host
When setting up a development environment, it’s almost always a good idea to use virtual hosts, because then you can easily host many separate test sites on the same machine. To do so:
- Find the apache directory on your computer. Mine was at
/etc/apache2/. - Open the
httpd.conffile and find the following lines:
# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf
- Make sure the
Includeline is uncommented (make sure there’s no # at the beginning of the line - Open the
http-vhosts.conffile and add a virtual host. It should look something like this:
<VirtualHost *:80>
ServerName your.virtual.host
DocumentRoot "/your/files/location/"
</VirtualHost>
This setup basically means anyone connecting to your computer through port 80 (a web browser), looking for your.virtual.host, will see the files contained in the /your/files/location/ directory.
This is just the bare minimum for setting up a virtual host, and if you’re new to the concept you should probably explore the other options. There should be examples already in the virtual host configuration file for more info.
Step 2: Direct Your Computer to the Virtual Host
So you’ve set up the virtual host, but if you type your.virtual.host into your web browser, it will look for it directly on the web first. You need to tell the browser to go directly to your computer if your.virtual.host is specified. To do so, open up the hosts file, which is typically located in the /etc/ directory.
You should see some entries for localhost already. Add another which ties your virtual host to your local machine:
127.0.0.1 your.virtual.host
Step 3: Direct Other Computers to your Virtual Host
Now for any other networked computers, you just need to direct requests for your.virtual.host to your local computer. To do so for Unix based machines you can edit the /etc/hosts files as mentioned above. For Windows XP machines, the host file is located at C:\Windows\system32\drivers\etc\hosts.
The most difficult question to answer is, how do you know the IP address of your local computer on a network? Typically your computer will be assigned an IP address dynamically, so you’ll have to edit the hosts file every time the computer logs back on. To get the IP address, check your connection in the Network Preferences section. You could also try assigning a static IP address somewhere at the end of the available range, but this isn’t recommended because it could cause conflicts.
For Parallels though, the IP address of the Mac hosting the virtual machine is static, and can be found by viewing the “Parallels NAT” settings in the Network Preferences section. Throw that IP address in the hosts file along with the address of your virtual host, and you should be good to go!
Creating a good development environment is obviously very important for any new development project, and with database-driven applications, it’s often a hassle to keep files and data in sync. With this in mind, I tried to come up with an efficient method of pushing a local development copy of this site to its live server.