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:
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 firstname.lastname@example.org
If you don’t want to have to remember the IP address you can add this to your /etc/hosts files, with:
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,
and lets configure apache and get it running:
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
# 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
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: http://192.168.56.101/ on either your main desktop or in your guest OS.