The Golden Spot

I hope this helps someone who is learning about Linux and web application programming with Python, Django, and Javascript.

Saturday, November 08, 2008

How to configure Postgresql with Django for localhost development with Apache 2.2.10

Debian Etch build of Postgresql(8.1) and Django subversion(9370)

I compiled psycopg2, Apache 2.2.10, mod_wsgi from source as mentioned here and here:

Installing Postgresql with Synaptic creates a user named postgres in the /etc/passwd file. Login as root or sudo and change the password for the system user named postgres:

# passwd postgres

. . . change the password to your favorite color password ( write it down! ( like all the others))

$ su postgres

. . . login and now

$ psql template1

this will bring up the postgresql shell using template1 ( read the postgresql docs regarding template1 database ) where I can change the postgresql password for the role ( or user ) postgres.

postgres# ALTER USER postgres WITH PASSWORD 'secretuberpassw' ;

create a role for Django to use while editing the Postgresql database:

postgres# CREATE ROLE djangopguser WITH LOGIN ENCRYPTED PASSWORD 'djangosecretamazing' ;

create the database and assign ownership to the djangopguser

postgres# CREATE DATABASE djangositedb OWNER djangopguser ;

my pg_hba.conf file:

# IPv4 local connections:
host all all md5 # this is here by default on Etch

my djangosite settings file:

DATABASE_ENGINE = 'postgresql_psycopg2'
DATABASE_NAME = 'djangositedb'
DATABASE_USER = 'djangopguser'
DATABASE_PASSWORD = 'djangosecretamazing'
DATABASE_HOST = '' # I have to be write the IP address here. If I leave it blank I can not authenticate to postmaster from Django

edit the /etc/postgresql/8.1/main/postgresql.conf file and uncomment

listen_addresses = 'localhost'
password_encryption = on

I am going to use virtual hosts so I can have multiple django sites at http://djangosite1.localhost and http://djangosite2.localhost as urls in my web browser.

first I will configure /etc/hosts and add the lines djangosite2.localhost djangosite1.localhost

this will allow url resolution of these domains to my localhost IP address ( this helps when looking at these urls in my web browser )

Now for the fun part of configuring Apache's httpd.conf. This has worked for me so far:

DocumentRoot "/usr/local/www/public_html"
LoadModule wsgi_module /usr/local/apache2/modules/
ServerName djangosite2.localhost
WSGIScriptAlias / /usr/local/www/public_html/djangosite2/apache/django.wsgi
<Directory /usr/local/www/public_html/djangosite2/apache>
Order allow,deny
Allow from all
Alias /media/ /usr/local/www/public_html/django-media/media/
<Directory /usr/local/www/public_html/django-media/media>
Order allow,deny
Allow from all

In addition to a bunch of httpd.conf directives that are included in the file by default, I left out the mod_wsgi file that is referenced here and placed in the directory as written in httpd.conf above. Also, some other information regarding the Django specific installation procedures that you can find yourself in the django documentation will make things more clear if you are having problems.


Post a Comment

<< Home