Setting Up a Local Development Environment in Ubuntu

Håvard Hegtun
This short guide explains how to set up a local development environment that allows you to create new sites on the fly and automatically serve them up on your local development machine in Ubuntu. This guide does not explain how to install mysql, php, apache or the like, it only describes how to automate the process of creating new sites without having to reconfigure your applications.

First, we need to install apache's mod vhost. You can do this via:

sudo apt-get install libapache2-mod-vhost-hash-alias

sudo mv /etc/apache/mods-available/vhost_alias.load /etc/apache/mods-enabled/

Now, we need to configure apache to make use of the vhost module. Let's start by creating the vhost directory:

sudo mkdir /var/www/vhosts

sudo chown www-data www-data /var/www/vhosts

And now add this at the end of our /etc/apache2/apache2.conf file:

#vhosts

NameVirtualHost *:80
UseCanonicalName Off
DocumentRoot /var/www/vhosts

Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all

VirtualDocumentRoot /var/www/vhosts/%0.0

This essentially says that any domain that we host locally will reside at /var/ www/vhosts/domain. You can modify the virtual document root as needed to add a public_html or htdocs directory if you wish. Make sure to cycle apache with:

sudo apache2ctl -k graceful

Apache should be running well enough now and you can testing this by creating a test domain directory (and adding a simple index.html file) and modifying your /etc/hosts file to point the test domain to 127.0.0.1. This works well enough on it's own but we don't want to add to the hosts file for every site we create. As such, we're going to install dnsmasq and route a full block of domains for our usage.

First off, let's install dnsmasq:

sudo apt-get install dnsmasq

Once installed, you will need to modify /etc/dnsmasq.conf and replace

#listen-address=

with:

listen-address=127.0.0.1

and at the end of the file add:

address=/dev/127.0.0.1

This will point all .dev domains to your local development environment. You can specify any extension you wish or a full domain (address=/example.com/127.0.0.1).

Now we need to modify /etc/dhcp3/dhclient.conf and uncomment the following line by removing the '#':

address=/dev/127.0.0.1

Restart your dnsmasq service with:

sudo /etc/init.d/dnsmasq restart

And you should be able to create new folders with the names site.dev in your /var/www/vhosts folder and they will be available at the domain in your browser. If you use an eclipse based editor you can change your workspace to sit in /var/www/vhosts and whenever you create a new project just name it site.dev and it will be instantly available.

Originally published at http://www.lamptricks.com/2007/11/25/local-development-and-hosts-on-ubuntu/

Published by Håvard Hegtun

An American immigrant born and raised in Norway. Now living in Southern California.  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.