无法通过主管启动Django / Gunicorn

时间:2016-01-27 20:29:22

标签: python django virtualenv gunicorn supervisor

我试图使用Supervisor和Gunicorn在Debian服务器上启动Django项目。当我运行命令" sudo supervisorctl start gunicorn"时,我收到以下错误:

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)

我的base.py基本设置文件读取我在&#34; hash1&#34;中设置的环境变量。虚拟环境的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...

我已经确认&#34; NotperlyConfigured&#34;此功能在设置文件中生成错误消息。

以下是我的主管和gunicorn文件:

# /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

我使用默认的wsgi.py和supervisord.conf文件。我的版本如下:

Django 1.8.4
Debian 8.2
Supervisor 3.0r1-1  (Installed globally)
Gunicorn 19.3.0     (Installed in virtual environment "hash1")

谁能看到我做错了什么?我不希望主管可以看到我的环境变量,这就是为什么我通过主管&#34;环境&#34;设置MEDIA_ROOT变量的原因。参数。但主管似乎仍然没有看到它。当我从命令行运行start-gunicorn脚本时,我能够启动gunicorn。我知道我做了一些愚蠢的事情,但我无法看到它。

谢谢!

1 个答案:

答案 0 :(得分:0)

我不确定,但我认为问题在于您在 root 用户环境中定义 MEDIA_ROOT ,然后以 smith <运行程序/ strong>用户,并且在其环境中未定义 MEDIA_ROOT 。 也许您可以尝试编辑 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)

然后启动django并检查错误消息以检查用户是否在其环境中定义了 MEDIA_ROOT