使用 mod_wsgi 在 Apache 上部署多个 django 应用程序 [英] Deploying multiple django apps on Apache with mod_wsgi

查看:33
本文介绍了使用 mod_wsgi 在 Apache 上部署多个 django 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一主机上部署两个不同的 django 应用程序:第一个将对应于 url/site1,第二个将对应于 url/site2.这是我的配置:

<前>LoadModule wsgi_module modules/mod_wsgi.soWSGIScriptAlias/site1/var/www/py/site1/site1/wsgi.pyWSGIScriptAlias/site2/var/www/py/site2/site2/wsgi.pyWSGIPythonPath/var/www/py/site1:/var/www/py/site2<目录/var/www/py/site1/site1"><文件 wsgi.py>订单拒绝,允许所有人都允许<目录/var/www/py/site2/site2"><文件 wsgi.py>订单拒绝,允许所有人都允许

这里还有两个应用程序的 wsgi.py 文件

<前>导入操作系统导入系统路径 = '/var/www/py/site1'如果路径不在 sys.path 中:sys.path.append(path)os.environ.setdefault("DJANGO_SETTINGS_MODULE", "site1.settings")从 django.core.wsgi 导入 get_wsgi_application应用程序 = get_wsgi_application()

现在,这是我的问题.当我去我的服务器时,让我们说 http://app1.sites.gr/site1 它几次加载站点 1,有时它加载站点 2 !!!!当我访问 http://app1.sites.gr/site2 时也是如此......我得到了站点 1 的欢迎页面,有时我会看到站点 2 的欢迎页面!我正在按 F5 并获得不同的欢迎页面.我已经检查了前几个小时的所有内容,没有发现任何异常......

请在我发疯之前告诉我可能是什么问题......

谢谢!

解决方案

这是 Django 1.4 生成的 wsgi.py 文件的问题.即使在不同的子解释器中,尝试在同一进程中托管两个不同的 Django 实例也是行不通的.

变化:

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

到:

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

或者最好仍然使用守护进程模式并委托每个进程在不同的守护进程组中运行.

也就是说,而不是:

WSGIScriptAlias/site1/var/www/py/site1/site1/wsgi.pyWSGIScriptAlias/site2/var/www/py/site2/site2/wsgi.pyWSGIPythonPath/var/www/py/site1:/var/www/py/site2

使用:

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

<小时>

更新

请注意,现在有一篇关于此原因和其他原因的完整博客文章.

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 !

解决方案

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.

这篇关于使用 mod_wsgi 在 Apache 上部署多个 django 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆