25

I want to deploy two different django apps in the same host: The first will correspond to the url /site1 and the second to the url /site2. Here's my configuration:

LoadModule wsgi_module modules/mod_wsgi.so

WSGIScriptAlias /site1 /var/www/py/site1/site1/wsgi.py
WSGIScriptAlias /site2 /var/www/py/site2/site2/wsgi.py

WSGIPythonPath /var/www/py/site1:/var/www/py/site2

<Directory "/var/www/py/site1/site1">
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

<Directory "/var/www/py/site2/site2">
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

Also here's the wsgi.py file for both applications

import os
import sys

path = '/var/www/py/site1'
if path not in sys.path:
    sys.path.append(path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "site1.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Now, here's my problem. When I go to my server, let's say http://app1.sites.gr/site1 it some times loads site1, and some other times it loads site2 !!!! The same goes when I visit http://app1.sites.gr/site2 ... Sometiems I get the welcome page for site1, sometimes I get the welcome page for site2 ! I am hitting F5 and getting different welcome pages. I have checked everything for the previous hours and did not find anything strange...

Please, tell me what could be the problem before I go crazy ...

Thanks !

Serafeim
  • 14,962
  • 14
  • 91
  • 133

3 Answers3

51

This is a problem with the wsgi.py file generated by Django 1.4. It will not work where trying to host two distinct Django instances in the same process, even though in separate sub interpreters.

Change:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "site1.settings")

to:

os.environ["DJANGO_SETTINGS_MODULE"] = "site1.settings"

Or better still use daemon mode and delegate each to run in distinct daemon process groups.

That is, instead of:

WSGIScriptAlias /site1 /var/www/py/site1/site1/wsgi.py
WSGIScriptAlias /site2 /var/www/py/site2/site2/wsgi.py

WSGIPythonPath /var/www/py/site1:/var/www/py/site2

use:

WSGIDaemonProcess site1 python-path=/var/www/py/site1
WSGIScriptAlias /site1 /var/www/py/site1/site1/wsgi.py process-group=site1 application-group=%{GLOBAL}

WSGIDaemonProcess site2 python-path=/var/www/py/site2
WSGIScriptAlias /site2 /var/www/py/site1/site2/wsgi.py process-group=site2 application-group=%{GLOBAL}

UPDATE

Note that there is a whole blog post about this and other causes now.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • 2
    This actually worked, thank you ! However I have another problem now: When I try to visit both sites in the same browser, it sometimes logs me out from site2 (when I visit site1) - but not the opposite... Could this be because I have /site1 and /site2 ? Should I try what Erik proposes and have site1.sites.gr and site2.sites.gr ? – Serafeim Jul 17 '12 at 07:28
  • 4
    You need to set SESSION_COOKIE_NAME or SESSION_COOKIE_PATH differently for each site as they are under the same domain. See http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango – Graham Dumpleton Jul 18 '12 at 03:31
1

Your apps listen on the same port, and there doesn't seem to be a proxy that delegates them to different ones.

You either have to setup VirtualHosts within apache or use Nginx, lighttpd or something else to create a proper proxy

Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
  • Can't I delegate to the proper script through the different url (site1 vs site2) ???? I don't want to use different ports or configure a proxy ! I just want to run two django apps on the same apache host - I have read elsewhere that this is possible :( – Serafeim Jul 16 '12 at 13:58
  • So should I run my two sites using gunicorn on different ports and then just add the following "ProxyPass /site1 http://127.0.0.1:8111/ ProxyPassReverse /site1 http://127.0.0.1:8111/" for both sites on my httpd.conf ?? – Serafeim Jul 16 '12 at 15:09
  • I thought you just wanted to use apache, in that case use VirtualHosts http://httpd.apache.org/docs/2.2/vhosts/examples.html – Hedde van der Heide Jul 16 '12 at 15:13
  • Well, what i'd really like to do is what I ask... have sites.gr/site1 and sites.gr/site2 - each one pointing in its own django app... But from what I can understand is not possible :( – Serafeim Jul 16 '12 at 19:12
  • Its perfectly possible with apache using virtualhost I even send you an example only requiring some extra settings concerning static files and wsgi allocation – Hedde van der Heide Jul 16 '12 at 20:21
1

Graham Dumpleton's response is the one you probably want to read closest, but I would suggest saving yourself a lot of heartburn by hosting your two Djangos at the root of different subdomains rather than at non-root locations on the same domain. There are lots of gotchas for running non-root Django sites IMHO.

Good luck!

Erik
  • 7,479
  • 8
  • 62
  • 99
  • 2
    Well, after around 8 months of (not heavy) production use, with four different sites (/site1, /site2, site3 and site4), everything is working really great (yes, even sessions with the SESSION_COOKIE_NAME setting) ! The only thing to remember is to never use absolute urls but only through revese and {% url %} -- after all that's the django-DRY-way :) – Serafeim Feb 20 '13 at 21:02