1
votes

I've searched, tried and ran almost every tutorial available to run my django application with apache or even nginx (most of which are over 3 or 4 years old).

I've managed to pull through and get parts of everything to work with nginx until I gave up, now I'm trying with apache2, the only error so far I'm having is with wsgi.py, everything else works fine, but running

sudo python3 -i /var/www/shinra/shinra/wsgi.py

gives me the following error:

Traceback (most recent call last):
  File "/var/www/shinra/shinra/wsgi.py", line 23, in <module>
    application = get_wsgi_application()
  File "/home/ubuntu/.local/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/django/conf/__init__.py", line 110, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'shinra'

My wsgi.py is set as follows:

import os
import sys

PROJECT_DIR = '/var/www/shinra'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shinra.settings")
def execfile(filename):
     globals = dict( __file__ = filename )
     exec( open(filename).read(), globals )

activate_this = os.path.join( PROJECT_DIR, 'env/shinra/bin', 'activate_this.py' )
execfile( activate_this )

from django.core.wsgi import get_wsgi_application application = get_wsgi_application()

I've also tried running it with the default wsgi.py to no avail. As for the rest of the project, it's just a brand new django project, nothing else in there has been modified other than putting the virtual envirnoment inside /var/www/shinra/env/shinra/bin. Thank you, I've been fighting with this for a while and I'm new to django. Running this with apache2 obviously yields the same error in the apache logs.

EDIT: It would also be good to mention that I'm trying to run this in an amazon web services EC2 Instance

UPDATE:

I tried doing everything from scratch using gunicorn again, so far it works if im on my virtual environment and run

/home/ubuntu/venvs/shinra/bin/gunicorn -c /home/ubuntu/shinra/scripts/gunicorn_config.py shinra.wsgi

But if I to set that using supervisor I get this error:

[2017-06-21 05:15:25 +0000] [10320] [INFO] Starting gunicorn 19.7.1
[2017-06-21 05:15:25 +0000] [10320] [INFO] Listening at: http://0.0.0.0:8000 (10320)
[2017-06-21 05:15:25 +0000] [10320] [INFO] Using worker: sync
[2017-06-21 05:15:25 +0000] [10323] [INFO] Booting worker with pid: 10323
[2017-06-21 05:15:25 +0000] [10323] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/home/ubuntu/venvs/shinra/lib/python3.5/site-packages/gunicorn/arbiter.py", line 578, in spawn_worker
    worker.init_process()
  File "/home/ubuntu/venvs/shinra/lib/python3.5/site-packages/gunicorn/workers/base.py", line 126, in init_process
    self.load_wsgi()
  File "/home/ubuntu/venvs/shinra/lib/python3.5/site-packages/gunicorn/workers/base.py", line 135, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/home/ubuntu/venvs/shinra/lib/python3.5/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/home/ubuntu/venvs/shinra/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/home/ubuntu/venvs/shinra/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/home/ubuntu/venvs/shinra/lib/python3.5/site-packages/gunicorn/util.py", line 352, in import_app
    __import__(module)
ImportError: No module named 'shinra'
[2017-06-21 05:15:25 +0000] [10323] [INFO] Worker exiting (pid: 10323)
[2017-06-21 05:15:25 +0000] [10320] [INFO] Shutting down: Master
[2017-06-21 05:15:25 +0000] [10320] [INFO] Reason: Worker failed to boot.

The gunicorn config file is:

command = '/home/ubuntu/venvs/shinra/bin/gunicorn'
pythonpath = '/home/ubuntu/venvs/shinra/bin/python'
bind = '0.0.0.0:8000'
workers = 3
1
You can't run a wsgi.py directly with Python usually. For Apache the Django instructions are at docs.djangoproject.com/en/1.11/howto/deployment/wsgi/modwsgi There is nothing in your question to suggest what you set up, so is impossible to give any guidance on what may not have worked for Apache.Graham Dumpleton

1 Answers

1
votes

I finally figured it out! My configuration for the supervisor was missing a few parameters:

[program:shinra_gunicorn]
command=/home/ubuntu/venvs/shinra/bin/gunicorn -c /home/ubuntu/shinra/gunicorn_config.py shinra.wsgi
directory=/home/ubuntu/shinra
environment=PATH="/home/ubuntu/venvs/shinra/bin"
autostart=true
autorestart=true
stderr_logfile=/var/log/shinra.err.log
stdout_logfile=/var/log/shinra.out.log