Check out the Latest Articles:
A working copy, Django, PIL, Imaging, libjpeg, mysql, git, apache, mod_wsgi, easy_install, pip, django-imagekit, cdn storage on CentOS 5.4

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
yum groupinstall “GNOME Desktop Environment”
yum groupinstall “Development Tools”

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.

Add New User

groupadd webmaster
useradd -c “username”  -g webmaster -m -s /bin/bash username
passwd username

Near line 77 where it reads “root ALL=(ALL) ALL add
username ALL=(ALL) ALL

From here you can change to your user to leave the root account as is.
If you find that some of the item do not want to run you should use the sudo command

Install Python 2.6 along side of Python 2.4

The trick to getting Python 2.6 working on CentOS 5.4 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.

tar -zxvf Python-2.6.4.tgz
cd Python-2.6.4
./configure --prefix=/opt/python2.6 --with-threads --enable-shared --with-zlib=/usr/include
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/ /usr/lib
ln -s /opt/python2.6/lib/ /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/ /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

tar -zxvf MySQL-python-1.2.3c1.tar.gz
cd MySQL-python-1.2.3c1
python build
python install
cd ..

tar -zxvf Django-1.1.1.tar.gz
cd Django-1.1.1
python build
python 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.

tar -zxvf mod_wsgi-2.8.tar.gz
cd mod_wsgi-2.8
./configure --with-python=/usr/local/bin/python
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/lib64/httpd/modules/
line 787
AddHandler wsgi-script .wsgi

Make sure your Apache restarts once the module has been loaded

/etc/init.d/httpd restart

Create a separate directory for your virtual hosts

I like to house all my vhosts in a seperate directory

cd /etc/httpd/conf
mkdir vhosts
cd vhosts

ServerName localhost

ErrorLog /www/django_test1/mysite/logs/error_log
CustomLog /www/django_test1/mysite/logs/access_log combined

UseCanonicalName Off

Alias /media/ "/www/django_test1/mysite/media/"
<Directory "/www/django_test1/mysite/media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing

Alias /mediaadmin/ "/opt/python2.6/lib/python2.6/site-packages/django/contrib/admin/media/"
<Directory "/opt/python2.6/lib/python2.6/site-packages/django/contrib/admin/media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing

WSGIScriptAlias / /www/django_test1/mysite.wsgi
WSGIDaemonProcess mysite processes=7 threads=1 display-name=%{GROUP}
WSGIProcessGroup mysite
WSGIApplicationGroup %{GLOBAL}

Create your mod_wsgi file

According to a few searches I have done online it is best practive to leave your mod_wsgi file in the parent folder of the app.

mkdir /www
cd /www
mkdir django_test1
cd django_test1
vi mysite.wsgi


import os, sys

sys.path.insert(0, '/www/django_test1')
sys.path.insert(0, '/www/django_test1/mysite')

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
os.environ['PYTHON_EGG_CACHE'] = '/www/django_test1/mysite/.python-eggs'

import django.core.handlers.wsgi

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

Save the file

Create a .python-eggs directory

in mysite

mkdir .python-eggs
chmod -R 777 .python-eggs

Start the mysql server

/etc/rc.d/init.d/mysqld start

Install PIL on CENTOS

Install PIP in order to use Imaging

/opt/python2.6/bin/easy_install-2.6 pip

/opt/python2.6/bin/pip install

Install GIT

rpm -Uvh

yum install git

Configure MySQL with another user

mysql --user=root mysql
CREATE USER 'webdev'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'webdev'@'localhost'

Create a Django project

/opt/python2.6/bin/ startproject mysite

Create a logs directory in your project as this is needed for mod_wsgi and apache

cd mysite
mkdir logs

Symlink your admin media folder

mkdir media
chmod -R 777 media
mkdir mediaadmin

ln -s /opt/python2.6/lib/python2.6/site-packages/django/contrib/admin/media /www/django_test1/mysite/mediaadmin/

Configure your /etc/httpd/conf/httpd.conf file to include your projects base settings

ServerAdmin admin@appelvm01.centos
HostName appelvm01.centos


DocumentRoot "/www/django_test1/mysite"

<Directory "/www/django_test1/mysite">
AllowOverride None
Options None
Order allow,deny
Allow from all

# include VirtualHosts config files
Include conf/vhosts/*.conf

add WSGISocketPrefix to httpd.conf end of file

WSGISocketPrefix /var/run/wsgi

Make sure hostname is equal to the one in Apache conf /etc/httpd/conf/httpd.conf
else change by running

hostname "new_name"

Give permissions

chcon -R -h -t httpd_sys_content_t /www/django_test1/mysite

I found this wasnt good enough so I had to turn off SElinux

vi /etc/selinux/config



According to a blog remove your -Indexes

vi /etc/httpd/conf.d/welcome.conf
# -Indexes

Configure Firewall

sudo vi /etc/sysconfig/iptables-config
Make sure following variables are set to “yes”:

Open Web ports:

sudo /sbin/iptables -I INPUT -p tcp -m state --state NEW,ESTABLISHED --dport 80 -j ACCEPT
sudo /sbin/iptables -I INPUT -p tcp -m state --state NEW,ESTABLISHED --dport 443 -j ACCEPT

Restart iptables with:
sudo /sbin/service iptables stop
sudo /sbin/service iptables start

Make sure rules are still in. Try restarting server, too, to make sure setting picked up.

Make sure your MySQL and Apache are always on after restart

/sbin/chkconfig --add httpd
/sbin/chkconfig --level 2345 httpd on
/sbin/chkconfig --list httpd

/sbin/chkconfig --add mysqld
/sbin/chkconfig --level 2345 mysqld on
/sbin/chkconfig --list mysqld

Install South through easy_install to allow for easy db migration

/opt/python2.6/bin/easy_install-2.6 South

Install Django-cumuls for Rackspace/CDN storage

/opt/python2.6/bin/pip install django-cumulus

/opt/python2.6/bin/pip install -r

/opt/python2.6/bin/easy_install-2.6 django-imagekit

You should now be able to go to your servers ip address, and you should see a welcome screen!

From here on you can start developing your app, you should be able to enable your admins autodiscover and set the urls so that you can access the admin section.

  1. physical therapist on Thursday 8, 2010

    nice post. thanks.

  2. eJournal on Thursday 8, 2010

    it was very interesting to read.
    I want to quote your post in my blog. It can?
    And you et an account on Twitter?

  3. Arsenij2010 on Thursday 8, 2010

    I would like to exchange links with your site
    Is this possible?

  4. Best Credit Card Rate on Thursday 8, 2010

    it was very interesting to read.I want to quote your post in my blog. It can?And you et an account on Twitter?

  5. WP Themes on Thursday 8, 2010

    Good dispatch and this fill someone in on helped me alot in my college assignement. Thanks you seeking your information.

  6. [...] approach I believe is best without a little assistance. one tutorial says add the following links…on-centos-5-4/ ln -s /opt/python2.6/lib/ /usr/lib ln -s /opt/python2.6/lib/ [...]

  7. emetib on Thursday 8, 2010

    regarding wsgi install

    ./configure –with-python=/usr/local/bin/python

    AND not
    ./configure –with-python=/opt/python2.6/bin/python


  8. Arno Smit on Thursday 8, 2010

    Just after our Python installation we create a few symlinks. and essentially those two are the same.