Múltiples sitios usando Django y mod_wsgi en Apache
Frecuentes
Visto 3,259 veces
12
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
(y viceversa).
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.
Aquí está mi configuración:
A
is being served on port 8080
, B
is being served on port 80
.
VirtualHost:
<VirtualHost *:8080>
ServerAdmin x@x.net
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 x@x.net
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>
puertos.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
Como ves 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).
¡Gracias!
1 Respuestas
15
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")
a:
os.environ["DJANGO_SETTINGS_MODULE"] = "PROJECT_A.settings"
Similarly for other wsgi.py file.
Respondido 26 ago 12, 23:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas django apache mod-wsgi or haz tu propia pregunta.
By the way, this other pregunta features a practical example by Graham himself of using separate daemon process groups. Helped me a lot, you got my votes Graham! - Gerardo Yin
Don't assume you have the same problem and don't expect people to be mind readers. Create a separate question and detail exactly what your problem is. - graham dumpleton