如何使用virtualenv在本地开发中设置环境变量

时间:2015-03-26 09:50:16

标签: python django apache amazon-web-services wsgi

我目前正试图通过ZIP将我的django应用程序发布到AWS elasticbeanstalk。

由于我有本地和在线开发环境,我想使用AWS无论如何都使用的环境变量。

对于我的开发环境,我已经在使用virtualenv了。 Apache运行时启用了wsgi,并且应该使用这样的环境。不幸的是,它不知道我在bin / activate中设置的环境。

Django抛出一个500错误,因为它无法连接到数据库,因为它无法访问环境变量:

1:53.862779 2015] [wsgi:error] [pid 20639] [remote 127.0.0.1:51896] KeyError: 'RDS_DB_NAME'

如果我激活我的环境并查看env vars:

(myenv)[bs@debian-gohan:]/var/www/vhosts/mysite $ env | grep RDS
RDS_HOSTNAME=localhost

......我也得到了其他人......

我的Apache Vhost是:

<VirtualHost *:80>
    WSGIDaemonProcess mysite python-path=/home/bs/envs/myenv/bin/python2.7:/home/bs/envs/myenv/lib/python2.7/site-packages
    WSGIProcessGroup mysite
    WSGIScriptAlias / /var/www/vhosts/main-page/mysite/wsgi.py

有没有人知道为什么apache没有看到环境变量?

编辑1: 我试图在Apache配置中使用SetEnv,如下所示:

SetEnv RDS_HOSTNAME "localhost"

不幸的是,这不起作用。然后我删除了ProcessGroup,只使用了一个简单的WSGIPythonPath。这也不起作用。

apache日志中的错误保持不变:

[Thu Mar 26 11:30:34.046807 2015] [wsgi:info] [pid 23012] [client 127.0.0.1:55231] mod_wsgi (pid=23012, process='', application='mysite.dbz.dev|'): Loading WSGI script '/var/www/vhosts/main-page/mysite/wsgi.py'.
[Thu Mar 26 11:30:34.178717 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] mod_wsgi (pid=23012): Target WSGI script '/var/www/vhosts/main-page/mysite/wsgi.py' cannot be loaded as Python module.
[Thu Mar 26 11:30:34.178812 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] mod_wsgi (pid=23012): Exception occurred processing WSGI script '/var/www/vhosts/main-page/mysite/wsgi.py'.
[Thu Mar 26 11:30:34.178924 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] Traceback (most recent call last):
[Thu Mar 26 11:30:34.178947 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]   File "/var/www/vhosts/main-page/mysite/wsgi.py", line 17, in <module>
[Thu Mar 26 11:30:34.179111 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]     application = get_wsgi_application()
[Thu Mar 26 11:30:34.179157 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]   File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Thu Mar 26 11:30:34.179319 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]     django.setup()
[Thu Mar 26 11:30:34.179363 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]   File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 20, in setup
[Thu Mar 26 11:30:34.179606 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Thu Mar 26 11:30:34.179651 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 46, in __getattr__
[Thu Mar 26 11:30:34.179774 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]     self._setup(name)
[Thu Mar 26 11:30:34.179789 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
[Thu Mar 26 11:30:34.179918 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]     self._wrapped = Settings(settings_module)
[Thu Mar 26 11:30:34.179959 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 94, in __init__
[Thu Mar 26 11:30:34.180072 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Thu Mar 26 11:30:34.180112 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Thu Mar 26 11:30:34.180220 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]     __import__(name)
[Thu Mar 26 11:30:34.180246 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]   File "/var/www/vhosts/main-page/mysite/../mysite/settings.py", line 94, in <module>
[Thu Mar 26 11:30:34.180400 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]     'NAME': os.environ['RDS_DB_NAME'],
[Thu Mar 26 11:30:34.180443 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]   File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
[Thu Mar 26 11:30:34.180613 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231]     raise KeyError(key)
[Thu Mar 26 11:30:34.180665 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] KeyError: 'RDS_DB_NAME'

1 个答案:

答案 0 :(得分:0)

像这样设置

<VirtualHost hostname:80>
   ...
   SetEnv VARIABLE_NAME variable_value
   ...
</VirtualHost>

在wsgi脚本中。

os.environ.setdefault("variable", "value")

覆盖变量,

os.environ['var']="val"