Web Development through a VM
I recently wrote a blog on setting up a local environment in macosx to develop locally. Although this is great and a good alternative the the web servers out there as it is faster, it is not ideal if you need to run different versions of php and also doesn't truly reflect the set up of the linux servers that you are running your website off.
That is why I have written this blog, it is a guide to setting up a virtual machine in virtualbox which you can run your website through and develop against. You can also mount the disk via sshfs meaning that you can navigate the virtual machine like a mounted disk on your mac. This seems a more secure way of developing for clients. Not only are you making your environment a more true reflection of the servers that you are running off but you are also storing the information on an encrypted disk. Which, should your laptop get stolen, we are relying on the fact that it is highly unlikely it has been stolen by an experienced hacker.
First of download the latest version of Virtualbox. Create a virtual machine (the defaults should be fine) you may want to choose 1gb of ram if you can spare it, it just gives you that edge. Mount the disk image for the linux distro of your choice and install it.
At this point you need to set up your own server environment. There are lots of guides out there, so I won't go through it here. Also it would be a good idea to make sure that you have ssh installed on the virtual machine too.
sudo apt-get install ssh
Next we need to configure virtualbox to allow you to ssh into, it doesn't do this by default. This is simple enough. Power off your virtual machine, then go to settings and choose 'Network'. Here you should already have Adapter 1 enabled, choose Adapter 2 and enable it. The 'Attached to' part should be 'Host-only Adapter' and the rest please see the below pitcure:

Once you have done this fire up your virtual machine again as we need to do a little bit of configuration to get the Network fired up. First off type in terminal on your mac.
ifconfig
You should see an ipaddress for your virtual machine next to vboxnet0. For me the ip address of my virtual machine was set to 192.168.56.10.
Now we need to make sure the network will work to do this all you need to do is edit /etc/network/interfaces on your virtual machine to add the eth1 configuration.
This is a simple as adding the following lines based on our vboxnet0 is configured to 192.168.56.10.
auto eth1
iface eth1 inet static
address 192.168.56.10
netmask 255.255.255.0Save this and then run the following to fire up eth1:
sudo ifup eth1
Now you should be able to drop back into terminal on your mac and ssh into your vm
ssh mat@192.168.56.10

That is really all you need to be able to develop through your virtual machine. You should be able to type in the ipaddress of your virtual machine into your browser to pick up the default index.html of your apache on the virtual machine.
The host should be able to pick up your vhosts by settings /etc/hosts on your host machine to something like the follwowing:
test.local 192.168.56.10
Then when you type test.local into the address bar on your browser you should be redirected to the site that you set in the vhosts in your virtual machine. If it all works, you should also notice how much quicker it is. The beauty of having a dedicated server environment for your local development. You could set up different machines with different versions of php, easily swappable to develop locally from, all without having to fiddle with the backend of your host machine and break anything.
The next thing, which took me a long time to figure out but actually is fairly easy, is to sshfs to your virtual machine from your host and mount it so that it is like a disk that is mounted to your host and you can navigate and share files with it easily.
You need to install Macfusion for this to work, on top of that you also need to make sure you have Macfuse installed with the latest binaries. I use snow leopard and found the following link had everything I needed to get it to work:
http://blog.philippklaus.de/2011/04/fix-sshfs-with-macfuse-on-mac-os-x-10-6-7-snow-leopard/
Once you have both of these installed do the following, obviously changing out the ipaddress to what yours is set to:
cd ~
mkdir Mounted_VM
This will simply create the directory that you want to mount your VM on, then:
cd /Applications/Macfusion.app/Contents/PlugIns/sshfs.mfplugin/Contents/Resources/
This should get you into the correct directory to run the following command
./sshfs-static mat@192.168.56.10:/ ~/Mounted_VM
You should now see that when you open finder and go to the directory 'Mounted_VM' you will be able to navigate through your entire virtual machine.
Job done, hope you find this guide useful and you have some success with getting a Virtual Machine set up correctly to do local development through. In my humble opinion it is the best way to go. Any questions or if I am missing anything, contact me and I will try to help and post any edits on this guide.
Reseach and Resources:
http://blog.philippklaus.de/2011/04/fix-sshfs-with-macfuse-on-mac-os-x-10-6-7-snow-leopard/
http://crackingdrupal.com/blog/greggles/easier-and-safer-drupal-development-virtualbox-virtualization
http://muffinresearch.co.uk/archives/2010/02/08/howto-ssh-into-virtualbox-3-linux-guests/





Comments
VirtualBox is the way to go
VirtualBox is the way to go :-)
I found a useful command recently:
$ vboxmanage guestproperty enumerate NAME_OF_VIRTUAL_MACHINE
It gives all the details including the IP address of the virtual machine. Very useful :-)
Hi Adnan
Hey Adnan :-)
Thanks, this is brilliant. A really helpful command that speeds things up even more.
:-)