从 Django wsgi.py 文件访问 Apache SetEnv 变量 [英] Access Apache SetEnv variable from Django wsgi.py file

查看:31
本文介绍了从 Django wsgi.py 文件访问 Apache SetEnv 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正像广泛建议的那样,尝试将 Django 的密钥和数据库传递分离到环境变量中,以便我可以在本地/生产服务器之间使用相同的代码库.

I'm trying to separate out Django's secret key and DB pass into environmental variables, as widely suggested, so I can use identical code bases between local/production servers.

我遇到的问题是在运行 Apache + mod_wsgi 的生产服务器上正确设置然后读取环境变量.

The problem I am running into is correctly setting and then reading the environmental vars on the production server running Apache + mod_wsgi.

我的用户配置文件中设置的变量不可用,因为 Apache 不是以该用户身份运行.使用 SetEnv 在 Virtual Hosts 文件中设置的变量不可用,因为范围有些不同.

Vars set in my user profile aren't available because Apache isn't run as that user. Vars set in the Virtual Hosts file with SetEnv are not available because the scope is somehow different.

我读过一些1,2 的 SO 答案,这导致了这个博客有解决方案.

I've read a couple 1,2 of SO answers, which leads to this blog with a solution.

我不知道如何将解决方案应用到使用 wsgi.py 文件的当前版本的 Django,它看起来像:

I can't figure out how to apply the solution to current versions of Django which use a wsgi.py file, which looks like:

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

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

我如何将该博客解决方案应用于 wsgi.py 文件,或者是否有更好的地方来存储 env-vars 以便 Django 可以获取它们?

How can I apply that blog solution to the wsgi.py file, or is there a better place to store env-vars where Django can get at them?

推荐答案

如果其他人对 Graham 的回答感到沮丧,这里有一个实际适用于原始问题的解决方案.我个人觉得从 Apache 设置环境变量非常有用和实用,尤其是因为我配置了自己的托管环境并且可以做任何我想做的事情.

If anyone else is frustrated by Graham's answer, here is a solution that actually works for the original question. I personally find setting environmental variables from Apache to be extremely useful and practical, especially since I configure my own hosting environment and can do whatever I want.

wsgi.py(在 Django 1.5.4 中测试)

wsgi.py (tested in Django 1.5.4)

from django.core.handlers.wsgi import WSGIHandler

class WSGIEnvironment(WSGIHandler):

    def __call__(self, environ, start_response):

        os.environ['SETTINGS_CONFIG'] = environ['SETTINGS_CONFIG']
        return super(WSGIEnvironment, self).__call__(environ, start_response)

application = WSGIEnvironment()

值得注意的是,你失去了 django.core.wsgi.get_wsgi_application 的面向未来的方法,它目前只返回 WSGIHandler().如果 WSGIHandler.__call__ 方法被更新并且你也更新了 Django,你可能需要在参数改变时更新 WSGIEnvironment 类.我认为这是为了方便而付出的很小的代价.

Of minor note, you lose the future-proof method of django.core.wsgi.get_wsgi_application, which currently only returns WSGIHandler(). If the WSGIHandler.__call__ method is ever updated and you update Django also, you may have to update the WSGIEnvironment class if the arguments change. I consider this a very small penalty to pay for the convenience.

这篇关于从 Django wsgi.py 文件访问 Apache SetEnv 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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