I'm currently working on a project and I would like to test it out on two laptops at home where one laptop connects to the localhost on the other. I am using XAMPP. How do I do this?
That's definitely possible. We'll take a general case with Apache here.
Let's say you're a big Symfony2 fan and you would like to access your symfony website at http://symfony.local/
from 4 different computers (the main one hosting your website, as well as a Mac, a Windows and a Linux distro connected (wireless or not) to the main computer.
1 Set up a virtual host:
You first need to set up a virtual host in your apache httpd-vhosts.conf
file. On XAMP, you can find this file here: C:\xampp\apache\conf\extra\httpd-vhosts.conf
. On MAMP, you can find this file here: Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
. This step prepares the Web server on your computer for handling symfony.local
requests. You need to provide the name of the Virtual Host as well as the root/main folder of your website. To do this, add the following line at the end of that file. You need to change the DocumentRoot
to wherever your main folder is. Here I have taken /Applications/MAMP/htdocs/Symfony/
as the root of my website.
<VirtualHost *:80>
DocumentRoot "/Applications/MAMP/htdocs/Symfony/"
ServerName symfony.local
</VirtualHost>
2 Configure your hosts file:
For the client (your browser in that case) to understand what symfony.local
really means, you need to edit the hosts file on your computer. Everytime you type an URL in your browser, your computer tries to understand what it means! symfony.local
doesn't mean anything for a computer. So it will try to resolve the name symfony.local
to an IP address. It will do this by first looking into the hosts file on your computer to see if he can match an IP address to what you typed in the address bar. If it can't, then it will ask DNS servers. The trick here is to append the following to your hosts file.
/private/etc/hosts
; /etc/hosts
; \Windows\system32\private\etc\hosts
;\Windows\system32\drivers\etc\hosts
;\Windows\system32\drivers\etc\hosts
;Hosts file
##
# Host Database
# localhost is used to configure the loopback interface
##
#...
127.0.0.1 symfony.local
From now on, everytime you type symfony.local on this computer, your computer will use the loopback interface to connect to symfony.local. It will understand that you want to work on localhost (127.0.0.1).
3 Access symfony.local
from an other computer:
We finally arrive to your main question which is:
How can I now access my website through an other computer?
Well this is now easy! We just need to tell the other computers how they could find symfony.local
! How do we do this?
3a Get the IP address of the computer hosting the website:
We first need to know the IP address on the computer that hosts the website (the one we've been working on since the very beginning). In the terminal, on MAC and LINUX type ifconfig |grep inet
, on WINDOWS
type ipconfig
. Let's assume the IP address of this computer is 192.168.1.5.
3b Edit the hosts file on the computer you are trying to access the website from.:
Again, on MAC, this file is in /private/etc/hosts
; on LINUX, in /etc/hosts
; and on WINDOWS, in \Windows\system32\private\etc\hosts
(if you're using WINDOWS 7, this file is in \Windows\system32\drivers\etc\hosts
).. The trick is now to use the IP address of the computer we are trying to access/talk to:
##
# Host Database
# localhost is used to configure the loopback interface
##
#...
192.168.1.5 symfony.local
4 Finally enjoy the results in your browser
You can now go into your browser and type http://symfony.local
to beautifully see your website on different computers! Note that you can apply the same strategy if you are a OSX user to test your website on Internet Explorer via Virtual Box (if you don't want to use a Windows computer). This is beautifully explained in Crafting Your Windows / IE Test Environment on OSX.
You might wonder how to access your localhost website from a mobile device. In some cases, you won't be able to modify the hosts file (iPhone, iPad...) on your device (jailbreaking excluded).
Well, the solution then is to install a proxy server on the machine hosting the website and connect to that proxy from your iphone. It's actually very well explained in the following posts and is not that long to set up:
On a Mac, I would recommend: Testing a Mac OS X web site using a local hostname on a mobile device: Using SquidMan as a proxy. It's a 100% free solution. Some people can also use Charles as a proxy server but it's 50$.
On Linux, you can adapt the Mac OS way above by using Squid as a proxy server.
On Windows, you can do that using Fiddler. The solution is described in the following post: Monitoring iPhone traffic with Fiddler
@Dre. Any possible way to access the website from another computer by not editing the host file manually? let's say I have 100 computers wanted to access the website
This is an interesting question, and as it is related to the OP question, let me help.
You would have to do a change on your network so that every machine knows where your website is hosted. Most everyday routers don't do that so you would have to run your own DNS Server on your network.
Let's pretend you have a router (192.168.1.1). This router has a DHCP server and allocates IP addresses to 100 machines on the network.
Now, let's say you have, same as above, on the same network, a machine at 192.168.1.5
which has your website. We will call that machine pompei.
$ echo $HOSTNAME
pompei
Same as before, that machine pompei at 192.168.1.5
runs an HTTP Server which serves your website symfony.local
.
For every machine to know that symfony.local
is hosted on pompei we will now need a custom DNS Server on the network which knows where symfony.local
is hosted. Devices on the network will then be able to resolve domain names served by pompei internally.
Step 1: DNS Server
Set-up a DNS Server on your network. Let's have it on pompei for convenience and use something like dnsmasq.
Dnsmasq provides Domain Name System (DNS) forwarder, ....
We want pompei to run DNSmasq to handle DNS requests Hey, pompei, where is symfony.local
and respond Hey, sure think, it is on 192.168.1.5 but don't take my word for it
.
Go ahead install dnsmasq, dnsmasq configuration file is typically in /etc/dnsmasq.conf
depending on your environment.
I personally use no-resolv
and google servers server=8.8.8.8 server=8.8.8.4
.
*Note:* ALWAYS restart DNSmasq if modifying /etc/hosts file as no changes will take effect otherwise.
Step 2: Firewall
To work, pompei needs to allow incoming and outgoing 'domain' packets, which are going from and to port 53. Of course! These are DNS packets and if pompei does not allow them, there is no way for your DNS server to be reached at all. Go ahead and open that port 53. On linux, you would classically use iptables
for this.
Sharing what I came up with but you will very likely have to dive into your firewall and understand everything well.
#
# Allow outbound DNS port 53
#
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -p tcp --sport 53 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 53 -j ACCEPT
iptables -A OUTPUT -p udp --sport 53 -j ACCEPT
Step 3: Router
Tell your router that your dns server is on 192.168.1.5
now. Most of the time, you can just login into your router and change this manually very easily.
That's it, When you are on a machine and ask for symfony.local
, it will ask your DNS Server where symfony.local
is hosted, and as soon as it has received its answer from the DNS server, will then send the proper HTTP request to pompei on 192.168.1.5
.
I let you play with this and enjoy the ride. These 2 steps are the main guidelines, so you will have to debug and spend a few hours if this is the first time you do it. Let's say this is a bit more advanced networking, there are primary DNS Server, secondary DNS Servers, etc.. Good luck!