Using VirtualBox as a local development server, a tutorial.
Having a local development server makes testing new web code, sites, etc much faster (and cheaper!) then using hosting somewhere. There are a lot of applications that bundle/install a LAMP-like service onto your computer, but then you don’t learn all the fun and tricky things it takes to administer your own web server, as well as enjoy full control.
This was my thinking, so I set about to use VirtualBox to create a virtual machine, and use that virtual machine as my web development server. It doesn’t take long to figure out on your own, but to save anyone the time, I thought I’d log my findings in the form of a tutorial.
First, you want to make your virtual machine in VirtualBox. I used Ubuntu 9.10 Server Edition as my OS. Once it is installed and configured, we can start making the changes to access what your virtual machines apache is hosting.
When you install VirtualBox to your computer, it has a home directory somewhere. In a linux environment, that resides at:
~/.VirtualBox/
The file we need to configure is an xml file with the same name as the virtual machine you created:
~/.VirtualBox/Machines/<vmname>/<vmname>.xml
In the <ExtraData>, we need to add the port forwarding. By this I mean, when you go to localhost:8080 on your browser, it will connect to what your virtual machine is hosting. The commands are very simple.
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/web/HostPort" value="8080"/> <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/web/GuestPort" value="80"/> <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/web/Protocol" value="TCP"/>
In English, this is saying, “When the host machine looks at port 8080, send it to the virtual machines port 80, and this is using the TCP protocol.” When you point your browser to localhost:8080, it should now show you what your virtual machine is hosting.
I also wanted to easily access the virtual machine, so I added the ability to ssh to it.
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" value="2222"/> <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" value="22"/> <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" value="TCP"/>
So, when at my console I enter: ssh -l
And that is it!