Creating a virtual development server using Virtualbox

For those of you who read Installing Fedora 10 on Windows XP using VirtualBox, this is another post along the same lines as that one, except this one is for configuring your virtual machine as a development server for when you are forced to code on your Windows host.

f10_in_vb

I am a bit of a snob when it comes to development, I simply refuse to develop on a windows host as I just think that the app will end up being deployed on a nix system, so it should be developed on a nix system. I also think using a windows host for development takes away from a lot of the raw development involved with using nix systems. And, with the amount of quality development and virtualization tools available to us these days, we can develop on anything, using any tool.

If you haven’t set up a virtual machine using Virtualbox and your choice of Linux OS on a Windows machine, then read through my other post before continuing with this one. Also note, you will need to have Apache configured and running on your virtual machine.

Now, what I am going to do is configure VirtualBox to accept connections to itself on port 80 and port 22, so we can develop on one machine (Windows) but using a Linux server.

Configure the network

The first thing we need to do is get our machine on our local network and a proper IP address for it. Open up the settings for your virtual machine and click on the ‘network’ tab, then select ‘Adapter 2’ and change ‘Attached To’ to ‘Host Interface’. This will allow your virtual machine to get a DHCP assigned IP address and actually put it on the network.

vm_network

Save your changes and boot your VM.

Note: In later versions of VirtualBox (ie, 2.2.4) your Network adapter screen may look like the following, in which case just copy the settings I have used here.

Alternate VM Network

Configuring Apache / Port 80

Now we need to make sure our Guest Linux OS can accept connections on port 80. Fedora 10 blocks these connections by default, to open them, edit the file /etc/sysconfig/iptables using vim, emacs, gedit, pico, whatever. Find the line accepting port 22 connections that looks like this;

-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT

And add a line below it like this;

-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT

Now restart iptables;

$ service iptables restart

Forwarding port 80

The next step is to forward your port 80 to your virtual machine. You can forward any port you like, for example, you might have a web server on your Host machine and not want to forward port 80, so you can choose port 8080. I want a seamless virtual server environment, so I chose port 80.

Open up a command prompt (Start > Run > ‘cmd’ + Enter) and change to your Virtual Box directory;

cd C:\Program Files\Sun\xVM VirtualBox

Now we will get aquainted with the command line tool, ‘VBoxManage.exe’.

To forward port 80 on your host to port 80 on your guest, type the following (make sure you substitute “Fedora 10” with your VM’s name);
NOTE: Leo in the comments has recommended changing ‘pcnet’ to ‘e1000′ for Intel network cards!

VBoxManage.exe setextradata "Fedora 10" VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/HostPort 80

VBoxManage.exe setextradata "Fedora 10" VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/GuestPort 80

VBoxManage.exe setextradata "Fedora 10" VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/Protocol TCP

Then we can view our changes using the following command;

VBoxManage.exe getextradata "Fedora 10" enumerate

vbmoxmana_port80

Browsing to your virtual server

At this stage you should be able to browse to your virtual server using any of the name based or home based virtual hosts on your vm.
ie, browsing to localhost on your host machine should display the default virtual host on your guest.

However, if you have name based virtual hosts on your guest (as I do) then you need to add those names to your windows ‘hosts’ file so it knows to look for them locally and not on the internet. This is very easy to do.

Open the file c:\Windows\System32\drivers\etc\hosts and add the names of all your name based hosts after ‘localhost’ on the line starting with 127.0.0.1, eg;

127.0.0.1         localhost host1 host2 mysite

Then, after saving, these hosts on your guest should load in your browser from the guest machine.

Setup ssh server

If you want to edit the files on your guest from your host using SFTP through an IDE (like Netbeans, Zend Studio, Eclipse) or an app like WinSCP, then keep reading.

Having an SFTP connection to a server is a great way to integrate your remote files into your local development environment and makes editing much easier and faster. SFTP is FTP over SSH and requires only an SSH connection to the server to work. We need to make sure ssh is running on our Guest;

$ service sshd start

Now, we want to make sure it starts when the machine starts;

chkconfig sshd on

We now need to follow the same procedure to forward the host port 22 to the guest port 22 as we did for port 80 above;
NOTE: Leo in the comments has recommended changing ‘pcnet’ to ‘e1000′ for Intel network cards!

VBoxManage.exe setextradata "Fedora 10" VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort 22

VBoxManage.exe setextradata "Fedora 10" VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort 22

VBoxManage.exe setextradata "Fedora 10" VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol TCP

Now you will be able to ssh to the guest from your host. You can test this in putty (or anything you’d like) by connecting to 127.0.0.1 on port 22 and logging into your guest machine with your normal user details.

Thats it. You now have a fully integrated virtual server for development. If you have any questions, suggestions, problems or concerns, please let me know in the comments.

  • nhb
    I've got the host as Ubuntu and the guest as XP with Apache running in XP. How do I set that up?

    Thanks, this is the kind of info I'm looking for, but the other way round.
  • cbiggins
    Hi nhb,
    It should be very similar, I recently did it again using Mac OS X (host) and Kubuntu (Guest) and it was the same. Just find the app 'vboxmanage' and the commands should be identical.
    Let me know if you need any other help.
  • nhb
    Ok I got as far as the bridged adaptor, then went to modify the iptables files, but I can't find the sysconfig folder. I'm running Ubuntu 8.10, which appears to be different to Fedora. I've been using Windows for the last 15 years or so and have only started with Ubuntu in the last couple of months, so its all new to me.
  • cbiggins
    Ahh yes, Ubuntu recently changed to ufw from iptables because iptables config scares people. Check this link; https://help.ubuntu.com/8.04/serverguide/C/fire...
  • nhb
    I give up. I've had a read of that article, and I'm lost to be honest.
  • cbiggins
    You should just need to either disable ufw;
    sudo ufw disable

    Or, if you want it to run, allow connections from ports 22 and 80..
    sudo ufw allow 22
    sudo ufw allow 80

    And thats it.
  • nhb
    Ok I did that, went through the rest of your set up using VBoxManage and now the virtual machine won't start up at all. Configuration for host port failed. How do I set the values back so that it will run?
  • cbiggins
    To return it to default values, just set it to nothing;
    VBoxManage.exe setextradata "Fedora 10" VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort
  • nhb
    Well that didn't work. I now have a virtual machine that took time to set up with the server and applications, that won't run. Thanks for your help.
  • excellent helped very much thanks!
  • Your welcome Rich, glad it helped! :)
  • Spot on - been looking for this info all afternoon after spending hours fiddling with changing the network adapter etc etc... cheers :-)
  • Nice comment Moggen, Thanks!
  • Moggen
    One thing to keep in mind with this setup is that you will get problems if your network connection goes down. For example if you use a laptop computer and want to work with the virtual machine while disconnected from the wireless network.
    A solution to this is to install the Microsoft Loopback adapter, connect it as another virtual network adapter in the virtual machine and set up a local network to communicate between Windows and Fedora. This will work even if the wireless net is down/disabled.

    Info about installing the loopback in XP: http://support.microsoft.com/kb/839013
  • Thanks for that Leo, I added that to the post.
  • Leo
    If your network card is INTEL, change 'pcnet' with 'e1000'. Example:

    VBoxManage.exe setextradata “Fedora 10″ VBoxInternal/Devices/e1000/0/LUN#0/Config/ssh/HostPort 22

    Good luck!
blog comments powered by Disqus