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;

  1. $ 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)
  1. $ 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;

  1. $ /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);

  1. $ wget http://www.djangoproject.com/download/1.0/tarball/
  2. $ tar xzvf Django-1.0.tar.gz
  3. $ cd Django-1.0
  4. $ 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;

  1. <VirtualHost *:80>
  2.         ServerName www.example.com
  3.         SetHandler python-program
  4.         PythonHandler django.core.handlers.modpython
  5.         SetEnv DJANGO_SETTINGS_MODULE mysite.settings
  6.         PythonDebug On
  7.         PythonPath "['/home'] + sys.path"
  8.         ErrorLog logs/example-error_log
  9.         CustomLog logs/example-access_log common
  10. </VirtualHost>
  11.  
  12. <VirtualHost *:80>
  13.         ServerName media.example.com
  14.         DocumentRoot /home/media/
  15. </VirtualHost>

also make sure to un-comment this line in the httpd.conf;

  1. 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;

  1. [mysqld]
  2. skip-networking
  3. 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.

7 Responses to “Getting Django up and running on Centos 5.2 with Apache(mod_python) and MySQL”

  1. Dirkjan Ochtman Says:

    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.

  2. Daniel Says:

    Django is available in the EPEL repository (http://fedoraproject.org/wiki/EPEL/FAQ#howtouse)…

    As simple as yum install Django ;-)

  3. Dougal Says:

    Last I checked it was only version 0.9.6. Not many people will want to use that with 1.0 being out…

  4. Daniel Says:

    You are right. But it has been updated to 1.0 recently.

  5. Dougal Says:

    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?

  6. Daniel Says:

    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

  7. Dougal Says:

    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.

Leave a Reply