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!

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

  1. Joe Turner says:

    Have you tried vagrant? Means you can use the files on your mac, yet run them through your VM…clever bit of kit

  2. Peter says:

    I have nearly the exact same setup as you’re using, but have not been successful. The Ubuntu install works fine, but I can never subsequently boot into the virtual machine. What type of file are you using for a virtual disk? VDI? VMDK?, VHD?

    -Peter

  3. Corey says:

    I’ve installed Ubuntu onto my Virtual Machines, installed Guest Additions, changed to a Static IP, and made numerous post-installation updates to the guest OS.

    Now I’m just stuck on Step 8 Part 2: Copy your SSH public key over ( ~/.ssh/id_rsa.pub | pbcopy -> ~/.ssh/authorized_keys)

    What is that asking me to do? I’m a bit of a noob here. I understand the other instructions but don’t exactly know what to do with this one.

    • Luke L says:

      Hi Corey, when you want to connect to the virtual machine from your local machine you will do so over SSH. In order for it to not require you to enter your password each time, you can give it your SSH keys. Googling “set-up SSH passwordless login” should explain more.

  4. Hello, I do appreciate I’m bit late with my comment, but I was reading this alternative to MAMP Pro that creates more troubles than benefit.

    Even in my case I don’t understand point number 8. Well, I got what is supposed to do, but it is not clear where I should do this operation. I guess on the Ubuntu terminal, but at the time of writing I am still in the process of reading.

    Any hint, would be appreciated.

  5. Chris says:

    Dude, this tutorial is totally useless! Where the hell do you get this IP 192.168.1.123 from ? Why is it .123 and not .77 or .33 ?

    • Luke L says:

      It’s a static IP, so it’s up to you to decide what IP to use. Otherwise we’d just use DHCP and let the router choose each time.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>