Install Python2.6.4, mod_wsgi and Django on CentOS 5.4

Once I got Django running on windows, I started hacking around setting Django up on CentOS. Little did I know, that upgrading Python and getting Django wouldn't be that easy. CentOS 5.4 uses python 2.4, and replacing it is not really an option since yum and other core packages depend on it.

The solution was to install 2.6.4 from source and use /opt for the prefix  so that 2.6.4 becomes the default python. Then move on to install mod_wsgi, setup Apache to use mod_wsgi and Install Django on CentOS 5.4

 

 

Step1: Install Build Requirements

Run the following command as root (or with sudo) to install gcc and the development libraries used by python:



yum install gcc gdbm-devel readline-devel ncurses-devel zlib-devel bzip2-develsqlite-devel db4-devel openssl-devel tk-devel bluez-libs-devel


 

Step2: Download & Install Python 2.6 from source



cd wget http://www.python.org/ftp/python/2.6.4/Python-2.6.4.tgz


tar xvfz Python-2.6.4.tgz


cd Python-2.6.4 ./configure --prefix=/opt/python2.6 --with-threads --enable-shared


 

Configure Python 2.6 so that we're not overwriting the system's python installation:



./configure --prefix=/opt/python2.6 --with-threads --enable-shared


make


make install


 

After running the make command you will see a list of modules that were not built. If you installed all of the devel libraries listed above, the only missing modules should be bsddb185 and sunaudiodev. You probably don’t need these – bsddb185 is the old version of the berkely db module, and sunaduiodev is for solaris. On x86_64 db and imageop may also be in the list, but if you look at the setup.py file, it looks like this is normal.

 

Step 3: Add an alias to root's .bash_profile and make a symbolic link



vi ~/.bash_profile


alias python='/opt/python2.6/bin/python'


 



source ~/.bash_profile


ln -s /opt/python2.6/bin/python /usr/bin/python2.6


 

Step4: Configure ld to find your shared libs



cat >> /etc/ld.so.conf.d/opt-python2.6.conf /opt/python2.6/lib (hit enter) (hit ctrl-d to return to shell)


ldconfig


 

Step 5: Test python 2.6 installation

On the console type python, and you should get an interactive Python 2.6 session like:



Python 2.6 (r25:XXXX, Dec 11 2009, 23:18:24) [GCC X.XXXXX.X Type "help", "copyright", "credits" or "license" for more information. >>> Hit ctrl-d to exit


 

Step 6: Install setuptools



cd..


wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg#md5=bfa92100bd772d5a213eedd356d64086


sh setuptools-0.6c11-py2.6.egg --prefix=/opt/python2.6


 

Step 7: Install MySQL



wget http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm


wget http://rpms.famillecollet.com/enterprise/remi-release-5.rpm rpm -Uvh remi-release-5*.rpm epel-release-5*.rpm


yum --enablerepo=remi install mysql-server mysql-devel python-devel


 

Step 8: Install MySQLdb package



wget http://internap.dl.sourceforge.net/sourceforge/mysql-python/MySQL-python-1.2.2.tar.gz


tar xvfz MySQL-python-1.2.2.tar.gz


cd MySQL-python-1.2.2


python setup.py build


python setup.py install


 

Verify MySQL-Phyton DB adapter:

On the console type python, and you should get an interactive Python session . In the session prompt below:

Python 2.6 (r25:XXXX, Dec 11 2009, 23:18:24) [GCC X.XXXXX.X Type "help", "copyright", "credits" or "license" for more information. >>> Hit ctrl-d to exit

 

Type

import MySQLdb

 

There should be no errors on the screen. So far so good!

 

Step 9: Install Apache2 and Apache2 Devel



yum install httpd


yum install httpd-devel


 

Step 10: Installing mod_wsgi

What is mod_wsgi?

The aim of mod_wsgi is to implement a simple to use Apache module which can host any Python application which supports the Python WSGI interface. The module would be suitable for use in hosting high performance production web sites, as well as your average self managed personal sites running on web hosting services.

 

Why mod_wsgi?

The consensus seems to be that mod_wsgi is the prefered Apache module (as opposed to mod_python). It's stable, less memory intensive, and faster.

 

Configure and Install mod_wsgi to link with Python 2.6 shared libs:



cd /opt/python2.6/lib/python2.6/config


ln -s ../../libpython2.6.so .


cd wget http://modwsgi.googlecode.com/files/mod_wsgi-3.1.tar.gz


tar xvfz mod_wsgi-3.1.tar.gz


cd mod_wsgi-3.1 ./configure --with-python=/opt/python2.6/bin/python


make


make install


 

It’s important to read the mod_wsgi docs on building with shared libs - mod_wsgi should compile to around 250kbytes.

Confirm that the size of mod_wsgi.so is around 250 Kilobytes:



ls -Al /usr/lib/httpd/modules/mod_wsgi.so


 

Mine turned out to be around 295 kilo bytes.

 

Add mod_wsgi to Apache 2.x:



vi /etc/httpd/conf/httpd.conf


LoadModule wsgi_module /usr/lib/httpd/modules/mod_wsgi.so


 

Restart Apache



service httpd restart


 

Check if mod_wsgi is loaded in the loaded module list by typing:



httpd -M


 

Step 10: Next Download Django.

Assuming your django download is extracted in /usr/src/django



ln -s /usr/src/django/django /opt/python2.6/lib/python2.6/site-packages/


 

You should now be up and running django on CentOS.

 

Important Notes:

1. SElinux does not go very well with wsgi environment. For testing, please turn off SElinux and then try these steps.

2. Apache might use wrong python version while calling wsgi handlers. Use wsgihome and wgipythonpath directives inside the httpd.conf to point it correctly.

3. In case of any errors, check the Apache error log for more details. You can always post a comment here, in case of any help.