Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple sites using Django and mod_wsgi on Apache

I'm currently using two Django applications (say A & B) hosted on the same domain (but being served on different ports) through Apache. I believe my setup is correct, but I'm randomly getting 500s on both the sites. The 500 on A (say for example) occurs mostly after a request has been served on B (and vice versa).

Upon inspecting the error log (of say A for instance), I see that A's wsgi module is trying to access B's settings.py file (which obviously doesn't happen to be there, since the project paths are different) [and this does happen the other way too, B's wsgi raises an exception complaining of missing A's settings.py file]. I'm not sure why they'd look for the other settings file, the imports (for settings.py) on all views are specific to the respective projects.

Here's my setup:

A is being served on port 8080, B is being served on port 80.

VirtualHost:

<VirtualHost *:8080>

  ServerAdmin [email protected]
  ServerName  string1

  Alias /static/ /home/PATH_TO_PROJECT_A/static/

  <Directory /home/PATH_TO_PROJECT_A/static>
  Order deny,allow
  Allow from all
  </Directory>

  WSGIScriptAlias / /home/PATH_TO_PROJECT_A/wsgi.py

  <Directory /home/PATH_TO_PROJECT_A>
  <Files wsgi.py>
  Order deny,allow
  Allow from all
  </Files>
  </Directory>


  LogLevel warn
  ErrorLog  /SOME_PATH/errorA.log
  CustomLog /SOME_PATH/accessA.log combined
</VirtualHost>


<VirtualHost *:80>
  ServerName string1
  ServerAdmin [email protected]

  Alias /APP_B/static/ /home/PATH_TO_PROJECT_B/static/

  <Directory /home/PATH_TO_PROJECT_B/static>
  Order deny,allow
  Allow from all
  </Directory>

  WSGIScriptAlias /APP_B /home/PATH_TO_PROJECT_B/wsgi.py/

  <Directory /home/PATH_TO_PROJECT_B>
  <Files wsgi.py>
  Order deny,allow
  Allow from all
  </Files>
  </Directory>

  ErrorLog /home/SOME_PATH/error2.log
  CustomLog /home/SOME_PATH/access2.log combined

  # All other files on B:80 (other than /APP_B are served normally
  DocumentRoot /home/foo/public_html/xyz/public

</VirtualHost>

ports.conf:

NameVirtualHost *:8080
Listen 8080
Listen 80

<IfModule mod_ssl.c>
 Listen 443
</IfModule>

<IfModule mod_gnutls.c>
 Listen 443
</IfModule>

wsgi.py on 'A':

import os, sys

sys.path.append('home/PATH_TO_PROJECT_A') #1

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT_A.settings") #2

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

And exactly the same on B, with changes to line #1 and #2.

Error I get from the error.log say for instance from A:

 [Sun Aug 26 17:01:49 2012] [error] [client x] Traceback (most recent call last):
 [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 219, in __call__
 [Sun Aug 26 17:01:49 2012] [error] [client x]     self.load_middleware()
 [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 39, in load_middleware
 [Sun Aug 26 17:01:49 2012] [error] [client x]     for middleware_path in settings.MIDDLEWARE_CLASSES:
 [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 184, in inner
 [Sun Aug 26 17:01:49 2012] [error] [client x]     self._setup()
 [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
 [Sun Aug 26 17:01:49 2012] [error] [client x]     self._wrapped = Settings(settings_module)
 [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 95, in __init__
 [Sun Aug 26 17:01:49 2012] [error] [client x]     raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
 [Sun Aug 26 17:01:49 2012] [error] [client x] ImportError: Could not import settings 'PROJECT_B.settings' (Is it on sys.path?): No module named PROJECT_B.settings

As you see A's error log complains that B's settings.py is missing. Please shed some light, I've no idea what's wrong. I don't see why one app would look for the other's settings.py file to import?

Both the applications work and execute as expected, but they do break at random requests serving the 500 (which gets cleared if I refresh again).

Thank you!

like image 377
MrMuddy Avatar asked Aug 26 '12 22:08

MrMuddy


People also ask

What is mod_wsgi in Apache?

mod_wsgi is an Apache HTTP Server module by Graham Dumpleton that provides a WSGI compliant interface for hosting Python based web applications under Apache. As of version 4.5. 3, mod_wsgi supports Python 2 and 3 (starting from 2.6 and 3.2).

Can I use Django with Apache?

Django will work with any version of Apache which supports mod_wsgi. The official mod_wsgi documentation is your source for all the details about how to use mod_wsgi. You'll probably want to start with the installation and configuration documentation.

Can you have two Django apps on one database?

Multiple Apps can point to one database. What happens is that django creates different tables in the database. You received this message because you are subscribed to the Google Groups "Django users" group.


1 Answers

Django breaks the geneated wsgi.py for running multiple Django instances in same process in different sub interpreters. Either use mod_wsgi daemon mode and delegate each to a separate daemon process group, is better anyway, or change:

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

to:

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

Similarly for other wsgi.py file.

like image 110
Graham Dumpleton Avatar answered Sep 22 '22 05:09

Graham Dumpleton