SECRET_KEY error with Django using uWSGI

4.3k views Asked by At

I'm trying to run a Python 3 Django application with uWSGI, and having trouble.

I run uWSGI as a daemon:

$ sudo service uwsgi start

I'm using a file like the following: [uwsgi]

chdir = /home/ubuntu/my_app
module = my_app.wsgi
# path below is to virtual environment
home = /home/ubuntu/my_app/env 
http = :8000
check-static = /var/www/my_app
daemonize = /var/log/uwsgi/my_app

# process-related settings
master = true
processes = 10
vacuum = true

And I get the following puzzling error:

mapped 800360 bytes (781 KB) for 10 cores
*** Operational MODE: preforking ***
Traceback (most recent call last):
  File "./titlematch_api/wsgi.py", line 14, in <module>
    application = get_wsgi_application()
  File "/home/ubuntu/titlematch_api/env/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    django.setup()
  File "/home/ubuntu/titlematch_api/env/lib/python3.4/site-packages/django/__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/ubuntu/titlematch_api/env/lib/python3.4/site-packages/django/conf/__init__.py", line 48, in __getattr__
    self._setup(name)
  File "/home/ubuntu/titlematch_api/env/lib/python3.4/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/ubuntu/titlematch_api/env/lib/python3.4/site-packages/django/conf/__init__.py", line 92, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "./titlematch_api/settings.py", line 20, in <module>
    SECRET_KEY = os.environ['SECRET_KEY']
  File "/usr/lib/python3.4/os.py", line 631, in __getitem__
    raise KeyError(key) from None
KeyError: 'SECRET_KEY'

I have set the SECRET_KEY environment variable, both for my current user, and as root, and I can successfully execute os.environ['SECRET_KEY'] in the python shell.

2

There are 2 answers

0
Joran Beasley On BEST ANSWER

you cannot really do that as your environment is setup in ~/.bashrc locally which uwsgi does not have access to.

you might be able to get away with putting the variable in /etc/rc.local but Im not even sure that will work

you can add them to your file

[uwsgi]
chdir = /home/ubuntu/my_app
module = my_app.wsgi
# path below is to virtual environment
home = /home/ubuntu/my_app/env 
http = :8000
check-static = /var/www/my_app
daemonize = /var/log/uwsgi/my_app

# process-related settings
master = true
processes = 10
vacuum = true

#environment
env=SECRET_KEY="My Super Secret Key"

or you can configure it directly on the app object in your python file

app = Flask(__name__)
app.secret_key="My Super Secret Key"

which to be frank is probably the way I would do it

0
nachonachoman On

inside your uwsgi ini config: env = SECRETKEY=$(SECRETKEY) ^^^ this will instruct uwsgi to take the SECRETKEY env var and ensure it is set in the forked processes it creates