简体   繁体   English

使用环境变量隐藏 Django SECRET_KEY 的问题

[英]Issue with Hiding Django SECRET_KEY Using environment variables

I am running my programs and Django Project on an AWS EC2 Ubuntu instance in a virtual environment.我正在虚拟环境中的 AWS EC2 Ubuntu 实例上运行我的程序和 Django 项目。 The django project is run on an apache server. django 项目在 apache 服务器上运行。 For purposes of this question I will say that my secret key is 'AAAA'.出于这个问题的目的,我会说我的密钥是“AAAA”。 I have an environment variable set ("SECRET_KEY").我有一个环境变量集(“SECRET_KEY”)。 It is properly setup, and when I put:它已正确设置,当我输入时:

import os print(os.environ['SECRET_KEY']) in the python shell, it prints the proper secret key, AAAA.在 python shell 中import os print(os.environ['SECRET_KEY']) ,它会打印正确的密钥,AAAA。 Therefore, I know python is able to access the proper environment variable.因此,我知道 python 能够访问正确的环境变量。 In settings.py, when SECRET_KEY='AAAA' , the project works, and everything executes properly.在 settings.py 中,当SECRET_KEY='AAAA'时,项目工作,一切正常执行。 However when I change this to: SECRET_KEY=os.environ['SECRET_KEY'] the server doesn't work (500 Internal Server Error).但是,当我将其更改为: SECRET_KEY=os.environ['SECRET_KEY']时,服务器无法正常工作(500 内部服务器错误)。

Everything seems like it should work.一切似乎都应该起作用。 Any suggestions or ideas on what I missed here?关于我在这里错过的任何建议或想法?

Turns out I had to add a default state to the os.environ.get like so:原来我必须像这样向 os.environ.get 添加一个默认的 state :

SECRET_KEY = os.environ.get('SECRET_KEY', 'mmglfamx3n927*93$ks#r)h%*a(@))vb7*=2q$&z(=6@q&*ghj')

I use it like this SECRET_KEY = os.getenv('SECRET_KEY') to get environment variables in my projects.我像这样使用SECRET_KEY = os.getenv('SECRET_KEY')来获取项目中的环境变量。

Another thing that you can do is print the env variable in the terminal server to make sure it's available for the application.您可以做的另一件事是在终端服务器中打印 env 变量,以确保它可用于应用程序。

Regards,问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 环境变量 Django:配置不当:SECRET_KEY 设置不能为空 - Environment Variables Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty 环境变量的 SECRET_KEY 错误 - SECRET_KEY errors with environment variables 使用uWSGI的Django的SECRET_KEY错误 - SECRET_KEY error with Django using uWSGI 当生成的 SECRET_KEY 以“$”开头时,如何从环境文件中转义 Django 中的 SECRET_KEY? - How to escape SECRET_KEY in Django from environment file when generated SECRET_KEY begins with '$'? VPS 上的 Django SECRET_KEY - Django SECRET_KEY on VPS Django设置SECRET_KEY - Django settings SECRET_KEY 部署Django SECRET_KEY - Deploy Django SECRET_KEY windows 中的设置命令不适用于 django SECRET_KEY | django.core.exceptions.ImproperlyConfigured:设置 SECRET_KEY 环境变量 - set command in windows not working for django SECRET_KEY | django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable 如何在 Django 中保密 SECRET_KEY? - How to keep a SECRET_KEY secret in Django? 使用 appengine “django.core.exceptions.ImproperlyConfigured:设置 SECRET_KEY 环境变量”在 Google 云上部署 Django 应用程序时出错 - Error in deploying Django app on Google cloud using appengine "django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM