Gunicorn Environment Variable Setting

后端 未结 3 1977
说谎
说谎 2021-02-02 16:42

I\'m currently having difficulty passing environment variables into Gunicorn for my Django project. I\'m on the latest 19.1 version. I have a wsgi.py file like so:



        
相关标签:
3条回答
  • 2021-02-02 17:00

    You just have to export your environment variable.

    export SERVER_ENV=TEST
    export SERVER_ID=TEST
    gunicorn -b 127.0.0.1:8080 --error-logfile - --access-logfile - app.wsgi:application
    

    And in your code you can get them like that

    os.getenv('SERVER_ENV')
    
    0 讨论(0)
  • 2021-02-02 17:16

    I don't quite understand what you are trying to do here. If you pass environment variables in the bash command line, they are already in os.environ: there is no need to get them from anywhere else. The environ dictionary is made up of elements passed from the request, not the shell.

    0 讨论(0)
  • 2021-02-02 17:20

    I got a similar problem when deploying gunicorn.service. And I passed environment variables to it by a file:

    <on gunicorn.service>
    [Service]
    ...
    EnvironmentFile=/pathto/somefilewith_secrets
    ...
    

    For example (cat /etc/systemd/system/gunicorn.service)

    [Unit]  
    Description=gunicorn daemon  
    After=network.target  
      
    [Service]  
    User=ubuntu
    Group=ubuntu
    WorkingDirectory=/home/ubuntu/10008/digichainOpen
    EnvironmentFile=/home/ubuntu/10008/digichainOpen/.env
    ExecStart=/home/ubuntu/.local/share/virtualenvs/digichainOpen-Zk2Jnvjv/bin/gunicorn \
              --worker-class=gevent --workers 4 \
              --bind unix:/home/ubuntu/10008/digichainOpen/gunicorn.sock digichainOpen.wsgi:application
                
    [Install]  
    WantedBy=multi-user.target  
    

    and the .env file can be:

    my_var=someValue
    some_secret=secretvalue
    another_secret=blah
    
    0 讨论(0)
提交回复
热议问题