Sushi Kanji

CentOS 5.3 Installing Python 2.5, mod_wsgi and mod_rpaf

CentOS 5.x comes packaged with Python 2.4, if you’re developing software with another version of Python you’re going to want to do one of two things. You can either thoroughly test your application against Python 2.4 or you can install the same version of Python that the software was written with. In my case I’ve been using Python2.5 so I’ll be configuring my server to use that instead.

Installing the required packages

First we’re going to have to make sure we have all the development tools and libraries required to configure and compile our source code correctly.

sudo yum update
sudo yum groupinstall 'Development Tools' 'Development Libraries'
sudo yum install tk-devel libjpeg-devel mysql-devel python-devel httpd-devel zlib-devel bzip2-devel

Installing Python 2.5

Python 2.4 is an essential package for any CentOS 5.x deployment. In fact if you uninstall Python 2.4 yum will cease to work properly. So instead of replacing the default CentOS package let’s install Python 2.5 into the /opt directory, which historically has been used for installing optional software.

sudo mkdir /opt/src
cd /opt/src
sudo wget http://python.org/ftp/python/2.5.4/Python-2.5.4.tgz
sudo tar xzvf Python-2.5.4.tgz

Next we’ll be compiling Python with a few extra flags. The first —prefix tells the make file which directory to install Python in. The second —enable-thread will enable Python’s threading libraries for use with mod_wsgi which uses threading to serve http request concurently. The —enabled-shared flag will be used in compiling mod_wsgi allowing access to Python 2.5 shared libraries. And finally —enable-unicode allows us to enable unicode ucs4 instead of the default ucs2.

cd Python-2.5.4
sudo ./configure --prefix=/opt/python2.5 --enable-thread --enable-shared --enable-unicode=ucs4
sudo make
sudo make install

Creating a symbolic link from /opt/python2.5/bin/ to /usr/bin will allow us to call python2.5 from anywhere in our system without typing in the full path everytime.

sudo ln -s /opt/python2.5/bin/python2.5 /usr/bin/python2.5

Before we can build any software against Python2.5 shared libraries we’ll have to tell ld where to look for them. To do this we’ll add a simple configuration file in /etc/ld.so.conf.d

su
cat >> /etc/ld.so.conf.d/opt-python2.5.conf
/opt/python2.5/lib (hit enter)
(hit ctrl-d to return to shell)
/sbin/ldconfig
exit
ln -s /opt/python2.5/lib/libpython2.5.so /opt/python2.5/lib/python2.5/config

Installing virtualenv

We can install virtualenv without installing setuptools, however, if we don’t the virtualenv script will not be installed into /opt/python2.5/bin. So let’s just go ahead and install setuptools first.

cd /usr/local/src
sudo wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c9.tar.gz
sudo tar xzfv setuptools-0.6c9.tar.gz
cd setuptools-0.6c9
sudo python2.5 setup.py install

Installing virtualenv is pretty straight forward. The only thing I’ve added is the creation of a symlink from /opt/python2.5/bin to /usr/bin so we can all virtualenv-2.5 from anywhere in our system.

cd /usr/local/src
sudo wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.3.3.tar.gz
sudo tar xvfz virtualenv-1.3.3.tar.gz
cd virtualenv-1.3.3
sudo python2.5 setup.py install
sudo ln -s /opt/python2.5/bin/virtualenv /usr/bin/virtualenv-2.5

Installing mod_wsgi

Previously we setup ld to find Python 2.5’s shared libraries, we did this because mod_wsgi needs to be compiled against the version of Python that it will be using to server our Python applications. Let’s download the mod_wsgi source.

cd /usr/local/src
sudo wget http://modwsgi.googlecode.com/files/mod_wsgi-2.5.tar.gz
sudo tar xzvf mod_wsgi-2.5.tar.gz

Make sure to set the —with-python flag the Python 2.5 installation before compiling.

cd mod_wsgi-2.5
sudo ./configure --with-python=/opt/python2.5/bin/python
sudo make
sudo make install

And finally we’ll have to add the mod_wsgi module to the Apache config.

sudo vi /etc/httpd/conf/httpd.conf
LoadModule wsgi_module modules/mod_wsgi.so

While still in the apache configuration file we’ll also set WSGIPythonHome. We have to make sure WSGIPythonHome is set because we installed Python 2.5 side by side with Python 2.4, if we don’t mod_wsgi will assume it will be using Python 2.4 and cause errors. I’ve chosen to use /opt/python2.5 here because I like to install modules I will be using on every deployment into the main Python 2.5 site-packages folder. Alternatively you can setup a baseline virtual environment that does not have any additional python site-packages installed.

WSGIPythonHome /opt/python2.5

Installing mod_rpaf-2.0

This next step is only really needed if you’re going to be using a proxy server in front of Apache. Mod_rpaf takes the X-Forward-For header and passes along the original requests IP address to Apache, if not installed all IP addresses will show up as 127.0.0.1 your localhost’s ip address.

cd /usr/local/src
sudo wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
sudo tar xzf mod_rpaf-0.6.tar.gz

I had trouble using the make file so I used the Apache extension tool instead to compile the module.

cd mod_rpaf-0.6
sudo /usr/sbin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

Now we just need to add the mod_rpaf module to the apache config.

sudo vi /etc/httpd/conf/httpd.conf
LoadModule rpaf_module modules/mod_rpaf-2.0.so

Example Django deployment

First we’ll create a virtual environment for our application.

cd /var/www/vhosts/domain.com/
virtualenv-2.5 env-domain.com

Before we can install software into our virtual environtment we’ll have to activate it. After the virtual environment is activated you can install any python software you’d like exactly how you normally would.

source /var/www/vhosts/domain.com/env-domain.com/bin/activate

Next we’ll need to create a wsgi script for mod_wsgi to use when serving our application.

vi /var/www/vhosts/domain.com/projectname.wsgi

Add the following contents to the script

import os
import sys
import site

# path to domain specific virtual environment where python packages will be installed
site.addsitedir('/var/www/vhosts/domain.com/env-domain.com/lib/python2.5/site-packages')

sys.path.append('/var/www/vhosts/domain.com/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

And finally add the sample VirtualHost directive to your http.conf or wherever you keep your vhost settings.

# note port is set to 7000 for a proxy_pass configuration
# mod_rpaf is not needed if apache is run standalone.

<VirtualHost 127.0.0.1:7000>
    ServerName domain.com
    DocumentRoot /var/www/vhosts/domain.com/public
    WSGIScriptAlias / /var/www/vhosts/domain.com/djangoproject.wsgi

    RPAFenable On
    RPAFsethostname On
    RPAFproxy_ips 127.0.0.1
</VirtualHost>

Don’t forget to restart apache when you are finished.

    sudo /etc/init.d/http restart

Comments

1 Tim Selaty says...

I don[HTML_REMOVED]t even know how to thank you right now...

This article is amazing and helped me not only accomplish all my goals, but understand server installing / configuring on a more intimate level.

Thank you.

Posted at 7:55 p.m. on August 2, 2011