One of the most common tools used to test and develop web projects is XAMPP. The idea is simple, you run a small web server on your local PC and use it to host a copy of the website you’re working on. The problem with this is that it is not that similar to the environment that the site will actually run under, hosting companies very rarely use Windows for starters.

There are plenty of other options; you could use an old PC as a home server, use a test account with a hosting company or just test it on a live server. These all have their problems; a dedicated server PC can be pricey and any remote testing will be slow and a little awkward to work with.

I personally use the first option since I have a small home server for a few other things anyway. It basically has a network share that is the document root for Apache which means I can edit files and just reload the page. This is a great set-up that works very well for me but what if you don’t have a server at home? The exact same thing can be done using a virtual server.

I’ve mentioned VirtualBox a few times before here, it’s software which lets you run another operating system in a window on your desktop. We can use it to create a virtual server to test our sites on, here’s a quick guide.

Install VirtualBox

Grab the VirtualBox installer from the official website or just use apt if you’re on Linux. Run through the installer and let it do it’s stuff then it’s on to the interesting bit.

Create a New Virtual Machine

This is the good part where we create a virtual Linux web server running on our desktop PC. Create a new machine by clicking the button in the top left, you should get a window a bit like this one

VirtualBox create VM window

We’ll be installing Ubuntu server as the base operating system so pick that as the machine type. The rest of the default settings are all fine although, depending on how much memory your desktop has and the application you want to test, you may want to increase the amount allocated to the machine.

There is one thing to change after this. By default VirtualBox isolates the machine from the network – not very useful if we want to connect to it from the outside. Open up the settings for the machine and go to the network tab.

VirtualBox network settings window

Selecting Bridged Adapter will disable any fancy network handling and attach the machine straight to the main network.

Install the Operating System

The first time you try and start the server it will ask you to pick an ISO to install from. This is is maybe a bit unexpected and a little confusing but just select the server install image and carry on with it. The next thing you should see is the same screen as if you’d just put the CD in to a physical PC.

First install screen for Ubuntu Server

The UI will guide you through the installation – the default options are all fine although you may want to pick a fancy host name.

You’ll get asked to create a user account at one point, just use any name and password for this as we’ll actually delete this user later. For the sake of this guide assume this was just username.

Once it’s all installed and rebooted it should give a login prompt with the hostname you picked.

Ubuntu server login screen

Configure the Operating System

The defaults are all basically fine but there is one thing that should really be changed. The default is to use a named account to login then change to the root user when needed, this is pretty irritating since we’ll only ever be installing stuff or editing config files – so lets just login as root and be done with it. Anything in a text box should be entered as a command on the server.

sudo su
cd
passwd

After these few commands the root user will have a password set and can be used to login. Ubuntu won’t let you delete a user that is logged in so to delete the default user we need to logout of both accounts

exit
exit

Log back in as root and remove the other user

userdel username
rm -R /home/username

That small window is pretty annoying so lets make that slightly bigger. Open the settings file for grub with

nano /etc/default/grub

and change the line

GRUB_GFXMODE=1024x768
GRUB_GFXPAYLOAD_LINUX=keep

CTRL+X to exit the editor and let it save the changes. To apply these changes we have to tell grub to update with

update-grub

Now reboot and it should be a bit bigger.

Finally install any updates that have been published since the ISO was released.

apt-get update
apt-get dist-upgrade
apt-get autoremove
reboot

Install Software

Luckily this part is fairly straight forward.

First we need Apache

apt-get install apache2

The default page should now show up if you go to the IP of the server in a browser, to find the IP out use ifconfig. The IP address is the bit shown next to inet addr.

Output of the ifconfig command showing the local IP address

For basic HTML pages this will do but as most sites these days are dynamic we may as well install PHP.

apt-get install libapache2-mod-php php-mysqlnd

The first thing here is the module for Apache to run PHP script and the second is the MySQL database extension for PHP. We’ll probably want to test with a local copy of a database so why not add MySQL server too

apt-get install mysql-server

This will ask you for a password for the root user. Not to be confused with the system root user, but you may as well use the same password for ease.

To create a shared network location we need Samba so lets install that too

apt-get install samba

Configure Software

That was a lot of stuff to install and now it all needs to be configured – luckily there is not that much that needs to be changed to get it working then we can teak things over time.

We’ve already got Apache serving up pages so we may as well start with that.

The default location to store web files in /var/www and there is no reason to use anything else. It’s got the default page in and owned by root though which is no good as we need permission to write to it.

rm -R /var/www/*
chown -R www-data:www-data /var/www

The server should not be offering a 404 page – we need to change the document root from /var/www/html to /var/www.

nano /etc/apache2/sites-enabled/000-default.conf

This file is where Apache stores the default host config. We just need to change the DocumentRoot option to remove the /html part from the path. Now reload the server config

apachectl graceful

The page should now be showing an index of an empty folder ready for us to dump all our projects in. The last thing to do is configure a network share so we can easily access the files. Open up the config file Samba

nano /etc/samba/smb.conf

then add a block to the bottom of this file that looks like this

[www]
path = /var/www
writeable = yes
browseable = yes
guest ok = yes
create mask = 0750
directory mask = 0750
force user = www-data
force group = www-data

The guest ok option means there is no need to authenticate to access the share and the last 4 are to make sure we don’t create files that Apache can’t read.

Restart Samba and we should be all good

service smbd restart

Testing it Out

Going to smb://<ip>/www (or \\<ip>\www in Windows language) should give a folder that files can be copied in to. When creating a file in this folder it should appear in the directory index Apache shows, if not then there is a problem with the share or web server config.

If all is well so far create a file called test.php and save the following in to it

<?php

phpinfo();

Viewing that file via the browser should show a dynamically generated page with information about the server, if not there is something wrong with the web server config.

All Done

You’ll probably want a few more PHP modules. Most of them can be installed with apt-get to find the package name to use you can use apt-cache to search. If I wanted to install the GD library for example apt-cache search php gd would tell me that the package name I want is php-gd – you can guess correctly a lot of the time.

There are control panels that you can get but all of the free ones I’ve tried don’t really make life much easier than the terminal does.

That’s it. Okay I admit a bit of a process, but it is much cooler than the boring XAMPP install and is a little bit more realistic.