searbe

    • Edit
    • Delete
    • Tags
    • Autopost

    Windows 7 Lamp - Ubuntu / VirtualBox PHP Development Environment

    NOTICE: I have made a more up-to-date tutorial. If you prefer, you can still follow this post. However I'd recommend you give my new setup a try - you'll find it at The Perfect Windows 7 LAMP using Ubuntu and Virtualbox. This post tells you to mount in a nasty way and uses virtualbox's Shared Folders feature which I find problematic.

    A few steps on getting yourself set up with an Ubuntu LAMP running in VirtualBox, sharing your internet connection in a way which keeps it portable (so it'll work on your laptop regardless of what network you're on) but also two-way (so you can see your virtual machine on the network and your vm can see you). It involves creating a host-only network and is quite easy once you've jumped through the hoops.

    Install VirtualBox

    Head on over to virtualbox.org, download and install. Then create a new Virtual Machine and set up your Ubuntu LAMP.

    Get an Ubuntu LAMP set up

    If you're not familiar with VirtualBox, you need to create a new machine. VirtualBox will take you through a wizard which you should be able to figure out. Head over to Ubuntu and grab yourself an ISO. Once you have the an Linux desktop sitting in front of you, get your server set up. Check out my Ubuntu LAMP guide if you don't know how.

    Add VirtualBox Shared Folders

    Power your virtual machine off and open VirtualBox. Click the VM (your virtual machine, in my case I called it 'ubuntu'), click Settings then Shared Folders. Add a shared folder from your Windows drive. This will contain all of the stuff you want to share with your Linux box.

    You can do multiple shares if you like. I prefer to have a single shared folder and treat it like a drop-box for everything.

    Install VirtualBox Guest Additions

    Start up your VM. You will need to install Guest Additions if you haven't already. Click 'Devices' at the top of your VM then click 'install guest additions'. This will mount a CD, in which there is a .sh shell script which you should execute (run "./scriptname.sh" in terminal).

    Mount your Shared Directories in the Ubuntu Virtual Machine

    In your running VM, make a directory somewhere. I use /development (mkdir /development). This is where the contents of the shared folder you created earlier will appear.

    To mount the shared folder:

    sudo mount -t vboxsf development /development

    With "development" being name you specified when you created the shared folder in VirtualBox and "/development" being the directory you just created in the Virtual Machine.

    It's a little annoying having to run this in the terminal each time the VM boots up - so what I did is stick it at the end of /etc/rc.local, just before exit 0 (rc.local is a script that's run just at the end of startup).

    Before that works, you'll want to edit /etc/sudoers (sudo nano /etc/sudoers) and add a line like this:

    %craig ALL = NOPASSWD: ALL

    (Change 'craig' to your user's group)

    Configure VirtualBox Port Forwarding

    Great, nearly there. You have a LAMP setup, you have a shared folder without any SAMBA rubbish, but you can't access your LAMP from Windows!

    Don't go and set up a bridged network - there's a better solution; VirtualBox's port forwarding. You want to make every request to port 80 of your PC forward to the VirtualBox guest.

    Open a command prompt in Windows and change directory to wherever VirtualBox is installed - in my case, C:\Program Files\Sun\VirtualBox.

    Take note of what your VM is called. Mine is called Ubuntu. If it has a space in the name, you'll need to wrap it in "double quotes" in the following commands.

    My Virtual Machine uses a pcnet adapter - others talk about e1000 - you may need to tweak as necesarry, but unfortunately I don't know the abbreviated names for the other adapters. Let's just presume your Virtual Machine uses a pcnet adapter.

    Run this to have every request to port 80 on your host (Windows 7) forward to port 80 on your guest (the Ubuntu VM).

    VBoxManage.exe setextradata Ubuntu "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/HostPort" 80 
    VBoxManage.exe setextradata Ubuntu "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/GuestPort" 80
     VBoxManage.exe setextradata Ubuntu "VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/Protocol" TCP

    Run the following and the extra data you just set should be spat back at you, just to confirm the settings are all in there okay:

    VBoxManage.exe getextradata Ubuntu enumerate

    Configure VirtualBox Host-Only Adapter

    Now we need to add a second network adapter so we have a permanent IP address to your Windows host. Power your Ubuntu VM down, edit the network adapters and enable the second adapter. Choose 'Attached to: host-only adapter'.

    Now you have an "internal networ" between Ubuntu and Windows. In VirtualBox, go to File->Settings->Network and click the configure icon for the host-only adapter. You'll see an IP address - default is 192.168.56.1. This is your Windows PC's "internal network" IP - so anywhere you need a permanent handle to your host, use this IP.

    Now you have a Windows 7 irrtitance (bug?) to fix: go to Run, and enter secpol.msc. Click Network List Manager Policies, then double click Unidentified Networks, and check the circle to have unidentified networks always classed as private. This will make unidentified networks use your private firewall rules, which will likely be a be more relaxed ruleset. If you don't do this, you might have trouble getting Ubuntu to talk to Windows 7 on this "internal network".

    Configure XDebug

    Okay now for XDebug. Jump back in to your Ubuntu VM and ensure your xdebug config file (probably at /etc/php5/apache/conf.d/xdebug.ini) has this:

    xdebug.remote_enable=on 
    xdebug.remote_handler=dbgp 
    xdebug.remote_host=[your HOST machines permanent IP on the host-only adapter, 192.168.56.1 in my case] 
    xdebug.remote_port=9000 
    xdebug.idekey=netbeans-xdebug

    IMPORTANT It seems that somehow this setup will now *ONLY* work if you have an xdebug session running, otherwise you get an infinite hang where it seems XDebug is trying to connect to a client (?!). So comment out the first line by putting a semi-colon in front of it:

    ;xdebug.remote_enable=on

    And only un-comment it when you want to run a debug session. Not great but I can't find another solution :-( Please comment if you know of a better way.

    Finished!

    Right - now restart VirtualBox and, hopefully, you will be all set up with a Linux development machine inside a Windows development environment!

    • 10 December 2010
    • Views
    • Permalink
    • Favorited 0 Times

    Comments 1 Comment

    Apr 27, 2011
    Alex said...
    That's great! Thanks!!

    Leave a Comment

  • Craig Bendell's Posterous

    PHP developer living in Worthing, UK working for D3R. Starting Android / Java development for fun.

  • About Craig Bendell

    PHP developer living in Worthing, UK working for D3R. Starting Android / Java development for fun.

  • Subscribe

    Subscribe to this posterous
    Unsubscribe
    Subscribe via RSS
    You're a contributor here (Edit)
    This is your Space (Edit)
    Follow by email »
    Get the latest updates in your email box automatically.
    Follow on Twitter
  • Follow Me