Gunicorn + Django在从新贵开始运行时没有得到环境变量

时间:2015-10-07 11:07:56

标签: python django ubuntu gunicorn upstart

  • 我在digitalocean的vps上运行 ubuntu 14.04
  • 我安装了 virtualenv virtualenvwrapper
  • 我有一个名为ssenv python 3 上运行的虚拟环境,位于:/home/david/Env/ssenv/

如果我进入manage.py文件所在的位置并运行:

gunicorn --workers 3 --bind unix:/home/david/StockSearch/stocksearch/stocksearch.sock stocksearch.wsgi:application
一切正常。 但如果我尝试使用以下gunicorn.conf upstart文件:

start on runlevel [2345]
stop on runlevel [!2345]

respawn
setuid david
setgid www-data
chdir /home/david/StockSearch/stocksearch

exec /home/david/Env/ssenv/bin/gunicorn --workers 3 --bind unix:/home/david/StockSearch/stocksearch/stocksearch.sock stocksearch.wsgi:application

然后运行:sudo service gunicorn start

我检查错误日志并看到这个混乱:

[2015-10-07 07:01:22 -0400] [8882] [INFO] Starting gunicorn 19.3.0
[2015-10-07 07:01:22 -0400] [8882] [INFO] Listening at: unix:/home/david/StockSearch/stocksearch/stocksearch.sock (8882)
[2015-10-07 07:01:22 -0400] [8882] [INFO] Using worker: sync
[2015-10-07 07:01:22 -0400] [8886] [INFO] Booting worker with pid: 8886
[2015-10-07 07:01:22 -0400] [8887] [INFO] Booting worker with pid: 8887
[2015-10-07 07:01:22 -0400] [8888] [INFO] Booting worker with pid: 8888
(ssenv)david@stockmeup:~/StockSearch/stocksearch$ sudo tail -30 /var/log/upstart/gunicorn.log 
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/arbiter.py", line 459, in reap_workers
    raise HaltServer(reason, self.WORKER_BOOT_ERROR)
gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/david/Env/ssenv/bin/gunicorn", line 11, in <module>
    sys.exit(run())
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/app/wsgiapp.py", line 74, in run
    WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/app/base.py", line 189, in run
    super(Application, self).run()
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/app/base.py", line 72, in run
    Arbiter(self).run()
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/arbiter.py", line 201, in run
    self.halt(reason=inst.reason, exit_status=inst.exit_status)
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/arbiter.py", line 297, in halt
    self.stop()
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/arbiter.py", line 342, in stop
    time.sleep(0.1)
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/arbiter.py", line 214, in handle_chld
    self.reap_workers()
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/arbiter.py", line 459, in reap_workers
    raise HaltServer(reason, self.WORKER_BOOT_ERROR)
gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
[2015-10-07 07:01:24 -0400] [8904] [INFO] Starting gunicorn 19.3.0
[2015-10-07 07:01:24 -0400] [8904] [INFO] Listening at: unix:/home/david/StockSearch/stocksearch/stocksearch.sock (8904)
[2015-10-07 07:01:24 -0400] [8904] [INFO] Using worker: sync
[2015-10-07 07:01:24 -0400] [8908] [INFO] Booting worker with pid: 8908
(ssenv)david@stockmeup:~/StockSearch/stocksearch$ sudo tail -30 /var/log/upstart/gunicorn.log 
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/django/conf/__init__.py", line 113, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
Traceback (most recent call last):
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/arbiter.py", line 507, in spawn_worker
    worker.init_process()
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/workers/base.py", line 118, in init_process
    self.wsgi = self.app.wsgi()
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/gunicorn/util.py", line 355, in import_app
    __import__(module)
  File "/home/david/StockSearch/stocksearch/stocksearch/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    django.setup()
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/django/__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/django/conf/__init__.py", line 48, in __getattr__
    self._setup(name)
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/david/Env/ssenv/lib/python3.4/site-packages/django/conf/__init__.py", line 113, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
[2015-10-07 07:01:28 -0400] [8960] [INFO] Worker exiting (pid: 8960)

所以基本上它开始了,引发了gunicorn.errors.HaltServer 在此期间,它再次提出了这个例外

再次启动,并引发Django配置不正确的错误,即使我已设置我的settings.py如下:

SECRET_KEY = os.environ['SECRET_KEY']

并在我的后激活文件中设置环境变量:

export DJANGO_SETTINGS_MODULE='stocksearch.settings.production'
export SECRET_KEY='obscured sensitive info'

1 个答案:

答案 0 :(得分:1)

post_activate与此处的任何内容都没有任何关系。你应该把它们放在你的新贵配置中:

env DJANGO_SETTINGS_MODULE='stocksearch.settings.production'

或在wsgi文件中:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "stocksearch.settings.production")