Build Your Own Dev Server with VirtualBox
Key Takeaways
- Sun’s VirtualBox is an effective tool for creating a test server environment, allowing developers to run Apache, PHP, and MySQL on a virtual Linux server, which can be accessed and interacted with just like a live server.
- Setting up a virtual server involves downloading the VirtualBox client and a disk image for a Linux server, creating a new virtual machine in VirtualBox, and installing the Linux server on the virtual machine. The server can be accessed from the host system by editing the xml configuration file for the virtual machine.
- The virtual server can be used to test server configurations, develop websites and applications, and practice sysadmin skills in a full Linux server environment running inside the user’s usual desktop. VirtualBox can be installed on various operating systems, including Windows, Linux, and macOS.
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.
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.
Frequently Asked Questions (FAQs) about Building Your Own Dev Server with VirtualBox
How can I install VirtualBox on different operating systems?
VirtualBox is a versatile tool that can be installed on various operating systems including Windows, Linux, and macOS. The installation process varies slightly depending on the operating system. For Windows, you can download the installer from the official VirtualBox website and follow the prompts. For Linux, you can use the terminal to install VirtualBox using the apt-get command. For macOS, you can download the dmg file from the official website and follow the installation wizard.
What are the system requirements for running VirtualBox?
VirtualBox is a lightweight application that doesn’t require high-end hardware. However, the performance of your virtual machines will depend on your system’s resources. You need at least 2GB of RAM and 20GB of free disk space to run a basic virtual machine. For optimal performance, a multi-core processor and 4GB or more RAM is recommended.
How can I create a virtual machine in VirtualBox?
Creating a virtual machine in VirtualBox is straightforward. First, open VirtualBox and click on “New”. Then, name your virtual machine, select the type and version of the operating system, and allocate RAM. Next, create a virtual hard disk and select its type (VDI is recommended). Finally, specify the storage details and click on “Create”.
How can I install an operating system on my virtual machine?
To install an operating system on your virtual machine, you need an ISO file of the operating system. In the settings of your virtual machine, go to the “Storage” section and under the “Controller: IDE”, click on the CD icon and select “Choose a disk file”. Locate your ISO file and click “Open”. Now, start your virtual machine and follow the installation prompts of the operating system.
Can I run multiple virtual machines simultaneously?
Yes, you can run multiple virtual machines simultaneously in VirtualBox. However, the performance will depend on your system’s resources. Each virtual machine uses a portion of your system’s RAM and CPU. If you run too many virtual machines for your system to handle, it may slow down or become unresponsive.
How can I share files between my host and guest operating systems?
VirtualBox provides a feature called “Shared Folders” that allows you to share files between your host and guest operating systems. To use this feature, go to the settings of your virtual machine, navigate to the “Shared Folders” section, and add a new shared folder. You can then access this folder from both your host and guest operating systems.
Can I take a snapshot of my virtual machine?
Yes, VirtualBox allows you to take snapshots of your virtual machine. A snapshot is a saved state of your virtual machine that you can revert to at any time. This is useful for testing software or settings without risking damaging your main system.
How can I improve the performance of my virtual machine?
There are several ways to improve the performance of your virtual machine in VirtualBox. You can allocate more RAM or CPU cores to your virtual machine, enable 3D acceleration, or increase the video memory. However, be careful not to allocate too many resources to your virtual machine as it may affect the performance of your host system.
Can I use VirtualBox for network simulation?
Yes, VirtualBox has built-in networking features that allow you to simulate different network configurations. You can create virtual network interfaces, configure NAT or bridged networking, and even simulate network latency and packet loss.
Is VirtualBox suitable for production environments?
While VirtualBox is a powerful tool for testing and development, it is not typically used in production environments. This is because VirtualBox is a type-2 hypervisor, which means it runs on top of a host operating system and may not provide the same performance and isolation as a type-1 hypervisor like VMware ESXi or Microsoft Hyper-V. However, for small-scale deployments or non-critical workloads, VirtualBox can be a cost-effective solution.
Louis joined SitePoint in 2009 as a technical editor, and has since moved over into a web developer role at Flippa. He enjoys hip-hop, spicy food, and all things geeky.