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

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

问题描述



我正在尝试将Django的秘密密钥和DB传递到环境变量中,如同广泛提出的那样,我可以在本地/生产服务器之间使用相同的代码库。 p>我正在运行的问题是正确设置,然后读取运行Apache + mod_wsgi的生产服务器上的环境变量。



我的用户配置文件中设置的Vars不是可用,因为Apache不是作为该用户运行的。



我已经在虚拟主机文件中设置了 SetEnv 阅读几个 1 2 的SO答案,这导致这个博客有一个解决方案。 p>

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

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

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

怎么可以我将该博客解决方案应用于wsgi.py文件,还是有更好的地方存储Django可以获取的env-var?

解决方案

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



wsgi.py在Django 1.5.4)

 从django.core.handlers.wsgi导入WSGIHandler 

类WSGIEnvironment (WSGIHandler):

def __call __(self,environ,start_response):

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

application = WSGIEnvironment()

在次要注意事项中,您将失去 django.core.wsgi.get_wsgi_application 的未来保护方法,该方法目前只返回 WSGIHandler( )。如果 WSGIHandler .__调用__ 方法已更新,并且您也更新Django,则可能必须更新 WSGIEnvironment 类if争论发生变化。我认为这是一个很小的罚款,为了方便起见。


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.

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

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.

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

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()

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?

解决方案

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 (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()

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天全站免登陆