简体   繁体   中英

Gunicorn + Django dosen't work when ran from upstart doesn't get environment varaibles

  • I'm running ubuntu 14.04 on a vps at digitalocean
  • I have virtualenv and virtualenvwrapper installed
  • I've got a virtual enviroment running on python 3 called ssenv located at: /home/david/Env/ssenv/

If I go into where my manage.py file is located and run:

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

everything works fine. but If I try using the following gunicorn.conf upstart file:

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

and then run: sudo service gunicorn start

I check the error logs and see this mess:

[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)

So basically it starts, raises gunicorn.errors.HaltServer during that it raises that exception again

starts again, and raises a Django improperly configured error even though I have set my settings.py like so:

SECRET_KEY = os.environ['SECRET_KEY']

and set the environment variables in my post-activate file:

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

post_activate doesn't have anything to do with anything here. You should put them in your upstart configuration:

env DJANGO_SETTINGS_MODULE='stocksearch.settings.production'

or in the wsgi file itself:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM