Getting Django up and running on Centos 5.2 with Apache(mod_python) and MySQL
This is partly for my reference (doing it the second time just now and I forgot everything!) but hopefully it will be helpful for others too.
Disclaimer; I am by no means a Linux expert. This is just how I got it working… If there is a better, easier or wiser way please let me know!
Let’s assume you have Yum installed (I didn’t on my VPS - but my provider installed it for me).
First, make sure you are up to date;
-
$ yum update
These are the packages I installed (httpd and python are already installed);
- mod_python (Apache uses this to run Python)
- mysql-server
- mysql-devel (easy_setup needs this)
- python-devel (easy_setup needs this)
- python-setuptools (so we have easy_setup)
- gcc (easy_setup needs this)
-
$ yum install mod_python mysql-server mysql-devel python python-devel python-setuptools gcc
At this point I would have added MySQL-python to this list, however the default repo’s I have only give me version 1.2.1 and Django needs 1.2.1p2 or later. The easiest way I found to get this was with easy_install but it requires a few other packages as mentioned above.
After that is finished you can go on to install MySQL-python by doing this;
-
$ /usr/bin/easy_install MySQL-python
This should get you version 1.2.2. Perfect. Now, if all went well you should now be almost ready to rock. All that is left is getting Django and doing some config, so follow the instructions here. Replicated below for convenience (but you should really check the website… i.e. a new version might be out);
-
$ wget http://www.djangoproject.com/download/1.0/tarball/
-
$ tar xzvf Django-1.0.tar.gz
-
$ cd Django-1.0
-
$ sudo python setup.py install
Now we have everything we need, just do a bit of set-up to do. I’m going to show you how to set this up as an Apache virtual host. It’s fairly simple, thankfully;
-
<VirtualHost *:80>
-
ServerName www.example.com
-
SetHandler python-program
-
PythonHandler django.core.handlers.modpython
-
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
-
PythonDebug On
-
PythonPath "['/home'] + sys.path"
-
ErrorLog logs/example-error_log
-
CustomLog logs/example-access_log common
-
</VirtualHost>
-
-
<VirtualHost *:80>
-
ServerName media.example.com
-
DocumentRoot /home/media/
-
</VirtualHost>
also make sure to un-comment this line in the httpd.conf;
-
NameVirtualHost *:80
In the above config you will need to change the server name to your domain. Change the PythonPath to where you have your Django project. The above config would be for a website residing in /home/mysite/ and the settings file in /home/mysite/settings.py
The media has been configured to run off a sub-domain as recommended in the Django docs. Django should not serve static media, it just eats up resources. Ideally media should be on another server, even before you move the database to another server… You can read some more about this stuff here along with a generic installation guide (rather than centos specific)
I also changed the default storage engine in MySQL to innodb as I’d discussed in a previous post and while I was in there I added the option ’skip-networking’ so the top lines on my my.cnf looked like this;
-
[mysqld]
-
skip-networking
-
default-storage_engine = innodb
This basically means Django will use innodb and skip-networking stops mySQL opening a port to the outside world. I only want to access it from inside this machine.
Now you arer ready to go serve a Django project!
Thanks to the folk #django for help with this.

October 9th, 2008 at 8:42 pm
You should really start using mod_wsgi rather than mod_python. It’s faster, has very good support from the author (whereas mod_python is nearly dead) and is actually fairly Pythonic in its support for WSGI.
October 14th, 2008 at 7:19 pm
Django is available in the EPEL repository (http://fedoraproject.org/wiki/EPEL/FAQ#howtouse)…
As simple as yum install Django
October 15th, 2008 at 12:21 am
Last I checked it was only version 0.9.6. Not many people will want to use that with 1.0 being out…
October 15th, 2008 at 8:29 am
You are right. But it has been updated to 1.0 recently.
October 15th, 2008 at 1:34 pm
That’s quite cool, it must make some of it easier. However, I guess it doesn’y install the MySQL-python as a dependancy does it? Since you could use that or you could use postgres.
In fact, I guess all it does it give you Django and the appropriate python packages?
October 16th, 2008 at 2:23 pm
That’s the deplist :
package: Django.noarch 1.0-1.el5
dependency: python(abi) = 2.4
provider: python.i386 2.4.3-21.el5
dependency: /usr/bin/env
provider: coreutils.i386 5.97-14.el5
dependency: /usr/bin/python
provider: python.i386 2.4.3-21.el5
You would indeed have to install the other packages with yum install MySQL-python and|or python-psycopg2
October 16th, 2008 at 2:25 pm
True but that then goes back to the problem I had with MySQL-python not having an up-to-date enough version in the repo’s I had.
I didn’t really know where to find better repo’s or what repos i could find and therefore found it with easy_install and that worked great.