Gunicorn service can't read environment variables

March 2, 2017 361 views
Nginx Django PostgreSQL Ubuntu 16.04

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.: SECRETKEY = os.environ['DJANGOSECRET_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?

1 comment
  • 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
    ...
    
Be the first one to answer this question.