Django - 无法将环境变量传递给 WSGI 接口上的 Apache/Passenger

发布于 2024-10-17 03:26:01 字数 1798 浏览 12 评论 0原文

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

这是我想要设置的内容:

我有一个 Django 应用程序,我通过 WGSI 接口通过 Apache/Passenger (modrails) 进行部署。

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

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

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

它位于 Ubuntu 10.04 服务器上

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

1)放入 /etc/profile -> ENV='DEV' 在 /etc/profile.d/environment.sh 下导出 ENV

2) -> #!/bin/sh ENV='DEV' 在我的虚拟主机配置文件中导出 ENV

3) -> PassEnv ENV

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

5) 如果我强行输入此文件,乘客需要一个 Passenger_wgsi.py 来注册您的应用程序 viron['ENV'] = 'DEV' 这已经成功了,但我无法做到这

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

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





@Josh

感谢您的回复。但我还有一些问题。

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

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

然后,在 myapp 文件夹内,有 Passenger_wsgi.py

,它的定义非常少,就像您所说的那样:

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并默认加载它,就像看起来的那样 此刻正在做的事。

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

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

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

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

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

Here is what I am trying to set:

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

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.

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.

It is on an Ubuntu 10.04 Server

So here is some test I did:

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

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

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

4) still in my virtual host config file ->
SetEnv 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

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

@Josh

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

With Passenger, the only things I had to set in the virtual host file
was 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

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

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 seems
to be doing at the moment.

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.

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅听莫相离 2024-10-24 03:26:01

我将建议一个替代解决方案,因为我不知道如何解决您的具体问题。

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

我们项目中的每个开发人员都有自己的设置文件和 wsgi 文件,以便能够篡改设置,而无法使用恶意值破坏生产。

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

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

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.

携君以终年 2024-10-24 03:26:01

我终于让它工作了,我将在之后提供我想出的方法
@乔希的建议。

现在我的 WSGI 文件如下:

import sys, os

sys.path.append(os.getcwd())
# this allows us to run the project under Passenger on our workstation
# it is simply a list of all the developers machine hostname it can be expended as we want
LIST_OF_DEV_HOSTNAME = ['PC1', 'PC2','PC3',]
# Return the current machine hostname
HOSTNAME = os.uname()[1]

# Import Django app module
if HOSTNAME in LIST_OF_DEV_HOSTNAME:
    # here if it is not detected you can append to your python path the root folder of your django app
    sys.path.append('/home/user/workspace/django-app')
    # As previously mentioned I use an Environment Variable in the settings.py to switch DB and debug settings
    # so here I set the ENV variable I use in the settings.py to reflect the Development environment
    os.environ['ENV'] = 'DEV'
else:
    # we could append any other needed path in here too
    sys.path.append('/any/other/folder')
    # As previously mentioned I use an Environment Variable in the settings.py to switch DB and debug settings
    # so here I set the ENV variable I use in the settings.py to reflect the Development environment
    os.environ['ENV'] = 'PROD'

# Shared between the two environments    
sys.path.append('/usr/lib/python2.4/site-packages/')

# Import the django app settings and Environment variable
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'

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

这就是我锻炼它的方法。

我还可以通过创建像 /opt/environments 这样的文件来实现它,并进行如下检查:
# 检查文件是否存在
if os.path.isfile('/opt/environments'):
os.environ['ENV'] = 'DEV'
别的:
os.environ['ENV'] = 'PROD'

它本来可以让我执行相同的检查,但我必须告诉每个人创建该文件。
这取决于您要创建文件或将主机名添加到列表中,因为我们的项目位于 SVN 下
对我来说,简单提交 WSGI 文件比创建文件更简单。

谢谢。

I finally got it to work and I will provide the way I figured it out after
@Josh suggestion.

Here is what looks like my WSGI file now:

import sys, os

sys.path.append(os.getcwd())
# this allows us to run the project under Passenger on our workstation
# it is simply a list of all the developers machine hostname it can be expended as we want
LIST_OF_DEV_HOSTNAME = ['PC1', 'PC2','PC3',]
# Return the current machine hostname
HOSTNAME = os.uname()[1]

# Import Django app module
if HOSTNAME in LIST_OF_DEV_HOSTNAME:
    # here if it is not detected you can append to your python path the root folder of your django app
    sys.path.append('/home/user/workspace/django-app')
    # As previously mentioned I use an Environment Variable in the settings.py to switch DB and debug settings
    # so here I set the ENV variable I use in the settings.py to reflect the Development environment
    os.environ['ENV'] = 'DEV'
else:
    # we could append any other needed path in here too
    sys.path.append('/any/other/folder')
    # As previously mentioned I use an Environment Variable in the settings.py to switch DB and debug settings
    # so here I set the ENV variable I use in the settings.py to reflect the Development environment
    os.environ['ENV'] = 'PROD'

# Shared between the two environments    
sys.path.append('/usr/lib/python2.4/site-packages/')

# Import the django app settings and Environment variable
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'

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

So this is the way I got it to workout.

I could also achieved it in creating a file like /opt/environments and did the check like this:
# check if the file exists
if os.path.isfile('/opt/environments'):
os.environ['ENV'] = 'DEV'
else:
os.environ['ENV'] = 'PROD'

it would have allowed me to perform the same check, but I would have to tell everyone to create the file.
It depends on what you want to do create a file or add your hostname to the list, since our project is under SVN
a simple commit of our WSGI file is for me more simple than the creation of the file.

Thank you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文