[Django]-How to use environment variables with supervisor, gunicorn and django (1.6)

28đź‘Ť

OK, I guess I got it.

I had tried including

environment=SECRET_KEY="secret_key_with_non_alphanumeric_chars"

in the conf file for supervisor but it didn’t like the non alphanumeric chars and I didn’t want to have my key in the conf file as I have it in git.

After loking at supervisor’s docs I had also tried with:

HOME="/home/django", USER="django"

but didn’t work.

Finally I tried with this and is working now!:

environment=HOME="/home/django", USER="django", SECRET_KEY=$SECRET_KEY

Maybe although it’s working it’s not the best solution. I’d be happy to learn more.

EDIT:

Finally, Ewan made me see that using the bash for setting the env vars wouldn’t be the best option. So one solution, as pointed by #Ewan, would be to use:

[program:my_project]
...
environment=SECRET_KEY="secret_key_avoiding_%_chars"

Another solution I found, for those using virtualenv would be to export the env vars in the “activate” script of the virtualenv, that is, edit your virtualenv/bin/activate file and add at the end your SECRET_KEY.

This way you can use % chars as generated by key generators for django and is valid if you don’t use supervisor.

I restarted my server without logging to check that it worked. With this option I don’t have to edit my keys, I can keep my conf files versioned and it works whether I use supervisor, upstart or whatever (or nothing, just gunicorn).

Anyway, I know I haven’t discovered anything new (well @Ewan raised an issue with supervisor) but I’m learning things and hope this can be useful to someone else.

👤equalium

11đź‘Ť

Also if you use gunicorn config file:

gunicorn -c gunicorn.py myproject.wsgi

It’s possible to pass environment variables in the gunicorn.py file like this:

bind = "0.0.0.0:8001"
workers = 3
proc_name = "myproject"
user = "django"
group = "django"
loglevel = "debug"
errorlog = "/home/django/myproject/log/gunicorn.log"
raw_env = [
   'DATABASE_URL=postgres://user:password@host/dbname',
   'SECRET_KEY=mysecretkey',
]
👤MadisonTrash

6đź‘Ť

Your .bashrc will only work for interactive shells so will work when running the shell script as your user however supervisor, running in the background, wont get passed these values.

Instead, in your supervsior .ini file set the environment variable there (more information in the documentation).

e.g.

[program:my_django_project]
environment=SECRET_KEY="my_secret_key"

After a little bit of trial and error, I noticed that the supervisor .ini file doesn’t like to have % in the environment variables section (even if you do quote it…). Based on your example in the comments I have tried this with supervisor==3.0 installed via pip and it works:

environment=SECRET_KEY="*wi4h$kqxp84f3w6uh8w@l$0(+@x$3cr&)z^lmg+pqw^6wkyi"

Only difference is I have removed the% sign. (I tried escaping it with \% but this still didn’t work)

Edit 2

Raised issue #291 with supervisor for this bug.

Edit 3

As noted in above issue, if a % is present in your secret key it must be escaped python-style: %%

👤Ewan

1đź‘Ť

You can escape % character by adding another % character.

Otherwise, quoting the values is optional but recommended. To escape percent characters, simply use two. (e.g. URI="/first%%20name")

Taken from here: http://supervisord.org/configuration.html

Leave a comment