Reputation: 14270
I'm trying to start a Django project on a Debian server using Supervisor and Gunicorn. When I run the command "sudo supervisorctl start gunicorn", I'm getting the following error:
Traceback (most recent call last):
File "/home/smith/venvs/hash1/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 507, in spawn_worker
worker.init_process()
File "/home/smith/venvs/hash1/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 118, in init_process
self.wsgi = self.app.wsgi()
File "/home/smith/venvs/hash1/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/home/smith/venvs/hash1/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
return self.load_wsgiapp()
File "/home/smith/venvs/hash1/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
return util.import_app(self.app_uri)
File "/home/smith/venvs/hash1/local/lib/python2.7/site-packages/gunicorn/util.py", line 355, in import_app
__import__(module)
File "/srv/http/example.com/repo/conf/wsgi.py", line 28, in <module>
application = get_wsgi_application()
File "/home/smith/venvs/hash1/local/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "/home/smith/venvs/hash1/local/lib/python2.7/site-packages/django/__init__.py", line 17, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/home/smith/venvs/hash1/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/home/smith/venvs/hash1/local/lib/python2.7/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/home/smith/venvs/hash1/local/lib/python2.7/site-packages/django/conf/__init__.py", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/srv/http/example.com/repo/conf/settings/prod.py", line 2, in <module>
from conf.settings.base import *
File "/srv/http/example.com/repo/conf/settings/base.py", line 128, in <module>
MEDIA_ROOT = get_env_variable('MEDIA_ROOT')
File "/srv/http/example.com/repo/conf/settings/base.py", line 23, in get_env_variable
raise ImproperlyConfigured(error_msg)
ImproperlyConfigured: Set the MEDIA_ROOT environment variable
[2016-01-27 19:39:31 +0000] [30408] [INFO] Worker exiting (pid: 30408)
My base.py base settings file reads environment variables which I set in my "hash1" virtual environment's postactivate hook:
# base.py
def get_env_variable(var_name):
try:
return os.environ[var_name]
except KeyError:
error_msg = "Set the %s environment variable" % var_name
raise ImproperlyConfigured(error_msg)
# The rest of my settings...
I've confirmed that the "ImproperlyConfigured" error message is being generated by this function in the settings file.
Here are my supervisor and gunicorn files:
# /etc/supervisor/conf.d/supervisor.conf
[program:gunicorn]
command=/srv/http/example.com/repo/bin/start-gunicorn
directory=/srv/http/example.com/repo
user=root
environment=MEDIA_ROOT="/var/www/swing/media/"
# start-gunicorn
#!/bin/bash
NAME=example
DJANGODIR=/srv/http/example.com/repo
USER=smith
GROUP=smith
WEBSITE=$NAME
NUM_WORKERS=3
DJANGO_SETTINGS_MODULE=conf.settings.prod
DJANGO_WSGI_MODULE=conf.wsgi
cd /home/smith/venvs/current/bin
source activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER \
--group=$GROUP \
--bind=127.0.0.1:8000 \
--access-logfile /var/log/gunicorn/access.log \
--error-logfile /var/log/gunicorn/error.log \
--pid /var/tmp/gunicorn.pid \
--log-level=info
I am using the default wsgi.py and supervisord.conf files. My versions are as follows:
Django 1.8.4
Debian 8.2
Supervisor 3.0r1-1 (Installed globally)
Gunicorn 19.3.0 (Installed in virtual environment "hash1")
Can anyone see what I'm doing wrong? I wouldn't expect that supervisor could see my environment variables which is why I've set the MEDIA_ROOT variable via supervisor's "environment" parameter. But supervisor still doesn't seem to be seeing it. I am able to start gunicorn when I run the start-gunicorn script from the command line. I know I'm doing something stupid but I can't see it.
Thanks!
Upvotes: 0
Views: 403
Reputation: 500
I am not sure, but I think the problem is that you are defining MEDIA_ROOT in the root user environment, then you run your program as smith user and it doesn't have MEDIA_ROOT defined in its environment. Maybe you can try editing base.py
def get_env_variable(var_name):
try:
return os.environ[var_name]
except KeyError:
error_msg = "Set the %s environment variable" % var_name
# For debugging process append username and environment vars to error_msg
user = os.environ['USER']
env_vars = os.environ.keys()
error_msg += " current user: %s env_vars: %s" % (user, env_vars)
raise ImproperlyConfigured(error_msg)
Then start django and check the error message to check if the user has MEDIA_ROOT defined in its environment.
Upvotes: 0