localhosts and virtual hosts

I’ve been working on a number of website development projects of late. I use my laptop, running Ubuntu Linux, for all of the development work. I use:

I also use an Oracle database for storing data for projects at work, Flinders University, my other projects haven’t needed a database platform yet.

To keep all of the projects organised I’ve created virtual hosts and associated directories for each project. Virtual hosts allow you to run more than one host on the same IP address. More information on virtual hosts is available here.

This is the procedure that I follow for starting a new project. The exact details may be different depending on your Linux distribution. I would imagine the same principles apply for doing this under Windows, but I don’t know how.

  1. Make a backup copy of the /etc/hosts file
    sudo cp /etc/hosts /etc/hosts.old
  2. Open the /etc/hosts file using vi as the root user
    sudo vi /etc/hosts
  3. Add an additional line for the new host, for example
    127.0.0.1 localhost4
  4. Save the file and exit
  5. Make a directory for the new project (all of mine are in a sub directory of my home directory)
    mkdir /home/user/web-sites/www4
  6. Copy the existing default virtual host configuration file
    sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/www4
  7. Open the new file using vi as the root user
    sudo vi /etc/apache2/sites-available/www4
  8. Change the value of the ServerName directive to the host specified in step 3
  9. Change the value of the DocumentRoot directive to the directory created in step 5
  10. Change the value of the Directory directive to the directory created in step 5
  11. Save the file and exit
  12. Enable the new virtual host
    sudo a2ensite www4
  13. Restart the Apache web server
    sudo /etc/init.d/apache2 restart
  14. Confirm the new site is up and running by visiting it in your web browser

I’ve got four websites so far running on my laptop using this technique and so far it is working really well. It means I can keep my directory structures clean and tidy which helps with keeping track of the various sites and the files within them.

Leave a Reply