Django Installation on Apache

I moved my Django environment from development to production here. One positive note for Django -- it is relatively easy to do the migration, and, I am running on different database and servers between the two environment. I am using the built-in development web server in development and sqlite as the database. In production I am using Apache and Postgres. I will write up how I migrate data between the two databases with relative ease in a separate article.

Of course, there are still some steps that are needed to setup the initial production environment. Here are some notes:

Postgres

I need to disable "ident" style authentication for the Postgres database connection between Apache's mod_python and Postgres. The normal/default postgres installation expects database connections made from normal linux users using OS authentication. Instead, from mod_python + Apache user, we need to enable plain user id + password authentication:

  • edit the postgres hba authentication file. On my SUSE system it is at: /var/lib/pgsql/data/pg_hba.conf
  • Add a line for my application database:
    local <my_database> all password
  • Leaving the existing catch all for the other databases:
    local all all ident sameuser
  • to reload this file, use pg_ctl reload (as postgres user)

Setup mod_python

Add the following block to your host file:

<IfModule mod_python.c>

<Location />
     SetHandler python-program
     PythonHandler      django.core.handlers.modpython
     SETEnv DJANGO_SETTINGS_MODULE desk.settings
     PythonDebug On
     PythonPath "['my_project_root'] + ['my_app_root'] + sys.path"
</Location>

<Location "/media">
     SetHandler None
</Location>

<LocationMatch "\.(jpg|gif|png)$">
      SetHandler None
</locationMatch>

</IfModule>

The first location block setups the python module to run Django. One thing of note is the setting of PythonPath. You should add BOTH your project root and your application root to the path. Otherwise you will have to reference the project name inside your applications, which is a no-no.

Linking Django admin files

Finally, don't forget to make a link to make the Django admin system works. You need to create a link in your apache document root to the installed Django admin file directory. e.g.:

cd my_doc_root
ln -s /usr/lib/python/site-packages/django/contrib/admin/media media

Note that Django uses the name "media" to reference those files. Since most likely you want to put your static files in the same directory structure, you have to name your static file directory something other than Django.