0

I'm gonna set a django app on a ubuntu server in a virtual host.
I have configured my virtual host so that something comes up!
But it persists on giving me 500 internal error because of some issues as you see here:

mod_wsgi (pid=31972): Target WSGI script '/var/www/mmcba/mmemp/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=31972): Exception occurred processing WSGI script '/var/www/mmcba/mmemp/wsgi.py'.
Traceback (most recent call last):
  File "/var/www/mmcba/mmemp/wsgi.py", line 25, in <module>
    application = django.core.handlers.wsgi.WSGIHandler()
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/wsgi.py", line 151, in __init__
    self.load_middleware()
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 80, in load_middleware
    middleware = import_string(middleware_path)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/module_loading.py", line 20, in import_string
    module = import_module(module_path)
  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 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/middleware.py", line 4, in <module>
    from django.contrib.auth.backends import RemoteUserBackend
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/backends.py", line 4, in <module>
    from django.contrib.auth.models import Permission
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/models.py", line 4, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/base_user.py", line 52, in <module>
    class AbstractBaseUser(models.Model):
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 110, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 247, in get_containing_app_config
    self.check_apps_ready()
  File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 125, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

I really appreciate your responses


Update #2

if I use this code in my wsgi.py:

import os

from django.core.wsgi import get_wsgi_application

os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
application = get_wsgi_application()

stack trace will become this:(mmemp is my app's name):

 mod_wsgi (pid=5579): Target WSGI script '/var/www/mmcba/mmemp/wsgi.py' cannot be loaded as Python module.
 mod_wsgi (pid=5579): Exception occurred processing WSGI script '/var/www/mmcba/mmemp/wsgi.py'.
 Traceback (most recent call last):
   File "/var/www/mmcba/mmemp/wsgi.py", line 16, in <module>
     application = get_wsgi_application()
   File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
     django.setup(set_prefix=False)
   File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 22, in setup
     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 56, in __getattr__
     self._setup(name)
   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 41, in _setup
     self._wrapped = Settings(settings_module)
   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 110, in __init__
     mod = importlib.import_module(self.SETTINGS_MODULE)
   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
     __import__(name)
 ImportError: No module named mmemp.settings

My Virtual Host Config., Here it is:

Listen 80

NameVirtualHost 148.251.218.202:80

<VirtualHost *:80>
        ServerAdmin webmaster@mmcba.com
        ServerName mmcba.com
        ServerAlias www.mmcba.com

        WSGIScriptAlias / /var/www/mmcba/mmemp/wsgi.py

        <Directory /var/www/mmcba/mmemp>
        <Files wsgi.py>
        Require all granted
        </Files>
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>
Arshamnm
  • 35
  • 9
  • Which version of django are you using? – akhilsp Aug 13 '17 at 01:57
  • Django 1.10 using Python 3.5.2 – Arshamnm Aug 13 '17 at 08:45
  • Show the mod_wsgi configuration you are using. It doesn't look like you have told mod_wsgi where you project is located. – Graham Dumpleton Aug 13 '17 at 20:44
  • I've added my virtual host configuration and also updated my stack trace --after reversing some mistakes. it's nearly a new error. Would you please take a look at the second stack trace again. & thank you for your response – Arshamnm Aug 13 '17 at 21:26
  • You should use daemon mode of mod_wsgi, not embedded mode. You then aren't telling mod_wsgi where your project code is located as per the Django documentation. See http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html and https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/modwsgi/ – Graham Dumpleton Aug 13 '17 at 22:33
  • It still gives me same errors :( – Arshamnm Aug 14 '17 at 09:57
  • Update the configuration in your question to show what you are using now. – Graham Dumpleton Aug 14 '17 at 10:15
  • Got it. I added project and app dirs to sys.path and finally it found settings module. Thank you – Arshamnm Aug 14 '17 at 10:23

2 Answers2

2

If you're using django 1.7+ do replace contents of your wsgi.py with this:

import os

from django.core.wsgi import get_wsgi_application

os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
application = get_wsgi_application()

OR try adding the following lines to settings.py

import django
django.setup()

There is a similar question here. Hope it helps.

akhilsp
  • 1,063
  • 2
  • 13
  • 26
2

Solution:
Add project and App dir to sys.path in wsgi.py and it'll work.
It works for every unknown module in your app

import sys
sys.path.append('<Project_Directory>')
sys.path.append('<Project_Directory>/AppName')
Arshamnm
  • 35
  • 9