I recently completed a
Django 1.1 website that was released on CentOS 5.2.
CentOS is a great enterprise Linux distro based on
Red Hat but if you know anything about enterprise distros is that they never contain the latest and greatest features. In order to take advantage of new frameworks and servers you will need to build and add them yourself. Most of the time this isn't too hard. For example, you can build and update PHP to 5.3 (the default is 5.2+) without any issue. Python, on the other hand, isn't as straight forward.
CentOS 5 comes with Python 2.4 and this can be an issue if you want to build a django app because django requires at least Python 2.5. This means that you can't just yum install/update to get all the pieces you need for your app to work. Here are the steps that worked for me and I hope that they might help you if you find yourself in a similar situation.
This is not a tutorial of how to setup a django app with Apache but instead it's intended to show you how to install and setup the packages necessary to run a django app on CentOS 5+. This tutorial will also assume that you know how to wget, untar, and make the files you will be downloading and installing.
Need to install with the power of root
Now you don't need be root to go through these steps but you need to at least have a user account that is able to sudo root level commands. I find it easier to either login or su as root to make system level changes and then go back out to a normal user account for everything else instead of adding sudo in front of every command.
Install Apache, MySQL, SQLite, and other developer packages
yum install httpd-devel mysql mysql-server
yum install mysql-client mysql-devel
yum install apr-devel sqlite3 sqlite-devel
yum install libjpeg libpng gcc make autoconf
yum install libxslt gettext zlib-devel
This tutorial will assume that you are using MySQL on production and you may also use SQLite for development. If you are using another database like PostgreSQL then you should install those files now. Also, please note that these are not the only packages that you will need for your webserver, this should just be considered a minimum to get your django app working.
Install Python 2.6 along side of Python 2.4
The trick to getting Python 2.6 working on CentOS 5.2 without breaking anything is to install it along side of the default Python 2.4. Do not try to update your default Python 2.4 because important CentOS modules require 2.4. To do this you will configure python to be installed in /opt/python2.6. Before you do this step make sure that you have yum installed sqlite-devel because python will look for the sqlite header files in order to build the module for it when you compile python.
cd Python-2.6.4
./configure --prefix=/opt/python2.6 --with-threads --enable-shared --with-zlib=/usr/include
make
make install
cd ~
After Python is installed in /opt/Python2.6 you need to create symbolic links to it.
ln -s /opt/python2.6/lib/libpython2.6.so /usr/lib
ln -s /opt/python2.6/lib/libpython2.6.so.1.0 /usr/lib
ln -s /opt/python2.6/bin/python /usr/local/bin/python
ln -s /opt/python2.6/bin/python /usr/bin/python2.6
ln -s /opt/python2.6/lib/python2.6.so /opt/python2.6/lib/python2.6/config/
Run the ldconfig to update the links to your shared libraries and then check your python version to make sure you can now use 2.6. Also, test to see if there are any conflicts between Python 2.4 and 2.6 using yum. If you do have problems then double check your symbolic links and make sure that you are not conflicting with /usr/bin/python or /usr/bin/python2.4.
/sbin/ldconfig -v
python -V
yum info httpd
Install setuptools, MySQL extension, and Django 1.1
If you previously installed setuptools but you will need to do again for 2.6 so that the packages are installed in /opt/python2.6. In order to use MySQL with Django you will need to download, build, and install the extension for it.
chmod u+x setuptools-0.6c11-py2.6.egg
./setuptools-0.6c11-py2.6.egg --prefix=/opt/python2.6
cd ~
cd MySQL-python-1.2.3c1
python setup.py build
python setup.py install
cd ~
cd Django-1.1.1
python setup.py build
python setup.py install
cd ~
Setup and Install mod_wsgi
A majority of the Django community uses wsgi as the way to interface with the webserver. I also prefer to use it because it's fast and very easy to setup. This tutorial will not go into setting up your app to use wsgi (however, I will give an example at the end), but the django project has a good tutorial for this. Please note that this step is very important. You may have previously installed mod_wsgi using yum but you will need to download it and build it using your new 2.6 in order for it to work with your app.
cd mod_wsgi-2.8
./configure --with-python=/usr/local/bin/python
make
make install
cd ~
Then you will need to edit your Apache httpd.conf in order to load the mod_wsgi module. After this has been completed restart Apache.
vi /etc/httpd/conf/httpd.conf
>> LoadModule wsgi_module /usr/lib/httpd/modules/mod_wsgi.s
That's it. You should be good to go as long as you setup your app with a .wsgi file and setup your app in your Apache config. For security reasons, I prefer to setup my django apps using virual hosts in /opt/vhosts/app-name and setup the config in /etc/httpd/conf.d/vhosts.conf. Here is an example to get you started:
<VirtualHost *:80>
DocumentRoot "/opt/vhosts/app-name"
ServerName app-name.com
WSGIScriptAlias / /opt/vhosts/app-name/django.wsgi
</VirtualHost>
Here is an example of my django.wsgi file that I use in my apps.
import os
import sys
sys.path.append('/opt/vhosts/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'app-name.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp/.python-eggs'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()