More realistic local web development server

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

create new machine 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.

virtual machine network settings page

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 installtion screen of 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=640x480

to

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.

reboot

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

apt-get update
apt-get dist-upgrade
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 part shown next to "inet addr"

the IP address shown in the command line

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-php5 php5-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 for it to work.

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

DocumentRoot /var/www/html

to

DocumentRoot /var/www

and 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:///www (or \\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 php5 gd would tell me that the package name I want is php5-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.

Post a new comment