Difference between revisions of "Django/Apache"

From Jon's Wiki
(mod_python considered harmful.)
Line 2: Line 2:
  
 
== Django Requirements ==
 
== Django Requirements ==
 +
 
* Python >= 2.4
 
* Python >= 2.4
 
* Apache 2
 
* Apache 2
* mod_python - Apache module to support Python code, similar to mod_perl
+
* mod_wsgi - Apache module for Python apps
 
* PostgreSQL >= 8.0
 
* PostgreSQL >= 8.0
 
* python-psycopg2 - PostgreSQL client library for Python
 
* python-psycopg2 - PostgreSQL client library for Python
 
* python-imaging  - Python Imaging library
 
* python-imaging  - Python Imaging library
* memcached and python-memcache ''(optional but reccommended)''
+
* memcached and python-memcache ''(optional but recommended)''
 +
 
 
== Preparation ==
 
== Preparation ==
  
Django can be installed from the Debian packaged version, or fetched with SVN or with git from the [http://git.catalyst.net.nz/gitweb?p=django.git Catalyst Django git mirror]. and mod_python and psycopg2 can be simply installed using apt-get:
+
Django can be installed from the Debian packaged version, and mod_wsgi and psycopg2 can be simply installed using apt-get:
  
  apt-get install python-django libapache2-mod-python python-psycopg2
+
  apt-get install python-django libapache2-mod-wsgi python-psycopg2
  
Also, check the mod_python Apache module is enabled:
+
Check the mod_python Apache module is enabled:
  
  a2enmod mod_python
+
  a2enmod wsgi
  
 
== Example Apache VHost configuration ==
 
== Example Apache VHost configuration ==
  
  <VirtualHost * >
+
  <VirtualHost *:80>
    DocumentRoot /var/www/django/clientsite
+
  ServerName www.clientsite.co.nz
    ServerName www.clientsite.co.nz
 
    ServerAlias clientsite.co.nz
 
 
    # Enable mod_python handler and django config
 
    <Location "/">
 
        SetHandler python-program
 
        PythonHandler django.core.handlers.modpython
 
        PythonPath "['/var/www/django'] + sys.path"
 
        SetEnv DJANGO_SETTINGS_MODULE clientsite.settings
 
        PythonDebug Off
 
    </Location>
 
 
   
 
   
    # disable mod_python for static content - adjust as required
+
  # static content (for now, but separate domain best)
    <Location "/media">
+
  AliasMatch /([^/]*\.(gif|png|jpg)) ''/path/to/myproject'''/static/$1
        SetHandler None
+
  <Directory ''/path/to/myproject''/static>
    </Location>
+
    Order deny,allow
+
     Allow from all
    # some variation on the following - omit or adjust as required
+
  </Directory>
    <LocationMatch "\.(jpg|gif|png)$">
 
        SetHandler None
 
     </LocationMatch>
 
 
   
 
   
 +
  WSGIScriptAlias / ''/path/to/myproject''/django.wsgi
 
  </VirtualHost>
 
  </VirtualHost>
  
This config enables the mod_python handler for the root / URL, and disables it for the media subdirectory and image URLs. Features to note:
+
== Example WSGI file ==
  
* The ''PythonPath'' setting. Note that the path added here is the '''parent''' directory of the document root.
+
This would go in the <tt>''/path/to/myproject''/django.wsgi</tt> file, as specified in the Apache WSGIScriptAlias directive.
* The ''DJANGO_SETTINGS_MODULE'' environment variable
 
* ''PythonDebug'' - should be explicitly set to "off"
 
  
== Static Content ==
+
import os
 
+
import sys
If you are using the django admin interface from the Debian python-django package, you will need to create a symbolic link to the admin media folder in the clientsite directory, for example:
+
 
+
# You may also need to add the Django path
  ln -s /usr/share/python-support/python-django/django/contrib/admin/media /var/www/django/clientsite/media
+
  path = "''/path/to/myproject''"
 
+
if path not in sys.path:
Alternitavely one could configure the Location directive to fetch content from the original path.
+
    sys.path.append(path)
 
+
For chunky sites, have all the static content served from a separate box using a lightweight HTTP server (lighttpd).
+
os.environ["DJANGO_SETTINGS_MODULE"] = "''myproject''.settings"
 
+
import django.core.handlers.wsgi
== External Links ==
+
application = django.core.handlers.wsgi.WSGIHandler()
* [http://www.djangoproject.com/documentation/ Django documentation] - [http://www.djangoproject.com/documentation/modpython/ "How to use Django with mod_python"].
 
* The [http://www.djangoproject.com/ Django Project] home page
 
* [http://code.djangoproject.com/ Django Trac] bug/ticket tracking.
 
 
 
 
 
[[Category:Django]] [[Category:Python]] [[Category:Apache]]
 

Revision as of 02:37, 24 October 2014

Setting up Django on Apache 2 is relatively straightforward, but with a few kinks noted herewith.

Django Requirements

  • Python >= 2.4
  • Apache 2
  • mod_wsgi - Apache module for Python apps
  • PostgreSQL >= 8.0
  • python-psycopg2 - PostgreSQL client library for Python
  • python-imaging - Python Imaging library
  • memcached and python-memcache (optional but recommended)

Preparation

Django can be installed from the Debian packaged version, and mod_wsgi and psycopg2 can be simply installed using apt-get:

apt-get install python-django libapache2-mod-wsgi python-psycopg2

Check the mod_python Apache module is enabled:

a2enmod wsgi

Example Apache VHost configuration

<VirtualHost *:80>
  ServerName www.clientsite.co.nz

  # static content (for now, but separate domain best)
  AliasMatch /([^/]*\.(gif|png|jpg)) /path/to/myproject'/static/$1
  <Directory /path/to/myproject/static>
    Order deny,allow
    Allow from all
  </Directory>

  WSGIScriptAlias / /path/to/myproject/django.wsgi
</VirtualHost>

Example WSGI file

This would go in the /path/to/myproject/django.wsgi file, as specified in the Apache WSGIScriptAlias directive.

import os
import sys

# You may also need to add the Django path
path = "/path/to/myproject"
if path not in sys.path:
    sys.path.append(path)

os.environ["DJANGO_SETTINGS_MODULE"] = "myproject.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()