Django development server on Debian 6 Stable

This is an older post, I now recommend using Vagrant — and a look at my Python VM Quickstart

This guide follows on from the previous post; Lightweight Debian VM on OS X with VirtualBox, but it should work more-or-less fine on a standard Squeeze install.

The webserver stack I'll be running will be for Django web development. This isn't for staging or production so I won't be installing a webserver or front-end such as Apache or nginx, Django's WSGI development server will suffice.

In case you shut down the instance previously, here's the startup command:

$ VBoxHeadless -s "Debian Webserver"

I use git, feel free to use whichever (D)CVS you prefer:

local$ ssh devserv
~$ devserv login:
~$ sudo apt-get install git-core

Now add the stable backports repo to your sources.list

sudo vim /etc/apt/sources.list

# squeeze-backports
deb http://backports.debian.org/debian-backports squeeze-backports main >> /etc/apt/sources.list

Installing psycopg2 is as you'd expect, but I've chosen to install Django 1.3 (official stable, the current major release in the squeeze backports repo). We'll need to update the apt cache as we just added a new repo.

~$ sudo apt-get update
~$ sudo apt-get install postgresql postgresql-client python-psycopg2
...
~$ sudo apt-get -t squeeze-backports install python-django

PostgreSQL Database setup

You'll need to configure postgres to allow local users to authenticate as db users.

$ sudo vim /etc/postgresql/8.4/main/pg_hba.conf

And modify the local line to change the ident value to "trust".

local   all         postgres                          ident

# TYPE DATABASE USER CIDR-ADDRESS METHOD ... # "local" is for Unix domain socket connections only local all all trust

Set up a user with privileges on a dev database:

$ sudo adduser django_dbo
...
$ su postgres
$ psql template1
...
template1=# create user django_dbo with password '';
template1=# create database django_dev;
template1=# grant all privileges on database django_dev to django_dbo;
template1=# 
$ exit
$ su django_dbo
$ psql django_dev
django_dev=>
django_dev=> 

Django setup

If that went smoothly then go ahead and setup a starter Django project:

~$ cd
~$ mkdir django
~$ cd django/
~/django$ django-admin startproject dev
~/django$ ls dev/
~/django$ 
__init__.py  manage.py  settings.py  urls.py

Update your settings.py file's DATABASES dictionary with the following:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'django_dev',
        'USER': 'django_dbo',
        'PASSWORD': '',
    }
}

Save that and try running syncdb to setup the db (plus a superuser).[1]

$ python manage.py syncdb

If that worked then fire up a dev server:

$ python manage.py runserver 0.0.0.0:8000

Visit your server on your local machine in a browser at: http://devserv:8000

The reason for running it under 0.0.0.0 is so that you can access it on your local machine. It probably won't be accessible on your LAN, and almost definitely shouldn't be accessible to the outside world[2].

Annotations

  1. chmod +x your manage.py file so you can execute it like thus:
    $ chmod +x manage.py
    $ ./manage.py runserver ...
  2. You should only run it under 0.0.0.0 if you're using the VM method from the previous post, otherwise it'll be publicly accessible.

References

  • Allow local logins via psql: http://www.cyberciti.biz/faq/postgresql-remote-access-or-connection/.