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.
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.
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.
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
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.
