Build Your Own Dev Server with VirtualBox

Sun’s VirtualBox is ideal for testing different desktop environments (for example, browser testing), but I’ve discovered it’s also great for running a test server environment. Instead of setting up Apache, PHP, and MySQL right on your desktop machine, you can place them in a virtual Linux server. That way there’s no interference with your desktop, and you can ensure that your development environment is as close as possible to your eventual deployment environment.

With a virtual Linux server running inside your desktop operating system, you can SSH into it, upload files to it, load web pages from it–whatever you’d do with a real live server. And all the software you need is free and simple to configure. Let’s make a start!

Setting Up Shop

The first step is to download the VirtualBox client. Pick the version appropriate for your host system.

You’ll also want to grab a disk image for your Linux server. For this tutorial, I’ll be using the 64-bit version of Ubuntu server 9.04, but feel free to use whatever distribution you’re more comfortable with. Of course, you might need to adapt some of the instructions to your particular setup.

We could also use a desktop build, but since we’re only interested in the server functionality, it’s best to stick with a server build: we’ll save on memory because no graphical desktop interface is loaded.

Installing the Ubuntu Server

Start up VirtualBox and click New for a new virtual machine. Step through the wizard, making sure to choose the 64-bit version of Ubuntu (if that’s the disk image you downloaded). I used the defaults for every other option: RAM, disk size, disk type, and so on.

Now select your new VM and click Start. VirtualBox will ask you how to install the OS on your virtual machine. Since we downloaded an .iso, choose CD/DVD-ROM device from the Media Type menu and Image File from the Media Source menu, selecting your Ubuntu Server iso. As the system boots, you’ll be presented with Ubuntu’s installer. Choose your language, and then select Install Ubuntu Server.

Follow the on-screen instructions to install the server. Notice that when you come to partitioning your hard disk, the virtual machine only “sees” the disk image you created before. Feel free to use the whole disk. Later on in the process, the installer will prompt you to install additional software. For our purposes, we’ll install the LAMP server and OpenSSH server packages. This way we have everything we need for a fully functional web server out of the box.

When it comes time to reboot your new server, you can “eject” the installation CD by choosing Devices>Unmount CD/DVD-ROM from the VirtualBox menu.

Log into your new system with the username and password you chose during installation. It’s also a good idea to upgrade your system with:

sudo aptitude update
sudo aptitude safe-upgrade

Accessing the Virtual Server from the Host System

Now that our server is up and running, we want to be able to access it from our host system. We’ll set it up so we can SSH to it, transfer files to it via SFTP, and make HTTP requests to Apache.

To do all this we need to edit the xml configuration file for our virtual machine:

  • On a Mac, the file is found at ~/Library/VirtualBox/Machines/<machine name>/<machine name>.xml
  • On Windows, it’s inside the .VirtualBox/Machines subdirectory in your home folder.

So for my machine, which I’ve called “Ubuntu Server,” I’m editing Machines/Ubuntu Server/Ubuntu Server.xml

At the top of the file you should see an <ExtraData> tag. Inside that tag, copy in the following tags:

<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" value="2222"/>
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" value="22"/>
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" value="TCP"/>
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/HostPort" value="8888"/>
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/GuestPort" value="80"/>
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/Protocol" value="TCP"/>

These lines configure VirtualBox to forward requests to specific ports on the host system onto other specified ports on the guest system. For SSH, we’re forwarding port 2222 of the host system to port 22 of the guest system (where OpenSSH is listening). The same principle applies to the Apache configuration items, with port 8888 on the host mapping to port 80 on the guest.

With that done, save the xml file and restart your virtual machine.

If the machine fails to start, it’s likely to be because of a network interface configuration problem. In the lines we added, we specified pcnet as the network interface. To ensure that’s what your virtual machine is using, right-click on it in the main VirtualBox window and click Settings. In the Network tab, select one of the PCnet adapters from the Adapter Type drop-down. You should be able to restart your virtual machine with no problems now.

Now if you open a browser on your host system and point it to http://localhost:8888/ you should see the default Apache “It works!” page. Great!

Similarly, to SSH into your new server, SSH to port 2222 on localhost with the username you set during the Ubuntu server installation. (If you’re on Windows, you can use the PuTTY SSH client to perform the same function):

ssh -l <username> -p 2222 localhost 

You’ll receive the usual “unknown host” security warning; type “yes” to connect and you’ll be prompted for your password. Upon entering it, you should be logged in to your server! Feel free to look around and make yourself at home.

While we’re still logged in, let’s do one more task: by default the Apache web root in Ubuntu Server is /var/www/, which your default user won’t have write permissions for. Let’s change that, so you can upload files to your web root with SFTP. Enter this command and hit return:

sudo chown <username> /var/www

To connect to your server with FTP, no extra configuration is necessary. OpenSSH gives you “free” FTP via the SFTP (SSH FTP) protocol. Most clients (FileZilla, for example) support it; just choose SFTP as the protocol, localhost as the server with port 2222, and your Ubuntu username and password. Choose /var/www/ as the default directory, and you should be able to transfer files to and from your server.

Let’s test that everything is working: create a php file named info.php containing the usual phpinfo call:

<?php
  phpinfo();
?>

Use your FTP client to upload that file to your server’s /var/www/ folder. Now point your browser to http://localhost:8888/info.php, and you’ll see the PHP info page. The System row at the top of the table will tell you PHP is running on Ubuntu.

There you have it! You can test server configurations, brush up on your sysadmin skills, and develop your web sites and applications in a full Linux server environment running inside your usual desktop.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • LFA

    Fan – freaking – tastic!
    Spot on tutorial.
    Thanks!
    //Fredrik

  • boriscy

    I use LinuxMint as my main operating system for development and as the Host for VirtualBox, Ruby, Python, Apache, PHP, MySQL, PostgreSQL and almost any open source software runs faster on Linux than in Windows. I use virtualize my Windows Session which works fine and Fast.

  • Jeremy

    Great tutorial. Configuring the ports in the XML file is what I needed. I had already figured out the rest but gave up at this point. Thanks.

  • http://www.optimalworks.net/ Craig Buckler

    Nice idea, although FTP-ing files to the VM before testing will get a little tedious. You should be able to set up a VirtualBox shared folder which matches your Windows/Mac folder against your VM’s Apache DocumentRoot.

  • carstep

    @Craig
    I did the same and it works just fine and I would recommend to do so

    r. Sandor

  • gvlx

    If you don’t want to change the settings file directly, you can also have the network connection on the Virtual Machine on Bridge Mode.

    In Bridge Mode, the network interface acts as if you had another network card in your computer, so it gets an IP address within the same network your computer is in and the VM becomes accessible to all computers there.

    If you are just testing with your own computer, the Host mode could be simpler: in this mode VirtualBox creates a virtual network switch within your computer (with its own DHCP server if you like) so that only you, on your computer, can access the VM.

    On another point of interest, Microsoft offers the Windows Server on trial for 180 days if you need to test on those systems (search microsoft.com for Windows Server Trial Software Downloads and register for the trial and download the cd-rom iso files).

  • BrianH

    Very cool – I’ve used a lot of VMWare for this purpose, but it always seemed so bulky. I followed your tutorial, and this looks really great! VirtualBox runs pretty light on my system. Thanks for the great post!

  • peppolypus

    I signed-in just to say: Thank You So Much!! I was tired to have working sites on my local env that went crashing on the server! Now I can test them as it’s meant to be! Thanks again!

  • Mike

    even better download one of these and install a complete dev server

  • lilrichieh

    Trying to set this up on a Imac but i keep getting an error. Tried to set the network card to something else but didnt make a diffrence.

    Will try something else i suppose

  • Louis Simoneau

    @boriscy: My main OS at home is Ubuntu Linux as well, but I can still see a benefit to running a virtual server inside it: I don’t need Apache loading up every time I boot my desktop, and sometimes I want to try out different server configurations (e.g. Nginx).

  • Louis Simoneau

    @Craig: yeah, that is indeed a much better idea than FTPing…I guess I just wanted to show that you can make it “just like” a real server. But if you need to transfer large numbers of files then a shared folder will be faster and easier.

  • http://preproductiontools.com Imagebox

    Got this running easily on my macbook pro, but I am having trouble adding the XML.
    In the “Machines/Ubuntu Server/Ubuntu Server.xml” file I do not have an “<ExternalData>” tag. I have an “<ExtraData>” tag which I tried adding the code snippet to, but it I get an error when i start the VM that way.
    Any ideas as to where I should insert the XML code would be greatly appreciated?

  • lilrichieh

    @Imagebox same happens for me.

  • Louis Simoneau

    Sorry guys! It is in fact <ExtraData>, not <ExternalData>…I’ve updated the post.

    What error message are you getting?

  • Dave

    VMWare also has free virtualization software that is much easier to use than VirtualBox. A single directory contains the entire VM which means you can copy it, move it, back it up etc.

    If you’re not up for installing the server yourself it is pretty simple to download VMWare Player, grab one of the linux web development machines available from the appliances store and you’re good to go.

    Having used both of them extensively over the past year I would stick with VMWare as my prefered choice for awhile yet.

  • Shannon

    I’m having the same problem as ImageBox.

  • Mike

    or try an entire dev environment, use one of these iso files and get a full Ubuntu install with everything ready to go http://www.turnkeylinux.com/

  • http://preproductiontools.com Imagebox

    Here is the Error…
    Configuration error: Failed to get “MAC” value
    (VERR_CFGM_VALUE_NOT_FOUND).

    Unknown error creating VM (VERR_CFGM_VALUE_NOT_FOUND)

    Details:
    Result Code:
    NS_ERROR_FAILURE (0×80004005)
    Component:
    Console
    Interface:
    IConsole {0a51994b-cbc6-4686-94eb-d4e4023280e2}
     
    and the xml…

    <ExtraData>
    <ExtraDataItem name=”VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort” value=”2222″/>
    <ExtraDataItem name=”VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort” value=”22″/>
    <ExtraDataItem name=”VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol” value=”TCP”/>
    <ExtraDataItem name=”VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/HostPort” value=”8888″/>
    <ExtraDataItem name=”VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/GuestPort” value=”80″/>
    <ExtraDataItem name=”VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/Protocol” value=”TCP”/>
    <ExtraDataItem name=”GUI/LastWindowPostion” value=”400,208,720,422″/>
    <ExtraDataItem name=”GUI/Fullscreen” value=”off”/>
    <ExtraDataItem name=”GUI/Seamless” value=”off”/>
    <ExtraDataItem name=”GUI/AutoresizeGuest” value=”on”/>
    <ExtraDataItem name=”GUI/MiniToolBarAutoHide” value=”on”/>
    <ExtraDataItem name=”GUI/LastCloseAction” value=”shutdown”/>
    </ExtraData>

  • Louis Simoneau

    @Imagebox
    I only get that error if the network interface defined in the forwarding instructions is different from the one set in the network settings.

    Look further down in the XML file, in the <Network> section. One of the Adapter tags should have enabled set to “true”: its “type” attribute will match what you select in the Network tab in the VirtualBox GUI. If setting that to the PCNet value doesn’t work, try switching back to Intel: change the type attribue of the Adapter tag to 82540EM (or choose the Intel PRO/1000 MT Desktop in the GUI), and change the ExtraDataItem values to /e1000/ instead of /pcnet/. So, for example, you’d have:

    <ExtraDataItem name="VBoxInternal/Devices/e1000/0/LUN#0/Config/ssh/Protocol" value="TCP">

    As long as your port forwarding instructions refer to an enabled network adapter, your VM should boot fine. Let me know if this works for you.

  • Daniel Wamara

    I did all that but when I restarted the VM, I keep getting a “Kernel panic – not syncing: VFS : Unable to mount root fs on unknown block (0,0)”. Do you know how I can solve this?

    D.

  • http://preproductiontools.com Imagebox

    @ Louis Simoneau

    Changing the Network Adapter setting in the VirtualBox GUI worked
     
    Thanks very much for your help.

  • lilrichieh

    I get the following error;

    Result Code:
    NS_ERROR_FAILURE (0×80004005)
    Component:
    Console
    Interface:
    IConsole {0a51994b-cbc6-4686-94eb-d4e4023280e2}

  • http://www.modernscribe.com GeneralBill

    Instead of forwarding port 2222 to 22 and 8888 to 80, I suggest mapping 22 to 22 and 80 to 80. That makes http://localhost/ work.

  • guruguy

    Great post, I already have a similar setup. I agree with Craig’s post saying that FTP is too tedious. Set up a share folder using Virtual box’s inbuilt file sharing feature. However if you have encoding troubles with various files (as I did) move on to a normal network share. This is a great idea that saved me buying a development server. The only downside is that it takes a while to load windows and then a VM when you just want to test something quickly, hopefully an SSD would fix that

  • Louis Simoneau

    @Mike: I was unaware of Turnkey Linux, looks like a great solution for doing this kind of thing…and the disk images are significantly smaller than the standard Ubuntu Server iso…thanks!

  • someTimesVarable

    A wonderful post and an answer to my inflated electricity bill. I have been using an old linux box networked to my pc, but this is a great moneysaver.

    One question though, can anyone suggest the configuration for using Webmin with VirtualBox. I’m a bit stumped on this one and would like to use it to simulate my original setup.

    Many thanks.

  • Moses Kurniawan

    Hello, I am just trying your trick, but it not working. When I open http://localhost:8888/ there is nothing opened, in the VM, I am running ubuntu server, Apache already running. How to troubleshoot this?

    Thanks!

  • jc

    You can also try turnkeylinux Lamp appliance. I used it with my virtual box set up and didn’t have to worry about any other installs since it comes with webmin, phpmysqladmin, sql and sftp already installed. The only thing I had to do was give my root greater access in order to use mysql gui tools on xp.

    jc

  • Louis Simoneau

    @Moses: have you restarted your VM after setting the port forwarding rules in the xml config?

  • Moses Kurniawan

    Louis Simoneau : Yes I already restart the VirtualBox after modifying the XML. Still not working. I have been googling and I have some trick to do with Guest’s Linux Firewall and open the port, restart (guest, vm, virtualbox), and still the same, not working.

    Any solution?

    Thanks!

  • Euan

    What a great article! Not only all the info I needed, but very clearly written as well. I wish I’d found it before I wasted so much time with MS VirtualPC. Thanks so much for this.

    I have one question though, about VirtualBox itself. If I leave the server running for a while, when I come back to it the screen is black and I haven’t worked out how to reactivate it. I can send a Ctrl-Alt-Del which shuts it down, so it is obviously still running, but I can’t get back into it to actually use it. I expect I’m missing something really basic, but would be glad of help.

  • http://www.info-care.com.pt gvlx

    @Euan

    Clicking with the mouse, keyboard, reactivating the input does not make the machine responsive? It is still responding to http and ssh?

    Power savings you defined/left on default on the OS within the VM might be forcing the machine to sleep.

  • http://www.mikehealy.com.au cranial-bore

    Thanks for this post, it’s gotten me closer to a Linux dev environment than I’ve gotten before.
    One issue I’m having though is that I can’t edit my php.ini file (/etc/php5/apache2/php.ini)
    I need to increase the PHP memory limit for Magento to have a chance.

    I don’t have permission over FTP. How can I edit this file?

  • Louis Simoneau

    You can edit it over your SSH connection using vi or nano:

    sudo nano /etc/php5/apache2/php.ini
  • http://www.mikehealy.com.au cranial-bore

    Thanks Louis – worked

  • melwood

    @GeneralBill:

    How did you set up a network share? With Samba? Could you explain how you did this (configured samba)?

    melwood

  • msruby64

    There are some great ideas in the article and comments. I’m currently using xampp for my local php/MySQL development. Does anyone have any pros and cons of using xampp versus Virtual Box + Turnkey Linux + Lamp Appliance?

    Mitchell

  • Clarisse451

    You have no idea how much time and headache you have saved me. I’m running a centos guest on my XP host and not only did your post work perfectly and I can see everything, but I can FTP and SSH to the server from the host and from any other PC on the network.
    You are forever bookmarked! Amazing post! Amazing. :)

  • a

    2
    3

    For those who prefer use e1000 as virtual netcard

  • Anonymous

    What’s the best way to set up multiple sites on a server like this? I’m assuming I should have a “websites” folder or something with separate folders for each site and then virtualhosts for each one in apache? I’ve never done this in a fresh linux install.

  • http://www.joliett89.comli.com/ jtt89

    There are suppose to be three methods of getting this to work (I am talking mostly about Cable Modem – ethernet cable connection <usb is probably ok too, but I am trying to focus on what I have in order to get it working):

    1) Adapter 1: Host-only Adapter (to connect host and guest ; pretty standard settup too: cable modem, Win7, CentOS – thats what a lot of people might have I think)
    2) Bridged Adapter (it is described here: http://www.youtube.com/watch?v=K40jFNBwyJI )
    3) Port forwarding, just like you describet in your post

    I dont have a static IP or a router (what a lot of people say would be a lot better) and it seems like port forwarding is the only way to go. Do you have any idea if any of these / all of them would work with that kind of configuration (again nothing but cable modem, win7, Linux guest)
    Thank you.

    • http://www.joliett89.comli.com/ jtt89

      Numner 1 should be 2 adapters at the same time Host-only Adapter + NAT as no 2 to establish Internet connection for the guest.