gpt4 book ai didi

python - 当从 Upstart 运行时没有获得环境变量时,Gunicorn + Django 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 17:22:32 25 4
gpt4 key购买 nike

  • 我在 digitalocean 的 vps 上运行 ubuntu 14.04
  • 我安装了virtualenvvirtualenvwrapper
  • 我有一个在 python 3 上运行的虚拟环境,名为 ssenv位于:/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'

最佳答案

post_activate 与这里的任何内容都没有任何关系。您应该将它们放入您的 upstart 配置中:

env DJANGO_SETTINGS_MODULE='stocksearch.settings.production'

或者在 wsgi 文件本身中:

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

关于python - 当从 Upstart 运行时没有获得环境变量时,Gunicorn + Django 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32990628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com