Setting up a Virtual Webserver

This is a quick guide on how to setup a webserver in a virtual machine. This is based on using Virtualbox and Ubuntu, so it might not work with all version of linux and though the install instructions inside the virtual machine will work with any virtualisation tool the rest of the info, of course, will not. This is as much a reminder to myself as I seem to always forget to do one thing when doing this. Its really useful being able to just create a VM and fire off testing without having to compromise your normal desktop. I do this for many different coding projects, allowing me to have the exact environment I need / users have.
Firstly, lets install virtualbox (of course you could install one of many other virtualisation products):

sudo apt-get install virtualbox

Follow the on screen instructions on how to create a guest os in virtual box, quite straight forward. I tend to use a dynamically allocated disk. Start up the OS and find the image file you wish to boot (I’ve been using Ubuntu 10.04).
Like you would do for any operating system install this to the local disk.
To make life easier lets first setup SSH to the system.

sudo apt-get install openssh-client openssh-server

Now turn off the guest OS and in virtual box go: settings > network > adapter 2 > Enable
and change to attached to: Host-only adapter using vboxnet0
Reboot the guest OS.
Now we need to edit: /etc/network/interfaces to (you need to run something like sudo pico /etc/network/interfaces/), adding:

auto eth1
iface eth1 inet static

Now to finish the network setup, run:

sudo ifup eth1

Lets now restart the network:

sudo /etc/init.d/networking restart

We should now be able to ssh in to the system using: ssh your-username@
If you don’t want to have to remember the IP address you can add this to your /etc/hosts files, with: yourvirtualos

Lets now install the important webserver tools:

sudo apt-get install apache2 php5 libapache2-mod-php5

Create a directory called “public_html” in your home directory,

mkdir public_html

and lets configure apache and get it running:

cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/userdir.conf userdir.conf
sudo ln -s ../mods-available/userdir.load userdir.load
sudo /etc/init.d/apache2 restart

sudo pico /etc/apache2/mods-available/php5.conf

change to:

<IfModule mod_php5.c>
<FilesMatch “.ph(p3?|tml)$”>
SetHandler application/x-httpd-php
<FilesMatch “.phps$”>
SetHandler application/x-httpd-php-source
# To re-enable php in user directories comment the following lines
# (from <IfModule …> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
# <IfModule mod_userdir.c>
# <Directory /home/*/public_html>
# php_admin_value engine Off
# </Directory>
# </IfModule>

i.e. comment out <IfModule… </IfModule>
And there you have it a webserver running in a virtual machine. Nice and straight forward really and takes about 10 minutes. Really useful for doing some web testing before uploading.
To test this just go to: on either your main desktop or in your guest OS.


One response to “Setting up a Virtual Webserver”

  1. krioma says :

    In addition to this, it is worth noting that the first time you use virtualbox you probably need to set up the host only networking in VirtualBox (the thing called vboxnet0) above. To do this all you have todo is in virtualbox go: File > Preferences > Network and hit add host only networks.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: