4

我试图在环境变量中设置我的 django“SECRET_KEY”。

我使用 virtualenv+virtualenvwrapper 作为环境。

在我输入之后

workon mydomain.com

我打了

export SECRET_KEY=xxxxxx

当我尝试做

echo $SECRET_KEY

我得到了我的价值(xxxxxx)

到目前为止,一切都很好。但是当我尝试在浏览器中访问我的网站(使用 ngnix 和 gunicorn)时,我得到了一个错误。

这是我的 gunicorn 监督脚本

[program:gunicorn]
directory = /home/<USER>/.virtualenvs/<DOMAIN>/<PROJECT>/
command=/home/<USER>/.virtualenvs/<DOMAIN>/bin/gunicorn <PROJECT>.wsgi:application -c /home/<USER>/.virtualenvs/<DOMAIN>/<PROJECT>/<APP>/gunicorn_conf.py
user = <USER>

这是我的 gunicorn_conf.py:

bind = "127.0.0.1:8000"
logfile = "/home/<USER>/logs/gunicorn.log"

这是我的settings.py:

def get_env_variable(var_name):
    try:
        return os.environ[var_name]
    except KeyError:
        error_msg = "Set the %s environment variable" % var_name
        raise ImproperlyConfigured(error_msg)
    

SECRET_KEY = get_env_variable("SECRET_KEY")

如何正确设置我的环境变量以便 ngnix 可以读取它?

编辑:追溯:

Traceback (most recent call last):
  File "/home/<USER>/.virtualenvs/<DOMAIN>/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 125, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/home/<USER>/.virtualenvs/<DOMAIN>/downloads/django-trunk/django/core/handlers/wsgi.py", line 236, in __call__
    self.load_middleware()
  File "/home/<USER>/.virtualenvs/<DOMAIN>/downloads/django-trunk/django/core/handlers/base.py", line 46, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/home/<USER>/.virtualenvs/<DOMAIN>/downloads/django-trunk/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/home/<USER>/.virtualenvs/<DOMAIN>/downloads/django-trunk/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/<USER>/.virtualenvs/<DOMAIN>/downloads/django-trunk/django/conf/__init__.py", line 126, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/<USER>/.virtualenvs/<DOMAIN>/downloads/django-trunk/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/<USER>/.virtualenvs/<DOMAIN>/<PROJECT>/<APP>/settings/staging.py", line 2, in <module>
    from .base import *
  File "/home/<USER>/.virtualenvs/<DOMAIN>/<PROJECT>/<APP>/settings/base.py", line 40, in <module>
    SECRET_KEY = get_env_variable("SECRET_KEY")
  File "/home/<USER>/.virtualenvs/<DOMAIN>/<PROJECT>/<APP>/settings/base.py", line 31, in get_env_variable
    raise ImproperlyConfigured(error_msg)
ImproperlyConfigured: Set the SECRET_KEY environment variable
4

2 回答 2

2

您可以使用指令在主管的配置中定义环境变量environment=VAR1=<VAR1>,VAR2=<VAR2>

像这样的东西:

[program:gunicorn]
directory=/home/<USER>/.virtualenvs/<DOMAIN>/<PROJECT>/
command=/home/<USER>/.virtualenvs/<DOMAIN>/bin/gunicorn <PROJECT>.wsgi:application -c      /home/<USER>/.virtualenvs/<DOMAIN>/<PROJECT>/<APP>/gunicorn_conf.py
user = <USER>
environment=SECRET_KEY=<SECRET_KEY>
于 2013-06-21T14:04:45.273 回答
0

您可以通过文件加载环境变量:

在您的应用程序文件夹中创建您自己的环境文件:

vim /home/ubuntu/workspace/apps/myapp/envfiles/myenvironment

将您的环境变量添加到该文件中:

APP_ENVIRONMENT="dev"

SECRET_KEY="mysecret_key"

然后将此环境加载到您的 gunicorn 服务文件中:

 sudo vim /etc/systemd/system/gunicorn.service

在 [Service] 博客中配置您的环境文件

 [Service]
 ....
 EnvironmentFile=/home/ubuntu/workspace/apps/myapp/envfiles/myenvironment
于 2020-07-13T11:38:17.493 回答