Let's say you have a production server configured to serve two domains: thirld.com and example.com. For testing, you also have a local virtual machine serving these domains. How can you visit these domains as served by your local VM?
The first step is to make sure your host machine can talk to the VM. By default, VirtualBox VMs are set up with NAT networking, which means that the VM can access the Internet, but the host can't access the VM, since it has no way to address it. To fix this:
- Go to VirtualBox settings (the global ones, not the VM's settings),
and create a host-only network
vboxnet0. Make sure it has DHCP enabled.
- Go to your VM's settings and add
vboxnet0as a second, host-only network adapter.
In your VM, add the following to
auto eth1 iface eth1 inet dhcp
Then start the interface with
ifup eth1. The VM will get an IP address like
192.168.56.101, while the host has an IP address like
ifconfigto figure these out.
At this point, you should verify that the host can ping the VM, and the VM can still access the Internet. (This is why we kept the first network adapter as NAT; otherwise the VM couldn't access the Internet.)
The final step is to fool your host into believing that your domains actually
live on the VM. On the host machine, add the following to
192.168.56.101 thirld.com example.com
Now load those domains in your browser. They will hit the VM instead of the production server. Congrats, you have a way to test your websites locally before pushing them to production. The main benefit of this approach is that your VM and your production server can have exactly the same configuration, so you don't need to keep separate configs for production and testing.
(Note that if you loaded
192.168.56.101 in your browser directly, you'd only
be able to see one of the domains, and not the other. Also, if your website
uses absolute URLs, various resources such as CSS files would be loaded from
the production server rather than your VM. This is why the