Django + mod_wsgi.从 Apache 的 SetEnv 设置操作系统环境变量 [英] Django + mod_wsgi. Set OS environment variable from Apache's SetEnv

查看:28
本文介绍了Django + mod_wsgi.从 Apache 的 SetEnv 设置操作系统环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要拆分 Django 的开发和生产设置.我决定如果设置了 USKOVTASK_PROD 变量,那么应用程序应该使用生产设置.我阅读了这篇文章并尝试这样做.

I need to split Django's development and production settings. I decided that if USKOVTASK_PROD variable is set, then app should use production settings. I read this article and tried to do it.

我的片段:

/etc/apache2/sites-enabled/uskovtask.conf:

<VirtualHost *:80>

ServerName uskovtask.*.com
ServerAlias uskovtask.*.com
DocumentRoot /mnt/ebs/uskovtask


Alias /static /mnt/ebs/uskovtask/static/
<Directory /mnt/ebs/uskovtask/static>
    Require all granted
</Directory>

#WSGIPythonPath /mnt/ebs/uskovtask
WSGIDaemonProcess uskovtask.*.com python-path=/mnt/ebs/uskovtask:/usr/lib/python2.7/site-packages
WSGIProcessGroup uskovtask.*.com
WSGIScriptAlias / /mnt/ebs/uskovtask/uskovtask/wsgi.py
SetEnv USKOVTASK_PROD 1


<Directory /mnt/ebs/uskovtask/uskovtask>
<Files wsgi.py>
    Require all granted
</Files>
</Directory>

</VirtualHost>

wsgi.py:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "uskovtask.settings")

from django.core.wsgi import get_wsgi_application

_application = get_wsgi_application()

def application(environ, start_response):
    if 'USKOVTASK_PROD' in environ:
        os.environ.setdefault('USKOVTASK_PROD', environ['USKOVTASK_PROD'])
    return _application(environ, start_response)

settings.py 的部分:

import os

if 'USKOVTASK_PROD' in os.environ:
    from settings_prod import *
else:
    from settings_dev import *

但它总是导入 settings_dev 的设置.为什么?

But it always imports settings_dev's settings. Why?

推荐答案

这与问题有关 从 Django wsgi.py 文件访问 Apache SetEnv 变量

正如答案所说,您需要继承 WSGIHandler.

You need to inherit WSGIHandler as the answer says.

正如 Graham Dumpleton 在第二个答案中解释的那样,

As Graham Dumpleton explains in the second answer,

话虽如此,您提到的博文通常无济于事.这个是因为它使用了设置过程的讨厌技巧基于每个请求 WSGI 的每个请求的环境变量在 Apache 中使用 SetEnv 设置环境设置.这会导致各种多线程配置中的问题,如果环境变量可能因 URL 上下文而异.对于以下情况Django,它没有帮助,因为 Django 设置模块会通常在处理任何请求之前导入,这意味着当时环境变量不可用需要.

That all said, the blog post you mention will not usually help. This is because it is using the nasty trick of setting the process environment variables on each request based on the per request WSGI environ settings set using SetEnv in Apache. This can cause various issues in a multi threading configuration if the values of the environment variables can differ based on URL context. For the case of Django, it isn't helpful because the Django settings module would normally be imported before any requests had been handled, which means that the environment variables would not be available at the time required.

我认为这就是你的情况.

and I think this is what is happening in your case.

这篇关于Django + mod_wsgi.从 Apache 的 SetEnv 设置操作系统环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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