Question

Gunicorn service can't read environment variables

I am having some troubles to configure a Django app (using virtualenv) with Postgres, Nginx, and Gunicorn on Ubuntu 16.04.

Everything worked fine until I started setting some environment variables for some Django configuration variables. Ex.: SECRET_KEY = os.environ[‘DJANGO_SECRET_KEY’].

I have tried a lot of options like setting variables on virtualenv’s activate file, setting variables on ubuntu, and on gunicorn.service file but nothing is working.

Does anyone know how to work on this issue? How to set the environment variables?

Subscribe
Share

Note that the format of EnvironmentFile needs to be:

some_secret=secretvalue
another_secret=blah

Note that the format of EnvironmentFile needs to be:

some_secret=secretvalue
another_secret=blah

Note that the format of EnvironmentFile needs to be:

some_secret=secretvalue
another_secret=blah

Hi! Instead of adding the variables to the gunicorn command line, you could load them through a file: <on gunicorn.service> [Service] … EnvironmentFile=/path_to_/some_file_with_secrets … This should make the trick. (dont forget to create the file with those secrets)

Hi! Instead of adding the variables to the gunicorn command line, you could load them through a file: <on gunicorn.service> [Service] … EnvironmentFile=/path_to_/some_file_with_secrets … This should make the trick. (dont forget to create the file with those secrets)

Hi! Instead of adding the variables to the gunicorn command line, you could load them through a file: <on gunicorn.service> [Service] … EnvironmentFile=/path_to_/some_file_with_secrets … This should make the trick. (dont forget to create the file with those secrets)

Hi! Instead of adding the variables to the gunicorn command line, you could load them through a file: <on gunicorn.service> [Service] … EnvironmentFile=/path_to_/some_file_with_secrets … This should make the trick. (dont forget to create the file with those secrets)

Hi! Instead of adding the variables to the gunicorn command line, you could load them through a file: <on gunicorn.service> [Service] … EnvironmentFile=/path_to_/some_file_with_secrets … This should make the trick. (dont forget to create the file with those secrets)

Hi! Instead of adding the variables to the gunicorn command line, you could load them through a file: <on gunicorn.service> [Service] … EnvironmentFile=/path_to_/some_file_with_secrets … This should make the trick. (dont forget to create the file with those secrets)

I could make it work by defining the variables on gunicorn command in the file /etc/systemd/system/gunicorn.service but I still think that it is not the best solution =/

[Service]
...
ExecStart=/virtuall_env_path/bin/gunicorn --workers 3 --bind unix:/path_to_myproject.sock myproject.wsgi:application -e my_var=value -e my_var2=value2
...

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This question was answered by @kameruchi:

Hi! Instead of adding the variables to the gunicorn command line, you could load them through a file: <on gunicorn.service> [Service] … EnvironmentFile=/path_to_/some_file_with_secrets … This should make the trick. (dont forget to create the file with those secrets)

View the original comment