108

I have a django app and trying to set it up with gunicorn first and later with supervisor and nginx.

The app is running with the normal django command perfectly like python manage.py runserver

I installed the gunicorn using pip like pip install gunicorn and django version is 1.5.3

when i run the below command inside the virtual env like below

gunicorn hello.wsgi:application -b xx.xxx.xxx.xx:8000 and faced the error

Traceback (most recent call last):
  File "/root/Envs/proj/bin/gunicorn", line 9, in <module>
    load_entry_point('gunicorn==19.0.0', 'console_scripts', 'gunicorn')()
  File "/root/Envs/proj/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 74, in run
    WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  File "/root/Envs/proj/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 166, in run
    super(Application, self).run()
  File "/root/Envs/proj/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 71, in run
    Arbiter(self).run()
  File "/root/Envs/proj/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 169, in run
    self.manage_workers()
  File "/root/Envs/proj/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 477, in manage_workers
    self.spawn_workers()
  File "/root/Envs/proj/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 537, in spawn_workers
    time.sleep(0.1 * random.random())
  File "/root/Envs/proj/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 209, in handle_chld
    self.reap_workers()
  File "/root/Envs/proj/local/lib/python2.7/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>

So why actually the above error is encountered and whats the fix ?

Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313

6 Answers6

87

run guncorn with --preload can see the error log, like this

gunicorn app:application --preload -b 0.0.0.0:5000 

This will usually give you a more detailed error message.

Mark Zhang
  • 1,038
  • 1
  • 8
  • 8
  • shall we write this command exactly as it is? or shall I replace app OR application with another value? if it should be replaced, how to get the value to be put here? – Ahmed Abdallah May 25 '21 at 14:38
  • 1
    I just added --preload to my command line and it showed me the problem. – boatcoder Mar 15 '22 at 05:35
40

Actually the problem here was the wsgi file itself, previously before django 1.3 the wsgi file was named with an extension of .wsgi, but now in the recent versions it will be created with and extension of .py that is the wsgi file must be a python module

so the file should be hello_wsgi.py and command should be

gunicorn  hello:application -b xx.xxx.xxx.xx:8000
Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313
  • 13
    I had a similar issue and I had a better idea of what was the problem by running `gunicorn --log-file=-`, this will output log straight in the console. http://stackoverflow.com/a/25689349/1092815 – GabLeRoux Feb 09 '15 at 01:23
  • I recommend seeing a below answer about using the --preload arg: https://stackoverflow.com/a/55623889/3559330 – mattyb Mar 04 '21 at 19:20
14

You didn't show a full output. It probably looks like

$ gunicorn elcarweb.wsgi
[2015-10-27 21:01:47 +0000] [5429] [INFO] Starting gunicorn 19.2.1
[2015-10-27 21:01:47 +0000] [5429] [INFO] Listening at: http://127.0.0.1:8000 (5429)
[2015-10-27 21:01:47 +0000] [5429] [INFO] Using worker: sync
[2015-10-27 21:01:47 +0000] [5434] [INFO] Booting worker with pid: 5434
[2015-10-27 21:01:47 +0000] [5434] [ERROR] Exception in worker process:
Traceback (most recent call last):
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 503, in spawn_worker
    worker.init_process()
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/workers/base.py", line 116, in init_process
    self.wsgi = self.app.wsgi()
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/util.py", line 355, in import_app
    __import__(module)
ImportError: No module named elcarweb.wsgi
Traceback (most recent call last):
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 503, in spawn_worker
    worker.init_process()
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/workers/base.py", line 116, in init_process
    self.wsgi = self.app.wsgi()
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/util.py", line 355, in import_app
    __import__(module)
ImportError: No module named elcarweb.wsgi
[2015-10-27 21:01:47 +0000] [5434] [INFO] Worker exiting (pid: 5434)
Traceback (most recent call last):
  File "/home/tomek/Elcar/elcarweb/env/bin/gunicorn", line 11, in <module>
    sys.exit(run())
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 74, in run
    WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/app/base.py", line 189, in run
    super(Application, self).run()
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/app/base.py", line 72, in run
    Arbiter(self).run()
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 170, in run
    self.manage_workers()
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 473, in manage_workers
    self.spawn_workers()
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 537, in spawn_workers
    time.sleep(0.1 * random.random())
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 210, in handle_chld
    self.reap_workers()
  File "/home/tomek/Elcar/elcarweb/env/lib/python2.7/site-packages/gunicorn/arbiter.py", line 455, in reap_workers
    raise HaltServer(reason, self.WORKER_BOOT_ERROR)
gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>

Last trackback is almost the same, but before You see required informations:

ImportError: No module named elcarweb.wsgi

If You see this error, then solution is run gunicorn with --chdir=/your/app/dir parameter.

Community
  • 1
  • 1
Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
  • 2
    i.e. in your gunicorn `exec` command within **gunicorn.conf**, add `--chdir==/home/user/dir` (`dir` being the directory containing your django project). – Hassan Baig Dec 09 '15 at 14:16
  • Adding this argument didn't cut it for me, I had to `cd` to this directory first otherwise `gunicorn` had no way of finding my project. – Yaron Jan 09 '17 at 07:59
  • For me in Ubuntu 22.04 worked `--chdir /home/username/projectdir` – dainys Apr 01 '23 at 08:20
12

For anyone facing the same issue, the problem is usually something in django itself. Activate your venv and run ./manage.py runserver

This will usually give you a more detailed error message.

Vsevolod Gromov
  • 471
  • 4
  • 11
4

I also had a similar error in Ubuntu 16.04, Django 1.11, using systemd.

My problem was that I had split my settings out into separate files; ie: instead of having all settings in project/settings.py, I have a few settings files like project/settings/dev.py.

I had to update DJANGO_SETTINGS_MODULE in the project/wsgi.py file from:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")  

to

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings.dev")

then gunicorn worked.

mattdedek
  • 2,525
  • 2
  • 17
  • 19
  • I really can't believe this worked! Sounds so easy but the amount of answers and tuts I had to go through to find this gem. Thanks a bunch. – kae_screechae Feb 13 '20 at 07:15
2

I got the very same error. Gunicorn was working when executed as root, but failed this way when executed as non-privileged user. In my case I install the 'dataset' python module with 'pip install dataset' and it screwed the permissions on /usr/lib/python2.7/site-packages/dataset to be readable only by root account. On other module installed as dependency like that was normality.

Fix :

chmod -R a+rX /usr/lib/python2.7/site-packages/dataset*
chmod -R a+rX /usr/lib/python2.7/site-packages/normality*

Some another issue was with the yaml package auto-installed by pip, but I have not found what exactly. The fix was to reinstall from fedora package:

dnf -y install python-yaml
Dhia
  • 10,119
  • 11
  • 58
  • 69