Nik's Technology Blog

Travels through programming, networks, and computers

Apache Web Development Testing Server Set-up

I've been setting up an Ubuntu linux machine for web development testing on my local network, but because the machine has only one IP address assigned to it I wanted a solution to be able to serve more than one website without changing the config in Apache each time.

One method would be to use a separate folder for each site but .htaccess rewrites don't work properly using this method and if you write hyperlinks that refer to the root of the site (i.e /contact.php), this can cause issues too.

Anyway, I thought of using different port numbers to distinguish the different testing websites. So one site maybe on, another on etc...

You set this up in Ubuntu as follows:

Open a Terminal prompt and type:

sudo gedit /etc/apache2/ports.conf

Enter your password and then for each port number you require add a Listen statement on a new line. So your file should look like:

Listen 80
Listen 2000
Listen 2001

Listen 443

Save the file and then go back to the Terminal and type:

sudo gedit /etc/apache2/sites-available/default

In this file you should find a <virtualhost *> tag followed by various commands. You need to copy and paste everything from <virtualhost *> to </virtualhost> onto a new line below.
Then rename the first <virtualhost *> to <virtualhost> where is the IP address of you machine. Rename the second virtual host to <virtualhost> and repeat for as many websites you want to set-up.

Then for each Virtual Host you'll need to change the DocumentRoot to the file path to each website on the local machine.

Once this is done you'll need to restart Apache to see if your changes have been successful. To do this type the following into the Terminal:

sudo /etc/init.d/apache2 restart