The day I got Django running on windows, I started hacking around setting Django up on CentOS. Little did I know 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
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-devel sqlite-devel db4-devel openssl-devel tk-devel bluez-libs-devel
Install Python
Download Python 2.6 source package, unzip, and enter working directory:
1 2 3 4 | 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 Python 2.6 so that we’re not overwriting the system’s python installation:
./configure --prefix=/opt/python2.6 --with-threads --enable-shared
Make and install:
1 2 | 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 looking at the setup.py, it looks like this is normal.
Add an alias to root’s .bash_profile:
1 2 3 | vi ~/.bash_profile alias python='/opt/python2.6/bin/python' source ~/.bash_profile |
Make a symbolic link:
ln -s /opt/python2.6/bin/python /usr/bin/python2.6
Configure ld to find your shared libs:
cat >> /etc/ld.so.conf.d/opt-python2.6.conf
Now Enter the following:
1 2 3 | /opt/python2.6/lib (hit enter) (hit ctrl-d to return to shell) ldconfig |
Test python 2.6 installation:
python
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
Install setuptools
1 2 3 | 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 |
Install MySQL
1 2 3 4 | 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 |
Install MySQLdb package
1 2 3 4 5 | 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:
python
In the prompt below:
Python 2.5 (r25:51908, Nov 9 2008, 23:18:24) [GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>>
There should be no errors on the screen. So far so good!
Install Apache2 and Apache2 Devel
1 2 | yum install httpd yum install httpd-devel |
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 mod_wsgi to link with Python 2.6 shared libs:
1 2 3 4 5 6 7 | 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 and install:
1 2 | 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 -MNext 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.
Share this Post


