I have several computers on my local network each with a local server environment provided by XAMPP. As it stands, the local server on each computer can only serve pages stored on that machine. This means that if I have a local site on computer A that I wish to access on computer B I have to replicate that site’s entire folder structure on computer B and on all other computers I want to access the site from.
No big deal really as I currently use ChronoSync to synchronise my site’s folder structure across computers. However, it is possible to have any computer on a local network serve pages that are stored on another computer on that same network. Here’s how.
I have an iMac and a MacBook both of which have local server environments.
The /etc/hosts file on the iMac looks like this:
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost # Virtual Hosts 127.0.0.1 imac.mysite1
The /etc/hosts file on the MacBook is almost identical apart from the virtual hosts entry which is:
# Virtual Hosts 127.0.0.1 macbook.mysite1
More on virtual hosts later. For now we’re only interested in the default localhost.
The IP address of the iMac on my network is 192.168.1.10, so to access the default localhost on the iMac from the MacBook I simply add the following to the MacBook’s /etc/hosts file:
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost # Virtual Hosts 127.0.0.1 macbook.mysite1 # Network Hosts 192.168.1.10 imac.localhost
The comment on line 15 is optional and only included for clarity. I’ve named the host imac.localhost to differentiate it from the MacBook’s own localhost, but the name can be anything you like, providing it is not already configured on the iMac.
Now, on the MacBook, when I point my browser to http://imac.localhost it will access the localhost on the iMac. If I change the URL to http://localhost it will access it’s own localhost.
Accessing the imac.mysite1 virtual host configured on the iMac from the MacBook is just as straightforward. I add the following to the /etc/hosts file on the MacBook:
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost # Virtual Hosts 127.0.0.1 macbook.mysite1 # Network Hosts 192.168.1.10 imac.localhost 192.168.1.10 imac.mysite1
The only difference from the previous example is that the name (imac.mysite1) must be the same as that configured on the iMac. If not, I’ll be unable to access the virtual host and it will default to localhost on the iMac.
I can of course delete macbook.mysite1 now if I wish, but I find it’s convenient to keep both, particularly as the MacBook may not always be connected to my network. This way, when it’s connected I can point my browser to http://imac.mysite1 and when not connected I can use http://macbook.mysite1.
As the iMac’s IP address is hard-coded in the MacBook’s /etc/hosts file it’s important to ensure that the iMac is always assigned the same IP address on the network. Check here for ways to assign a computer a static IP address.
Tested using XAMPP 1.7.2a for Mac and Snow Leopard 10.6.2
A native Brit exiled in Japan, Steve spends too much of his time struggling with the Japanese language, dreaming of Fish & Chips and writing the occasional blog post he hopes others will find helpful.
Awesome thank you very much 🙂 I will give it a try and may do a blog post about it once I have my new website going. 🙂
Okay Thanks Steve 🙂 if that other way is possible do you know of a link where I can go and find that way? I much prefer that way to be honest. Just so that i don’t have fix host files on both computers every time i have a new site.
So just so that I am clear, both hosts files on the macbook and iMac have to have the same information so that i can access it? Is there a way where I don’t need to do that at all?
…So just so that I am clear, both hosts files on the macbook and iMac have to have the same information so that i can access it?
If you have a virtual host named test defined on your iMac, the entry in the iMac’s hosts file may look like this;
If you want to access that same site from another machine you’ll need to add the following to the hosts file of that other machine;
Where 192.168.1.10 is the IP address of the iMac on your network.
…Is there a way where I don’t need to do that at all?
It’s possible, but this method has always worked for me.
Thanks for the article Steve. I have a question, I tried this near the end of day and had some odd issues, I missed the part about “both” systems running a local MAMP system, am I correct?
Should the system hosting the site be the only httpd.conf changed, from your other article “Configuring Virtual Hosts in MAMP on OSX”.
I think i’ve over thought this, I realized that the other article was about running more than one site, but I’ll leave the question in.
I had the second machine seeing the site but not rendering any server side pages (php).
I’ve used MAMP for a few years, and finally wanted to share the current dev with colleagues.
I missed the part about “both” systems running a local MAMP system, am I correct?…
You can use MAMP/MAMP. Currently, I’m using MAMP/XAMPP and it works fine.
Should the system hosting the site be the only httpd.conf changed…
Yes, but as you noted this is only a concern if you’re using virtual hosts.
I had the second machine seeing the site but not rendering any server side pages (php)…
On machine A if you type the IP address (http://192.xxx.x.xx) of machine B in your web browser it should serve the MAMP start page of machine B. This start page has a phpinfo option which will show you what version of PHP, if any, is running on machine B.
Access your device Settings and on Wi-Fi settings, choose the Wi-Fi network you’re using. Click on the blue right arrow and, in Proxy HTTP, change from Inactive to Manual.
Now you know what to do:
In Server enter the local network IP address of your server (mine is 192.168.1.101) and in Port enter 80 (mine was set to 80 in httpd-vhosts.conf and httpd.conf). In Authentication leave turned off.
NOTE: It will redirect ALL your device’s HTTP requests to your server (even http://www.google.com), therefore you need to set it back to INACTIVE after you’ve done testing your site on iPad/iPhone.