Install Python2.6.4, mod_wsgi and Django on CentOS 5.4

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 -M

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.

Share this Post
  Facebook  Digg It  Delicious

Similiar Posts


5 comments

  1. Hi Venky, thanks for the helpful write up! Is is possible to complete this installation with the standard CentOS 5.4 mySQL installation? I’d rather not mess with my mySQL repos… Thanks again!

  2. Hi Marty, It can be done. All the steps will remain same except you can ignore items which need SQL update.

  3. Hi,

    Thanks for these excellent instructions.

    Most of it was pretty straight forward until I reached
    “Install MySQLdb package”

    Actually, MySQL was already installed, but when I ran

    python setup.py build

    it errored out starting with

    _mysql.c:35:23: error: my_config.h: No such file or directory

    and then continued on with a flurry of errors.

    I did not find my_config.h unfortunately, and I already had MySQL installed. But, just for grins, I went through the MySQL installation (possibly screwing things up worse, but at least these things can usually be straightened out without too much hassle.) But, in the end, I still got the my_config.h error….

    I then did the mysql step, ran into errors, then ran it with –skip-broken leaving out the mysql-server part (perhaps risking incompatability).

    Then I ran the python scripts with build and install and did the test. Voila, the my_config.h error went away, so on with the test, but the results were semi-buggy…

    >>> import MySQLdb
    /opt/python2.6/lib/python2.6/site-packages/MySQL_python-1.2.2-py2.6-linux-i686.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /opt/python2.6/lib/python2.6/site-packages/MySQL_python-1.2.2-py2.6-linux-i686.egg/_mysql.pyc, but /usr/local/src/MySQL-python-1.2.2 is being added to sys.path
    MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated
    from sets import ImmutableSet

    Right about now, I’m wondering if I should hide my name from view lest people say, “What an idiot”?

    But this is just a virtual private system I’m doing some setup practice on before actually setting up something for a production web server, so I have an excuse, sort of :-)

    I’m guessing I should probably upgrade mysql to make sure all the pieces are there. Right?

    Anyway, mysql and apache came automatically with the Centos 5 install. Yikes…lucky this installation is my own personal one. I still have to configure Bind9 and apache better for automatic subdomains and set up the whole jsp thing, and then finish configuring exim and setting up wordpress-mu, zope, plone, drupal.

  4. I was trying in many ways to istalled the python2.6 on my server and where am stuck is at the
    Add an alias to root’s .bash_profile:

    1 vi ~/.bash_profile
    2 alias python=’/opt/python2.6/bin/python’
    3 source ~/.bash_profile

    where a sceen open and then copy paiste but it does seem worng but then exit the program exicuting all follow lines as

    ln -s /opt/python2.6/bin/python /usr/bin/python2.6
    and the
    cat >> /etc/ld.so.conf.d/opt-python2.6.conf
    also beleave that the
    /opt/python2.6/lib (hit enter)
    (hit ctrl-d to return to shell)
    ldconfig is ok
    but then when typing at the prompt the python I still see this

    root@hosting [~]# cat >> /etc/ld.so.conf.d/opt-python2.6.conf
    /opt/python2.6/lib (hit enter)
    (hit ctrl-d to return to shell)
    root@hosting [~]# ldconfig
    root@hosting [~]# python
    Python 2.4.3 (#1, Sep 3 2009, 15:37:37)
    [GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>>
    and not as you said

    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

    where did it all go wrong and how can it be fixed as am still novice to this part

    thanks

  5. @alian-guy Write me a comment with your gtalk id. lets see if we can debug this.

    @dan – did u manage to get it working?

Leave a comment


0.370 secs - 9 db - 16 cache - 15.7MB