Issue
I'm installing a previously built website on a new server. I'm not the original developer.
I've used Gunicorn + nginx in the past to keep the app alive (basically following this tutorial), but am having problems with it here.
I source venv/bin/activate
, then ./manage.py runserver 0.0.0.0:8000
works well and everything is running as expected. I shut it down and run gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
, and get the following:
[2016-09-13 01:11:47 +0000] [15259] [INFO] Starting gunicorn 19.6.0
[2016-09-13 01:11:47 +0000] [15259] [INFO] Listening at: http://0.0.0.0:8000 (15259)
[2016-09-13 01:11:47 +0000] [15259] [INFO] Using worker: sync
[2016-09-13 01:11:47 +0000] [15262] [INFO] Booting worker with pid: 15262
[2016-09-13 01:11:47 +0000] [15262] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker
worker.init_process()
File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/workers/base.py", line 126, in init_process
self.load_wsgi()
File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi
self.wsgi = self.app.wsgi()
File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
return self.load_wsgiapp()
File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
return util.import_app(self.app_uri)
File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/util.py", line 357, in import_app
__import__(module)
ImportError: No module named 'myproject.wsgi'
[2016-09-13 01:11:47 +0000] [15262] [INFO] Worker exiting (pid: 15262)
[2016-09-13 01:11:47 +0000] [15259] [INFO] Shutting down: Master
[2016-09-13 01:11:47 +0000] [15259] [INFO] Reason: Worker failed to boot.
I believe it has something to do with the structure of the whole application. Before, I've built apps with the basic structure of:
myproject
├── manage.py
├── myproject
│ ├── urls.py
│ ├── views.py
│ ├── component1
│ │ ├── urls.py
│ │ └── views.py
│ ├── component2
│ │ ├── urls.py
│ │ └── views.py
├── venv
│ ├── bin
│ └── ...
This one, instead, has a structure like:
myproject
├── apps
│ ├── blog
│ │ ├── urls.py
│ │ ├── views.py
│ │ └── ...
│ ├── catalogue
│ │ ├── urls.py
│ │ ├── views.py
│ │ └── ...
│ ├── checkout
│ │ ├── urls.py
│ │ ├── views.py
│ │ └── ...
│ ├── core
│ │ ├── urls.py
│ │ ├── views.py
│ │ └── ...
│ ├── customer
│ ├── dashboard
│ └── __init__.py
├── __init__.py
├── manage.py
├── project_static
│ ├── assets
│ ├── bower_components
│ └── js
├── public
│ ├── emails
│ ├── media
│ └── static
├── settings
│ ├── base.py
│ ├── dev.py
│ ├── __init__.py
│ ├── local.py
│ └── production.py
├── templates
│ ├── base.html
│ ├── basket
│ ├── blog
│ └── ....
├── urls.py
├── venv
│ ├── bin
│ ├── include
│ ├── lib
│ ├── pip-selfcheck.json
│ └── share
└── wsgi.py
So, there's no 'main' module running the show, which is what I expect gunicorn is looking for.
Any thoughts?
wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
application = get_wsgi_application()
Solution
Your error message is
ImportError: No module named 'myproject.wsgi'
You ran the app with
gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
And wsgi.py has the line
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
This is the disconnect. In order to recognize the project as myproject.wsgi
the parent directory would have to be on the python path... running
cd .. && gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
Would eliminate that error. However, you would then get a different error because the wsgi.py file refers to settings
instead of myproject.settings
. This implies that the app was intended to be run from the root directory instead of one directory up. You can figure this out for sure by looking at the code- if it uses absolute imports, do they usually say from myproject.app import ...
or from app import ...
. If that guess is correct, your correct commmand is
gunicorn --bind 0.0.0.0:8000 wsgi:application
If the app does use myproject
in all of the paths, you'll have to modify your PYTHONPATH to run it properly...
PYTHONPATH=`pwd`/.. gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
Answered By - Paul Becotte
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.