Sep 20, 2010

Setting up Django environment in CentOS 5

I am trying to setup Django v1.2.3 to run on our server which is CentOS 5.2.

Though Django support python2.4 bundled with this OS, I decide to get it running on Python2.6, which makes me feel not so out. In the beginning I was thinking it could be a task of 10 mins, and not expecting so many unexpected things happened:

The first issue is replacing python2.4 with python2.6 breaks the yum toolkit on CentOS 5.2. Shame for that. Fortunately some other guys has met the problem already and there are lot's of blogs and question/answers addressing this issue. I follow this one: how to install python2.6 on centos5 without breaking yum. It almost works, just a minor problem: yum report that libffi-3.0 not found while installing python2.6. So google again, and there it is:
wget ftp://ftp.pbone.net/mirror/centos.karan.org/el5/extras/testing/i386/RPMS/libffi-3.0.5-1.el5.kb.i386.rpm
After installing this rpm package, I can get python2.6 installed.

Now download Django-1.2.3.tar.gz from official site, unpack, and run python26 setup.py install. Oops, wait, there is problem:

invalid Python installation: unable to open usr/lib/python2.6/config/Makefile


Okay, google again, and someone said it's due to you have not installed python-dev package. Let's try yum install python26-dev and then run python26 setup.py install again. Bingo!

Now that we have the Django installed at /usr/lib/python2.6/site-packages. Let's give it a try:

1. update /etc/bashrc, add "export DJANGO_HOME=/usr/lib/python2.6/site-packages/django"
2. . /etc/bashrc && chmod a+x $DJANGO_HOME/bin/*.py
3. ln -s $DJANGO_HOME/bin/django-admin.py /usr/bin/django-admin.py
4. cd /tmp
5. django-admin startproject mysite

Oops, error again:
Traceback (most recent call last):
File "/usr/bin/django-admin.py", line 2, in ?
from django.core import management
ImportError: No module named django.core

Damn it, I forgot django is installed to python26. Okay, let me do something to hack it:
# cd $DJANGO_HOME/bin
# find . -name '*.py' | sed -i 's/env python/env python26/'

now run django-admin startproject mysite works. and let me continue:
6. cd mysite && python26 manage.py runserver

OMG, it breaks again, all the files generated by django-admin use python as default env, not python26. I am getting tired of adding my hacks to Django. I need to figure out some other ways.

Yes, python support virtualenv, that's exactly what I need! Follow the process described in this blog, I can continue again:
# wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.5.1.tar.gz
# tar xzf virtualenv-1.5.1.tar.gz
# cd virtualenv-1.5.1
# su greenl
$ python26 virtualenv.py ~/opt/local
$ cd ~
$ wget http://www.doughellmann.com/downloads/virtualenvwrapper-1.23.tar.gz
$ tar xzf virtualenvwrapper-1.23.tar.gz
$ cd virtualenvwrapper-1.23
$ cp virtualenvwrapper_bashrc ~/bin
$ cd ~
$ mkdir .virtualenvs
$ mkvirtualenv django
$ workon django
$ easy_install django
$ cd /tmp
$ sudo rm -rf mysite
$ django-admin.py startproject mysite
$ cd mysite
$ python manage.py runserver &
$ lynx localhost:8000

Works perfect!

In conclusion:
1. You can install python2.6 on CentOS 5 without breaking yum
2. You can install django latest version (currently v1.2.3) on python2.6 site-packages
3. You need to install virtualenv in order to get django run on python2.6 transparently
4. You may want to install virtualenvwrapper in order to make your life easier with virtualenv.
5. After you have done all these, you have an nice env for your django journey!

No comments: