Django not Recognizing Secret Key Environment Variable in Production

I’ve set my ‘DJANGO_SECRET_KEY’ as an environment variable, but I’m still getting the secret key error message in my server logs, and the secret key warning when I run ‘python manage.py check –deploy’. The web browser displays a (500) Internal Server Error page. I’m deploying on a Linux VPS Debian/Ubuntu server on Plesk running Apache/Nginx, with mod_wsgi as my wsgi server. I’ve been dealing with this issue for several days and I think I’ve tried everything (including setting it up to call from a file instead, and even hardcoding a secret key in the settings–just for troubleshooting), but Django refuses to recognize that the variable has been set.

Note, however, that I’m a coding novice, and this is my first Django project. I could use granular, step-by-step direction to make sure I’ve done everything correctly. Or advice if you’ve run into this before.

2025-09-23 11:27:00
undefined
2025-09-23 11:27:00
undefined

Welcome @jdchuck68 !

The setting in your settings file would be SECRET_KEY, make sure that that is what you’re setting and not DJANGO_SECRET_KEY.

If this is not the issue, please copy/paste the complete error message you are getting in your logs regarding this, including the complete stack trace if one is provided.

Also post

  • Your Apache configuration for your app, including showing how you’re setting the environment variable and how you’re referencing that environment variable within your Django settings file.
  • Your wsgi.py file
  • Your settings file, or at least the SECRET_KEY section.

Also clarify what you mean by “running Apache/Nginx”? Are you running Apache behind an nginx instance? (Yes, there are reasons to do this.)

I had it arranged so that the setting, ‘SECRET_KEY,’ was looking for the variable, ‘DJANGO_SECRET_KEY’ (as well as the database password, “DJANGO_DB_KEY’). But just to make sure, I changed ‘DJANGO_SECRET_KEY’ to ‘SECRET_KEY’ in settings.py and in the config file, with no success.

I have the SetEnv text, below, as an Additional Apache Directive, which lands in the /var/www/vhosts/system/coachcpredictor.com.conf/vhost.conf file and –/vhost_ssl.conf file. I’m using Plesk, so the main configuration files are auto-generated each time Apache restarts or when the files get rebuilt. One of my questions has been how to make sure the SetEnv directive gets into the correct config file.

DocumentRoot /var/www/vhosts/coachcpredictor.com/mFBP

WSGIScriptAlias / /var/www/vhosts/coachcpredictor.com/mFBP/mFBP/wsgi.py

SetEnv SECRET_KEY [##################]
SetEnv DJANGO_DB_KEY [#########]

<Directory /var/www/vhosts/coachcpredictor.com/mFBP/mFBP>

Require all granted

Nginx is running behind Apache, serving static files.

Here is the traceback, and then the text of the wsgi.py file, then the text of the settings.py file:

2025-09-23 14:46:19 Error 5.161.41.211 mod_wsgi (pid=1160235): Failed to exec Python script file ‘/var/www/vhosts/coachcpredictor.com/mFBP/mFBP/wsgi.py’. Apache error
2025-09-23 14:46:19 Error 5.161.41.211 mod_wsgi (pid=1160235): Exception occurred processing WSGI script ‘/var/www/vhosts/coachcpredictor.com/mFBP/mFBP/wsgi.py’. Apache error
2025-09-23 14:46:19 Error 5.161.41.211 Traceback (most recent call last): Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “/var/www/vhosts/coachcpredictor.com/mFBP/mFBP/wsgi.py”, line 17, in Apache error
2025-09-23 14:46:19 Error 5.161.41.211 application = get_wsgi_application() Apache error
2025-09-23 14:46:19 Error 5.161.41.211 ^^^^^^^^^^^^^^^^^^^^^^ Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “/usr/lib/python3/dist-packages/django/core/wsgi.py”, line 12, in get_wsgi_application Apache error
2025-09-23 14:46:19 Error 5.161.41.211 django.setup(set_prefix=False) Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “/usr/lib/python3/dist-packages/django/_init_.py”, line 19, in setup Apache error
2025-09-23 14:46:19 Error 5.161.41.211 configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) Apache error
2025-09-23 14:46:19 Error 5.161.41.211 ^^^^^^^^^^^^^^^^^^^^^^^ Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “/usr/lib/python3/dist-packages/django/conf/_init_.py”, line 102, in _getattr_ Apache error
2025-09-23 14:46:19 Error 5.161.41.211 self._setup(name) Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “/usr/lib/python3/dist-packages/django/conf/_init_.py”, line 89, in _setup Apache error
2025-09-23 14:46:19 Error 5.161.41.211 self._wrapped = Settings(settings_module) Apache error
2025-09-23 14:46:19 Error 5.161.41.211 ^^^^^^^^^^^^^^^^^^^^^^^^^ Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “/usr/lib/python3/dist-packages/django/conf/_init_.py”, line 217, in _init_ Apache error
2025-09-23 14:46:19 Error 5.161.41.211 mod = importlib.import_module(self.SETTINGS_MODULE) Apache error
2025-09-23 14:46:19 Error 5.161.41.211 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “/usr/lib/python3.12/importlib/_init_.py”, line 90, in import_module Apache error
2025-09-23 14:46:19 Error 5.161.41.211 return _bootstrap._gcd_import(name[level:], package, level) Apache error
2025-09-23 14:46:19 Error 5.161.41.211 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “<frozen importlib._bootstrap>”, line 1387, in _gcd_import Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “<frozen importlib._bootstrap>”, line 1360, in _find_and_load Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “<frozen importlib._bootstrap>”, line 1331, in _find_and_load_unlocked Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “<frozen importlib._bootstrap>”, line 935, in _load_unlocked Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “<frozen importlib._bootstrap_external>”, line 995, in exec_module Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “<frozen importlib._bootstrap>”, line 488, in _call_with_frames_removed Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “/var/www/vhosts/coachcpredictor.com/mFBP/mFBP/settings.py”, line 24, in Apache error
2025-09-23 14:46:19 Error 5.161.41.211 SECRET_KEY = os.environ[“SECRET_KEY”] Apache error
2025-09-23 14:46:19 Error 5.161.41.211 ~~~~~~~~~~^^^^^^^^^^^^^^ Apache error
2025-09-23 14:46:19 Error 5.161.41.211 File “”, line 685, in _getitem_ Apache error
2025-09-23 14:46:19 Error 5.161.41.211 KeyError: ‘SECRET_KEY’ Apache error

import sys
import os

sys.path.insert(0, ‘/var/www/vhosts/coachcpredictor.com/mFBP’)
os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘mFBP.settings’)

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

import os
from pathlib import Path
from django.core.exceptions import ImproperlyConfigured

#Build paths inside the project like this: BASE_DIR / ‘subdir’.

BASE_DIR = Path(file).resolve().parent.parent

#Quick-start development settings - unsuitable for production

#See Deployment checklist | Django documentation | Django

#SECURITY WARNING: keep the secret key used in production secret!

SECRET_KEY = os.environ[“SECRET_KEY”]

if not SECRET_KEY:
raise ImproperlyConfigured(“The DJANGO_SECRET_KEY environmental variable is not set.”)

#SECURITY WARNING: don’t run with debug turned on in production!

DEBUG = False

That’s a really good question, and one I’m not able to answer.

Given that everything else I’m seeing here looks good, you could try something like a print(os.environ) after your import os in wsgi.py to see what has been passed through.