Setting up Ubuntu Server in VirtualBox for a headless LAMP server in Mac OS X Lion

The title may be a bit of a mouthful, but it explains the purpose of this post. Similar to my previous post, this guide serves as a reference for myself but I figured others might also find it useful.

First the why. For a long time I have used MAMP Pro to test sites locally on my Mac, and truth be told it’s done pretty well. I was, and to a degree still am, a server n00b. It’s only in the past few months, administering my Linode, that I’ve learnt where and what to edit to get things to work. And with this new found control, it became increasingly obvious that the differences between Mac and Linux were starting to cause issues.

Installing memcached, mongo and other systems became a chore on Mac, requiring use of homebrew or other bad implementations of package managers. Botching the built in Apache caused more issues than it solved and testing Nginx became a nightmare when building in new modules.

Then I remembered VirtualBox. A powerful virtualization product, I’d been using it for years to test websites in Windows. Why not run Ubuntu Server, the same OS as my actual Linode server, in VirtualBox? Even better, why not run it invisibly in the background? Imagine it, boot up VirtualBox and you’ve got a complete replica of your production server. Upgrade to a new machine and take it with you!

So, on to the guide. For reference, the versions used are:

  • Mac OS X 10.7
  • Ubuntu Server 11.04
  • VirtualBox 4.0.12

Step 1) Download VirtualBox and Ubuntu Server

According to some reports, older versions of VirtualBox had issues with 64-bit OSs. This doesn’t seem to be an issue anymore from what I can tell.

Step 2) Create a new Virtual Machine in VirtualBox

  1. Open VirtualBox
  2. Click on Machine > New…
  3. Enter a Logical Name. Or not. I went with Morlock and called my Mac Eloi.
  4. Try and match the memory size to your production target. I’m running a Linode 512, so went for 512Mb of memory.
  5. Create a new hard disk. Again, try to match your production environment.
  6. Launch your new Virtual Machine. On first run you will be asked to provide a boot image, choose the Ubuntu Server image you downloaded above.
  7. Proceed to install Ubuntu as you would on your server. Ubuntu provides a “virtualised specific” boot option, however I found it caused more trouble than it was worth.
  8. VirtualBox should now boot into your Ubuntu Server! Now’s a good time to do the dirty post-install stuff.
  9. When you’re done, shutdown the Virtual Machine “sudo shutdown -h now”
  10. In a Terminal window in OS X, enter “VBoxManage startvm ‘My Image Name’ –type headless
  11. Ubuntu will now boot up in the background! You can SSH in via Terminal and administer it! To shutdown, just SSH in and issue the same command as in #9.

Step 3) Making it usable

The last thing is to tie it all together. In Terminal (on OS X), edit /etc/hosts and add your dev domain names e.g.

192.168.1.123    mysite.dev
192.168.1.123    anothersite.dev

Now when you visit mysite.dev now in your browser, the request will automatically send your request to the Virtual Machine. (You will need to set up Apache/Nginx on Ubuntu to handle these requests however).

The final thing is to add your sites as a shared folder within Ubuntu. Shutdown the Virtual Machine if it’s running and open Virtual Box. On the Settings for your Virtual Machine, add a new Shared Folder keeping a note of its share name.

Boot your machine once more. You will need to install the Guest Additions to access the Shared Folder which is a bit tricky on Ubuntu Server. Finally, create the mount point and mount the folder.

sudo mkdir /mnt/sharename
sudo mount -t vboxsf ShareName /mnt/sharename

You will now have a working share making developing sites much easier.

If you’ve got any tips or suggestions, please leave a comment below!