Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Dreamhost和WSGI上为Django设置环境变量_Django_Linux_Passenger_Dreamhost - Fatal编程技术网

在Dreamhost和WSGI上为Django设置环境变量

在Dreamhost和WSGI上为Django设置环境变量,django,linux,passenger,dreamhost,Django,Linux,Passenger,Dreamhost,我试图为Django设置一个不在源代码中的环境变量,在本例中是电子邮件密码。这就是我试图模仿的例子: server: /etc/systemd/system/gunicorn-superlists-staging.ottg.eu.service [Service] User=elspeth Environment=EMAIL_PASSWORD=yoursekritpasswordhere WorkingDirectory=/home/elspeth/sites/superlists-stagi

我试图为Django设置一个不在源代码中的环境变量,在本例中是电子邮件密码。这就是我试图模仿的例子:

server: /etc/systemd/system/gunicorn-superlists-staging.ottg.eu.service

[Service]
User=elspeth
Environment=EMAIL_PASSWORD=yoursekritpasswordhere
WorkingDirectory=/home/elspeth/sites/superlists-staging.ottg.eu/source
该示例使用nginx和gunicorn,但我正在尝试使用Dreamhost、Linux和wsgi进行设置

以下是我的文件结构:

.
|-- \ ~
|-- __pycache__
|-- database
|-- etc
|-- passenger_wsgi.log
|-- passenger_wsgi.py
|-- passenger_wsgi.pyc
|-- public
|-- source
|-- static
|-- superlists
|-- testGoat
|-- tmp
`-- virtualenv

超级列表是项目所在的位置。那么,在这种情况下,我应该如何设置环境变量呢

环境变量是指环境变量,因此我假设您使用脚本启动服务器。假设在bash中,您可以为示例创建一个文件

env_variables.sh

或者在python文件中使用os.environ执行同样的操作


然后在应用程序的设置中,使用os.environ.get'SECRET_KEY'加载环境变量。

我在阅读两勺Django时找到了解决方案。Apache不允许设置环境变量。相反,我在/etc文件夹中添加了一个secrets.json文件,并使用设置中的方法从json文件中读取机密数据。下面是该方法的外观:

import os

# 2 Scoops 5.4.1, Env Varible Alternative
import json 
from django.core.exceptions import ImproperlyConfigured

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# JSON-based secrets module (see 2 Scoops, 5.4.1)
with open(os.path.join(BASE_DIR, '../etc/secrets.json')) as f:
    secrets = json.loads(f.read())

def get_secret(setting, secrets=secrets):
    '''Get the secret variable or return explicit exception.'''
    try:
        return secrets[setting]
    except KeyError:
        error_msg = 'Set the {0} environment variable'.format(setting)
        raise ImproperlyConfigured(error_msg)

SECRET_KEY = get_secret('SECRET_KEY')
EMAIL_HOST = get_secret('EMAIL_HOST')
EMAIL_HOST_USER = get_secret('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = get_secret('EMAIL_HOST_PASSWORD')
EMAIL_PORT = get_secret('EMAIL_PORT')
JSON文件如下所示:

{
    "FILENAME": "secrets.json",
    "SECRET_KEY": "shhhh...it's a secret",
    "DATABASES_HOST": "127.0.01",
    "PORT": "5432",
    "EMAIL_HOST": "smtp.email.com",
    "EMAIL_HOST_USER": "name@email.com",
    "EMAIL_HOST_PASSWORD": "123456",
    "EMAIL_PORT": "000",
    "EMAIL_USE_TLS": "True"
}

谢谢你的建议。据我所知,Dreamhost不允许我使用任何特定脚本启动服务器。我发现了另一个使用JSON文件的方法。
{
    "FILENAME": "secrets.json",
    "SECRET_KEY": "shhhh...it's a secret",
    "DATABASES_HOST": "127.0.01",
    "PORT": "5432",
    "EMAIL_HOST": "smtp.email.com",
    "EMAIL_HOST_USER": "name@email.com",
    "EMAIL_HOST_PASSWORD": "123456",
    "EMAIL_PORT": "000",
    "EMAIL_USE_TLS": "True"
}