How to use django-cms through wsgi

This setup should work on apache 2.x using mod_wsgi. It needs mod_rewrite to map the Request to the wsgi script, though this could be done alternativly using an (wsgi)alias.

Besides your project, which you probably already have in place, wsgi needs a wsgi enabled VHost and a script, that instanciates the django WSGIHandler.

Before we look into the Details here is the Directory structure i used:

/example.com
   /lib/myproject
   /htdocs/app.wsgi

The WSGI Script

This is the script that offers the WSGIHandler. It first adds the Directory where our project lives to the python path. And then sets DJANGO_SETTINGS_MODULE to the projects settings file. Then the WSGIHandler is started and serves the Project.

app.wsgi

  import sys, os
  sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../lib/'))
  os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

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

The Virtual Host

The DocumentRoot? is the Folder where your wsgi script is placed. The user and group parameters to WSGIDaemonProcess should be filled with apropirate values. E.g. run as an unprivileged user that is able to access the scripts.

  <VirtualHost *:80> 
       ServerName example.com
       ServerAdmin someone@somewhere.com

     DocumentRoot "/path/to/your/wsgi/script"
     <Directory "/path/to/your/wsgi/script">
         Options +ExecCGI
         Order allow,deny
         Allow from all
     </Directory>

     WSGIDaemonProcess mysite user=myuser group=myuser processes=2 threads=5
     WSGIProcessGroup mysite

     AddHandler wsgi-script .wsgi

     RewriteEngine On
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule ^(.*)$ /app.wsgi/$1 [QSA,L]
</VirtualHost>

The Rewrite Rules map every Request to the app.wsgi script which has to be in DocumentRoot? and then serves the project.


Alternative: Yet another mod_wsgi VirtualHost? config. Does not use mod_rewrite.

<VirtualHost *>
  DocumentRoot /path/to/app/public
  
  # Sends all traffic through Django
  # WSGIScriptAlias / /path/to/app/app.wsgi
  # Sends all traffic through Django, excluding any url starting with "/static/"
  WSGIScriptAliasMatch ^/(?!(static/)) /path/to/app/app.wsgi
  
  # Aliases for all static files
  Alias /static/media /path/to/app/public/media
  Alias /static/admin/media /path/to/django/contrib/admin/media
</VirtualHost>

It should be noted that DocumentRoot really means nothing here. The WSGIScriptAliasMatch directive sends all traffic through django anyway, except any url that begins with /static/, which get aliased through to the paths provided.

Then make these adjustments to your main settings.py:

MEDIA_ROOT = '/path/to/app/public/media'
MEDIA_URL = '/static/media/'
ADMIN_MEDIA_PREFIX = '/static/admin/media/'