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 127.0.0.1/32 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 = '127.0.0.1' # 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

127.0.0.1 djangosite2.localhost
127.0.0.1 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:

Listen 127.0.0.1:80
ServerName 127.0.0.1
DocumentRoot "/usr/local/www/public_html"
LoadModule wsgi_module /usr/local/apache2/modules/mod_wsgi.so
NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
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
</Directory>
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
</Directory>
</VirtualHost>


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.

Friday, November 07, 2008

Python 2.6 and latest Django with Postgresql on Debian Etch.

I built and installed Python 2.6 from source but I used Debian Etch build of Postgresql from Synaptic. I needed to use Postgres instead of MySQL because the Python bindings to MySQLDB do not work with Python 2.6 as of today ( so I hear ). I decided to build the latest version of psycopg2, which is the Python binding for Postgres because the Synaptic version of psycopg2 might be configured for the Debian default version of Python 2.4.4. First, I had to get libpq-dev and libpq4 from Synaptic. Then I got the source of psycopg2-2.0.8. Next, build/install:


$ python2.6 ./setup.py build
$ sudo python2.6 ./setup.py install


( python2.6 is named as such because I made altinstall when running make for Python 2.6 )

I was able to test that this is working so far


$ python2.6
Python 2.6 (r26:66714, Nov 3 2008, 01:27:29)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 1, 0, 'alpha', 0)
>>> import psycopg2
>>> psycopg2.apilevel
'2.0'



It works so far; now I simply have to get Django and Apache working.

Labels: , , ,

mod_wsgi
Apache 2.2.10
Python 2.6
Django latest from subversion
on Debian Etch ADM64

compiling mod_wsgi, Python 2.6 and Apache 2.2.10 worked for me by configuring Python 2.6 with


--enable-shared

then after make and make install:


$ ln -s /usr/local/lib/libpython2.6.so.1.0 /lib


Next, link the python shared object to a place that mod_wsgi will use it.


$ sudo ln -s /usr/local/lib/libpython2.6.so /usr/local/lib/python2.6/config/


I compiled Apache successfully by building it with


./configure --with-included-apr --enable-ssl --prefix=/usr/local/apache2 --enable-so


then configure mod_wsgi using


$ ./configure --with-python=/usr/local/bin/python2.6 --with-apxs=/usr/local/apache2/bin/apxs


I checked that the /usr/local/apache2/modules/mod_wsgi.so was compiled with the python shared object by doing this

$ ldd /usr/local/apache2/modules/mod_wsgi.so

It shows that it was compiled with



libpython2.6.so.1.0 => /lib/libpython2.6.so.1.0 (0x00002b99a1f1d000)
libpthread.so.0 => /lib/libpthread.so.0 (0x00002b99a21c8000)
libdl.so.2 => /lib/libdl.so.2 (0x00002b99a22dd000)
libutil.so.1 => /lib/libutil.so.1 (0x00002b99a23e0000)
libc.so.6 => /lib/libc.so.6 (0x00002b99a24e4000)
libm.so.6 => /lib/libm.so.6 (0x00002b99a2721000)
/lib64/ld-linux-x86-64.so.2 (0x0000555555554000)




The test on

http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide

worked; So, off to my Django web development.

P.S. Thanks to Graham Dumpleton for help on

http://groups.google.com/group/modwsgi

from last year when I was trying to get this working with python 2.5 and an older version of Apache. Back then, I never got it to work and ended up using mod_python instead.

Labels: , , , ,