本文介绍了纠正gunicorn.conf以获取Django使用的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Nginx 在我的VPS上部署一个Django应用程序作为Web服务器, Gunicorn 安装在的virtualenv 。 (我使用 virtualenv virtualenvwrapper 。)



当我运行 Gunicorn 时,可以找到环境变量(如数据库密码,名称):

  workon virtual_env_name 
#从my_project的根路径
gunicorn my_project.wsgi --bind localhost:9000 --daemon
#这个工程

我以这种方式导出环境变量:

 #/ home / user_name / Envs / virtual_env_name / bin / postactivate 
export DATABASE_PASSWORD =secret_password

但是,下面的方式(无论是否激活了$ code> virtual_env_name ):

  sudo服务gunicorn启动
#env变量无法找到 - KeyError抛出

这是我的 gunicorn.conf 脚本如下:

  start on(本地文件系统和net-device-up IFACE = eth 0)
停止在运行级别[!12345]

#如果进程退出无法触发重新启动
respawn

setuid user_name
setgid www-data
chdir / home / user_name / my_project

exec / home / user_name / Envs / virtual_env_name / bin / gunicorn \
--name = my_project \
--pythonpath = / home / user_name / my_project \
--bind = 127.0.0.1:9000 \
my_project.wsgi:应用程序
/ pre>

我可以确认这个 gunicorn.conf 工作,如果我硬编码所有的密码,密钥到我的Django的 settings.py 而不是使用 os.environ [..]



当我开始 Gunicorn sudo service start ?第一种和第二种方式有什么区别?谢谢。

解决方案

您需要在gunicorn.conf中定义这些变量。

  env DATABASE_PASSWORD =secret_password


I am deploying a Django app on my VPS using Nginx as the web server and Gunicorn installed in virtualenv. (I am using virtualenv with virtualenvwrapper.)

When I run Gunicorn like this, environment variables (such as database password, name) can be found:

workon virtual_env_name
# from my_project's root path
gunicorn my_project.wsgi --bind localhost:9000 --daemon
# this works

I exported the environment variables this way:

# /home/user_name/Envs/virtual_env_name/bin/postactivate
export DATABASE_PASSWORD="secret_password"

However the way below does not (whether or not virtual_env_name is activated):

sudo service gunicorn start
# env variables can't be found - KeyError thrown

This is how my gunicorn.conf script looks like:

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectadly trigger a respawn
respawn

setuid user_name
setgid www-data
chdir /home/user_name/my_project

exec /home/user_name/Envs/virtual_env_name/bin/gunicorn \
    --name=my_project \
    --pythonpath=/home/user_name/my_project \
    --bind=127.0.0.1:9000 \
    my_project.wsgi:application

I can confirm this gunicorn.conf works if I hard code all the password, keys into my Django's settings.py instead of using os.environ[..].

What do I need to do to make my environment variables found when I start Gunicorn with sudo service start? What's the difference between the first and second way? Thanks.

解决方案

You need to define those variables inside gunicorn.conf.

env DATABASE_PASSWORD="secret_password"

这篇关于纠正gunicorn.conf以获取Django使用的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 05:11