Saturday, August 7, 2010

Django - Deploy an application

Once we have a Django application finished, we probably want to put it in a "real" web server.

This page explains how we can do it, using Apache and mod_wsgi, a module that allows Apache to support any Python application which supports the Python WSGI interface, like Django. We briefly summarize the steps in this post.
sudo aptitude install libapache2-mod-wsgi
  • We can test it by creating a wsgi script and modifying the Apache configuration file httpd.conf to allow the access to this file. For example we can make an script called /usr/var/www/scripts/myapp.wsgi
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'

response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)

return [output]


and adding some commands to the httpd.conf file, found in Ubuntu inside /etc/apache2 folder.
  • To deploy our Django application, we must create a file called django.wsig inside a folder, like mysite/apache, with the following content:
import os
import sys

sys.path.append("django_folder_application_path")
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

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

and the httpd.conf file must have the content explained in this page.

  • We must take into accout several things:
    • Make sure the folders have the correct permissions. Specifically, they must have read and execute permissions for the www-data user, the apache server user.
    • If we are using sqlite databases, the database file and its directory must have writing permissions, as it is explained here. Also we must ensure that the db name contains the full path.

No comments:

Post a Comment