[英]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.