Over the past few months, I’ve grown increasingly interested in simplifying deployment of my applications. I intend to create a one click deployment process for my latest Ruby on Rails application. My approach is to build a VM running the application then write a script to clone and configure the VM. I want the script to require as little user input as possible to create a brand new production instance of the application. Once the process completes, it will be replicated for future applications.
Today I’m going to discuss the process of building an Ubuntu 12.04 LTS server on VirtualBox. I’ll focus on:
- Creating the virtual machine.
- Installing Ubuntu 12.04.1 LTS Server Edition with OpenSSH
- Network configuration
- Managing the server
My host operating system is Ubuntu 12.10 Desktop Edition. I will assume you are using the same or know how to translate my instructions for use in your environment of choice.
Creating a virtual machine in VirtualBox is pretty trivial. Launch Oracle’s VM VirtualBox Manager, and click the “new” button. A wizard will appear with pretty simple instructions. Give the machine a meaningful name. I use scripts and aliases in most instances where I need to reference my virtual machine name so I favor verbosity over brevity here. I used the title ubuntu-12.04.1-LTS.
Use your judgement in allocating resources. I’m pretty stingy, but I give my machines 1024 MB (1 GB) RAM and 8 GB disk space on a fixed size VDI (VirtualBox Disk Image). The fixed size disk ensures that there’s never confusion about whether the VM can actually use more disk space. With dynamic allocation, the host may run out of room when the VM still thinks it should be able to get more.
I consider the addition of network adapters to be part of building the machine. I suggest the use of a bridged adapter on adapter 1 and a host only adapter on adapter 2. I’ll run through the process, but first, a little information about what we’re doing and why:
The bridged connection on adapter 1 allows your VM to access the internet via your hosts web connection without any additional configuration.
The host only connection on adapter 2 allows your host to connect directly to the VM. This allows you to connect via SSH and to send HTTP requests through your browser. The host only connection requires a host only network which we build from the VirtualBox VM Manager.
Before we start adding network adapters, lets make sure there’s a host only network ready for use. Access the VM Manager preferences from File -> Preferences or Ctrl+G then click “Network” on the left side of the dialog box. Your existing host only networks will be listed. If there is already a network that is not being used by a VM that will run at the same time as the new Ubuntu 12.04 server, take note of the name and close the box. Otherwise, click the icon with the + and a new network will appear on the list (vboxnet0 by default). Take note of the network name. Click the name to highlight it then click the edit button and take note of the IPv4 Address (In my case, 192.168.56.55).
Click the name of your new VM in the list of VMs on the right of the VM Manager window to select it. A list of settings and options appears on the right side of the window. Click the heading Network. With the Adapter 1 tab open, ensure that Enable Network Adapter and select Bridged Adapter from the Attached to select box. For Name choose your preferred network connection. (I use wlan0 which is my WIFI).
Switch to Adapter 2 and ensure that Enable Network Adapter is checked here too. Select Host-only Adapter from the Attached to select box, and select the host only network that we set up in the VM Manager preferences earlier for Name.
Just like that the VM’s hardware is configured. The next step is to download the Ubuntu 12.04.1 LTS server edition ISO and attach it to the VM’s CD/DVD drive. Download the ISO then click Storage in VB’s settings dialog. Right click IDE Controller and select Add CD/DVD Device. When prompted, click Choose disk and select the Ubuntu server ISO. Click OK to close the settings dialog.
You’re ready to boot the server. Click the name of the server then click Start. The server will begin to prompt for installation options. Make sure to enable networking and OpenSSH. I use mostly default settings and avoid installing packages until I’ve booted the system. If you have trouble, here’s the server guide.
You can interface with the server with the window provided by VirtualBox, but I prefer to use my native applications. First I need to get the server to recognize it’s host only connection which will appear at eth1. Run:
Notice there is an entry for eth0 which is the bridged connection to the internet. This means that you can run your package manager to begin installing software, but you don’t have a connection for running SSH. To fix this, we need to edit the network interface. I prefer to use vim when editing server files, so install it with:
$ sudo apt-get install vim
Or don’t, whatever. Open the /etc/network/interfaces file and add the following code:
iface eth1 inet static
In each section of italics, the first three sets of numbers are the same for each italicized IP address. These come from the first three sets of numbers you noted from the vboxnet0 host only network. Only the address line has the fourth set of numbers highlighted. You can specify whatever you want as long as it’s between 0 and 255 (exclusively). I use 55 for sentimental reasons and because it’s easy to remember.
After saving the changes, restart networking:
$ sudo /etc/init.d/networking restart
Then run ifconfig to ensure that the eth1 network is present and the IP is the one specified in the address line of the interface file.
Shut it down again and open the terminal for the host machinge. Enter the following command:
$ VBoxManage startvm ‘ubuntu-12.04.1-LTS‘ –type headless
Replace the italics with whatever you named your server. The server will start, but without the VirtualBox manager’s window. Wait a few seconds to let it boot then SSH into the server using the IP address specified for eth1.
$ ssh firstname.lastname@example.org
Once you are logged on, it’s probably a good idea to install updates, then install all the packages you need to run your server.
Using sshfs you can mount a directory on your VM to a directory on the local machine. This is convenient if you want to access VM files with local applications (such as gedit).
$ mkdir ~/dev/app-remote
$ sshfs email@example.com:/home/chris/dev/app /home/chris/dev/app-remote
You can then access files from the server by browsing to the local directory /home/chris/dev/app-remote.
In future articles, I’ll discuss setting up application environments and clone them for quick server setup.