I’ve just spent today making myself feel incredibly bad for switching back to Windows, but of course, I still need Linux for my web development stuff – you can’t beat a good LAMP and without it, Windows 7 can’t be my primary operating system. Sorry, but PHP on Windows 7 still doesn’t compare to PHP on Linux!
Install Virtualbox
So my install is Windows 7 with an Ubuntu virtual server using VirtualBox. All of my work sits on my windows partition – so native Windows apps aren’t fiddling around with files on a VM – and Ubuntu takes all requests and serves up pages from the windows partition. First step: Download and install VirtualBox.
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 yourself – you don’t need much in the way of disc space etc. Head over to Ubuntu and grab yourself an ISO if you haven’t already. Once you have the an Ubuntu (or whatever other distro) desktop sitting in front of you, you might want to follow my lamp guide to get a few bits installed – namely Apache, PHP, MySQL and XDebug.
Add VirtualBox Shared Folders
Once it’s set up, power your virtual machine off and open VirtualBox. Click the VM (your virtual machine, in my case I called it ‘ubuntu’) then click Settings and click ‘Shared Folders’. Add a shared folder from your Windows drive – this will contain all of your shared stuff. You can do multiple shares if you like – personally, I like to just have a single shared folder and treat it more like a drop-box than have to configure multiple shares.
Install VirtualBox Guest Additions
Once that’s done, start up your VM. You will need to install Guest Additions if you haven’t already. You can do this by clicking ‘Devices’ at the top of your running VM then clicking ‘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
Now, in your running VM, make a directory somewhere. I decided to be a bit messy and just create /development (mkdir /development). This is where the contents of your shared folder will go when you mount it.
The command to mount the shared folder is sudo mount -t vboxsf development /development – where “development” is the name you specified when you created the shared folder in the first place in VirtualBox, and “/development” is 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, so an ideal place for you to shove stuff like this in.
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!
One Comment on "Windows 7 Lamp – Use Ubuntu and VirtualBox for a Great PHP Development Environment"
Trackbacks
Story added... Your story was featured in virtualizationtimes.com! Here is the link to vote it up and promote it: http://www.virtualizationtimes.com/node/1673...