本文介绍了Django - 无法在 WSGI 接口上将环境变量传递给 Apache/Passenger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找解决方案,我无法将/etc/profile 中定义的变量传递给 apache.

I'm looking for a solution, I can't pass to apache a variable define in /etc/profile.

这是我要设置的内容:

我有一个 django 应用程序,我通过 WGSI 接口通过 Apache/Passenger (modrails) 部署该应用程序.

I have a django application that I deploy through Apache/Passenger (modrails) through the WGSI interface.

在我的 settings.py 中,我使用了这个 python 命令:环境 = os.getenv('ENV', 'PROD')因此,如果未定义环境变量,则默认情况下会进入生产设置.

In my settings.py I am using this python command : ENVIRONMENT = os.getenv('ENV', 'PROD')so if the ENVIRONMENT variable is not define it goes to the production settings by default.

这有助于我管理我连接到的数据库并激活调试工具.

That is helping me to manage which DB I connect to and activate debugging tools.

它正在生产中工作,因为它是默认值,当我在 DEV 服务器上时,我无法从/etc/profile 获取环境变量.

It is working in production because it is the default values, it's when I am on the DEV server that I can't get the environment variable from /etc/profile.

它在 Ubuntu 10.04 服务器上

It is on an Ubuntu 10.04 Server

所以这是我做的一些测试:

So here is some test I did:

1) 放入/etc/profile ->环境='开发'导出环境

1) put into /etc/profile -> ENV='DEV' export ENV

2) 在/etc/profile.d/environment.sh 下 ->#!/bin/sh环境='开发'导出环境

2) under /etc/profile.d/environment.sh -> #!/bin/sh ENV='DEV' export ENV

3) 在我的虚拟主机配置文件中 ->PassEnv ENV

3) in my virtual host config file -> PassEnv ENV

4) 仍在我的虚拟主机配置文件中 ->SetEnv ENV DEV

4) still in my virtual host config file -> SetEnv ENV DEV

5) 如果我强制使用此文件,则乘客需要一个passenger_wgsi.py 来注册您的应用程序viron['ENV'] = 'DEV' 这成功了,但我做不到

5) passenger require a passenger_wgsi.py to register your application if I force in this file viron['ENV'] = 'DEV' this gone a work but I am not able to do this

ENVIRONMENT = os.getenv('ENV', 'PROD')
os.environ['ENV'] = ENVIRONMENT

知道为什么或如何解决这个问题吗?谢谢你!





@Josh

Any idea why or how can I fix this?thank you!





@Josh

谢谢回复.但我还有一些问题.

Thanks for the reply. But I still have some questions.

对于Passenger,我唯一需要在虚拟主机文件中设置的东西是我的 python/django 应用程序中公共文件夹的位置.所以我的虚拟主机文件看起来像这样.

With Passenger, the only things I had to set in the virtual host filewas the location of the public folder inside my python/django app.So my virtual host file would look like this.

DocumentRoot path_to_my_public_folder # /home/user/workspace/myapp/public

然后在 myapp 文件夹中有passenger_wsgi.py

Then inside the myapp folder there is the passenger_wsgi.py

像你说的那样定义很少:

which define very little like you said :

import sys, os
sys.path.append(os.getcwd())

os.environ['DJANGO_SETTINGS_MODULE'] = "myapp.settings"

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

完成这些设置后,应用程序就可以运行了.所以我可能错过你的指示是当你说我们必须指定我们使用了哪个 wsgi 文件.我虽然它会直接定位passenger_wsgi.py并默认加载它看起来的样子目前正在做的事情.

and after these settings that was it, the application was running.So where I'm maybe missing your indication is when you are saying that we have to specify which wsgi file we used. I though it would directly locate passenger_wsgi.py and load it by default what it seemsto be doing at the moment.

您应该知道的另一件事是我们的项目驻留在一个 SVN 存储库中,并且我想为许多用户设置这个文件,这样我们就没有不同的设置.

Another things you should know is that our project is residing in an SVN repository and I want to setup this file for many users so we don't have different settings.

但是我明天要测试的是拥有一个包含公共信息的基本 wsgi 文件的想法并根据计算机的主机名,我将从 dev_wsgi.py 或 prod_wsgi.py 导入其他设置

But what I will test tomorow is the idea of having one base wsgi file with the common informationand base on the hostname of the computer I will import the others settings from like dev_wsgi.py or prod_wsgi.py

我应该能够使用 python 检索这个名称,并根据名称设置我需要使用的变量.

I should be able to retrieve this name with python and base on the name I will set the variables I need to use.

谢谢你的想法,非常感谢.

Thank you for the idea, it is well appreciate.

推荐答案

由于我不知道如何解决您的具体问题,因此我将提出替代解决方案.

I'm going to suggest an alternative solution since I have no idea how to fix your specific problem.

创建多个 WSGI 文件.production.wsgidev1.wsgidev2.wsgitest.wsgi 等等.每个网络服务器必须是无论如何配置了 /path/to/.wsgi .无论如何,wsgi 文件中只有少量代码,因此复制它并不是很昂贵.此外,您可以使用 _base.wsgi 来提供所有公共值,并要求派生的 wsgis 调用 application = wsgi.WSGIHandler().

Create multiple WSGI files. production.wsgi, dev1.wsgi, dev2.wsgi, test.wsgi etc. Each webserver has to be configured with the /path/to/.wsgi anyway. There is only a minimal amount of code in a wsgi file anyway, so replicating this isn't very expensive. Also, you could have a _base.wsgi to supply all the common values, and require the derived wsgis to call application = wsgi.WSGIHandler().

我们项目中的每个开发人员都有自己的设置文件和 wsgi 文件,以便能够篡改设置,而不会以流氓值破坏生产.

Each developer on our project has their own setting files and wsgi files to enable tampering with settings without ever being able to break production with a rogue value.

这篇关于Django - 无法在 WSGI 接口上将环境变量传递给 Apache/Passenger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 20:20