Setup Debian server in Virtualbox

The thing is that I like to keep my computer clean. I especially do not want to install web servers in to my computer. Laptop should not act as server. I try to resist temptation to install and test random software on it, because it could mess things up, but sometimes I have no choice. As a software developer, I often have to test and to work with a lot of software. To install and remove, then install and remove something again happens usually. Many servers and libraries with their specific versions and dependencies must be hosted on my single workstation. No surprising this leads to conflicts and disorder.

Fortunately there is an effective way to isolate those different installed items from each other, and of course to keep my computer clean. As you might guess I am talking about virtualization. I use Ubuntu and my favorite virtualization software is Virtualbox for its ease of use and rich feature feature set. And it also is free. This article describes instructions of how to install Debian as a guest operating system in Virtualbox which runs in Ubuntu host. I have chosen Debian, because it is possible to install very base version of Debian, which contain only what is needed and has a small footprint.

There is a special Debian flavor, to instal the very basic components of OS. It is called netinst and can be found at: http://www.debian.org/CD/netinst/ – ISO file is only 180MB. Now download the netinst CD image for i386 architecture.

Install Debian in to virtual machine

Open the VirtualBox, press “New” button in window toolbar, the New Virtual Machine Wizard starts. Click on the “Next” button. Wizard asks to name the new virtual machine, and to select OS type. I name mine new virtual machine as “Debian PHP” (I will use this virtual machine to host PHP systems), and Operating system and version are of course “Linux” ad “Debian”. Go to the next step: it asks how many RAM will be allocated for virtual machine,- stick with recommended 384MB. The next question is how to create a virtual disk. Check the “Boot Hard Disk” and select “Create new hard disk”. It will open another wizard in ths wizard – “Create New Virtual Disk” just go next. In the next question answer “Dynamicaly expanding storage”. The next question is Virtual Disk Location and Size, I leave default name, and select size of 2GB only,- because I do not want this virtal machine to spread and eat all free space on my computer. Both wizards can now be finished.

Open settings window of the new created VM. In Storage page click Empty cd drive (which is next to IDE Controller in the Storage Tree). Click CD icon in Attributes section to setup virtual CD/DVD drive. Then choose the downloaded Debian CD ISO file as a disk. Virtual machine is ready to be started.

Virtual machine boots from CD, starts the install. It first asks for language, English is ok. I choose locale en_US.UTF-8 and keyboard layout American English. In next question I set hostname to “debian-php” and stick with default domain name. Then enter the root password, and create new user. Asked about partitioning, choose default “Guided – use entire disk”. Some people are uncertain in this step, as setup says that it is going to wipe the disk, but it will not delete the disk of your computer, it talks about your new created virtual disk, which lives in one file actualy. I select to keep all files in one partition. Then finnish the partitioning with writing changes to disk. Install starts. It takes some time. During the process it asks some qustions for configuring apt, for instance what are proxy if any, and select apt mirror nearest to you. When “Software selection” dialog appears I select

Web server File server Mail server SQL server SSH server Standard system utilities

Setup will ask for workgroup name for Samba server, I choose default WORKGROUP. Before finishing setup will ask where install grub, choose default. Setup is finished.

Configure networking

During virtual machine creation, VirtualBox created virtual network adapter using NAT. Which enables virtual machine access internet. But it bypass host machine, so connection from host to virtual machine can not be established.

We will use another network adapter to access virtual machine from host. When virtual machine is stopped edit it’s settings not to use installation iso anymore, and add one more network adapter, choose it’s type to “host only network”. This new adapter will be used to comunicate with your virtual machine from your host. You can now start virtual machine again, when it starts, see if vboxnet0 adapter can be seen on host, just check ipconfig output for something similar to this:

vboxnet0 Link encap:Ethernet HWaddr 0a:00:27:00:00:00
inet addr:192.168.56.1 Bcast:192.168.56:.255 Mask:255.255.255.0
inet6 addr: fe80::800:27ff:fe00:0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:3808 (3.8 KB)

See what inet addr is given to host’s adapter to the network in which virtual machine also is accessible. Now in edit virtual machine’s /etc/network/interfaces file, so that connection to host could be made thru eth1 network interface.

The file alredy contains network interfaces defined:

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp
so we add also interface for host only networking adapter:

# The host network interface
auto eth1
iface eth1 inet static
address 192.168.56.2
netmask 255.255.255.0

Note ip is 192.168.56.2, because vboxnet0 ip is 192.168.56.1 so, both IPs are on the same network. Restart the Debian virtual machine. You can now ssh to it:

$ ssh devel@192.168.56.2

On host machine add line:

devel-php 192.168.56.2

So, you do not have to type ip but access the virtual machine via its hostname:

$ ssh devel@debian-php

And virtual machine is able to access internet. Nevertheless you do not have to run Virtualbox GUI:

$ nohup VBoxHeadless -s "Debian PHP" &

will start virtual machine in background. Few seconds later you should be able to ssh into it.

Configure Samba share

Let us now host sample php project on our server. In devel user home create a directory named projects. This directory is to be shared to host machine via Samba. In file /etc/samba/smb.conf make sure that options has such values set. And in the end of file add:

[Projects]
comment = Directory for hosted php projects
path = /home/devel/projects
browseable = yes
read only = no
guest ok = yes
valid users = devel

Reload samba:

# /etc/init.d/samba reload

In Ubuntu host Finder window press CTRL + L, and type in to location bar: smb://debian-php/Projects Enter devel user and its password. Shared directory opens, now you can bookmark it in Finder.

Configure virtual hosts

In shared directory create a new directory test, and index.php file in it – it will be our first project. In index.php add code:

<?php phpinfo();

Now back in debian login as root user and create file /etc/apache2/sites-available/projects with content:

ServerName test.dev
DocumentRoot /home/devel/projects/test
ErrorLog /home/devel/logs/test-error.log
LogLevel notice
CustomLog /home/devel/logs/test-access.log combined

Enable it:

# a2ensite projects

and reload Apache:

# /etc/init.d/apache2 restart

Login again to debian virtual machine with devel user and create directory logs with writing permissions for everyone.

then in Ubuntu host machine add one more hostname for test site:

192.168.56.2 test.dev

That is it! Try to connect to URL http://test.dev with web browser.

Comments !

tag cloud

social