0

I have set up a basic site in Django and am trying to serve it using Apache 2, however when trying i get an error:

[Tue Jul 07 08:03:50.274206 2020] [mpm_event:notice] [pid 21614:tid 140492525521856] AH00489: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Tue Jul 07 08:03:50.274292 2020] [core:notice] [pid 21614:tid 140492525521856] AH00094: Command line: '/usr/sbin/apache2'
[Tue Jul 07 08:03:55.841352 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] mod_wsgi (pid=21616): Target WSGI script '/home/ubuntu/Django/customspiritco/wsgi.py' cannot be loaded as Python module.
[Tue Jul 07 08:03:55.841401 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] mod_wsgi (pid=21616): Exception occurred processing WSGI script '/home/ubuntu/Django/customspiritco/wsgi.py'.
[Tue Jul 07 08:03:55.841626 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] Traceback (most recent call last):
[Tue Jul 07 08:03:55.841655 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport]   File "/home/ubuntu/Django/customspiritco/wsgi.py", line 3, in <module>
[Tue Jul 07 08:03:55.841661 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport]     from django.core.wsgi import get_wsgi_application
[Tue Jul 07 08:03:55.841669 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport]   File "/home/ubuntu/Django/env/lib/python3.6/site-packages/django/__init__.py", line 1, in <module>
[Tue Jul 07 08:03:55.841745 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport]     from django.utils.version import get_version
[Tue Jul 07 08:03:55.841768 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] ImportError: cannot import name 'get_version'

My installed version are: Apache/2.4.29 (Ubuntu), mod_wsgi/4.5.17, Python/3.6, Django/2.2.10

My djangoproject.conf for apache2 is:

<VirtualHost *:80> 
 ServerAdmin admin@customspirit.co
 ServerName customspirit.co 
 DocumentRoot /home/ubuntu/Django 

 <Directory /home/ubuntu/Django>
   Require all granted
 </Directory>  

 Alias /static /home/ubuntu/Django/static_in_env
 
 <Directory /home/ubuntu/Django/static_in_env> 
  Options FollowSymLinks
  Order allow,deny
  Allow from all
  Require all granted 
 </Directory> 

 Alias /media /home/ubuntu/Django/media_root
 
 <Directory /home/ubuntu/Django/media_root> 
  Options FollowSymLinks
  Order allow,deny
  Allow from all
  Require all granted 
 </Directory> 

 
 # adjust the following line to match your Python path 
 WSGIDaemonProcess customspiritco python-home=/home/ubuntu/Django/env python-path=/home/ubuntu/Django
 WSGIProcessGroup customspiritco
 WSGIScriptAlias / /home/ubuntu/Django/customspiritco/wsgi.py 
 
 <Directory /home/ubuntu/Django/custompspiritco>
  <Files wsgi.py>
   Require all granted
  </Files>
 </Directory>

</VirtualHost>

The init.py file where the error is looks like:

from django.utils.version import get_version

VERSION = (2, 2, 10, 'final', 0)

__version__ = get_version(VERSION)


def setup(set_prefix=True):
    """
    Configure the settings (this happens as a side effect of accessing the
    first setting), configure logging and populate the app registry.
    Set the thread-local urlresolvers script prefix if `set_prefix` is True.
    """
    from django.apps import apps
    from django.conf import settings
    from django.urls import set_script_prefix
    from django.utils.log import configure_logging

    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
    if set_prefix:
        set_script_prefix(
            '/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME
        )
    apps.populate(settings.INSTALLED_APPS)

The wgsi.py file looks like:

 import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'customspiritco.settings.production')

application = get_wsgi_application()

I get an Internal Server Error(500) every time i try and load anything and the error repeats in the log.

I have now reinstalled Django to version 2.2.14 which has solved the issue and brought a new issue

[Tue Jul 07 09:03:28.291015 2020] [wsgi:error] [pid 25208:tid 139979472283392] [remote myip:myport] ModuleNotFoundError: No module named 'django'

After compiling mod_wgsi for my version of python this error has now been replaced by:

[Tue Jul 07 10:43:39.699154 2020] [wsgi:info] [pid 1305:tid 140703057624000] mod_wsgi (pid=1305): Adding '/home/ubuntu/Django' to path.
[Tue Jul 07 10:43:39.699699 2020] [mpm_event:debug] [pid 1305:tid 140702956263168] event.c(2218): AH02471: start_threads: Using epoll (wakeable)
[Tue Jul 07 10:44:08.714222 2020] [authz_core:debug] [pid 1304:tid 140702826608384] mod_authz_core.c(809): [client myip:myport] AH01626: authorization result of Require all granted: granted
[Tue Jul 07 10:44:08.714265 2020] [authz_core:debug] [pid 1304:tid 140702826608384] mod_authz_core.c(809): [client myip:myport] AH01626: authorization result of <RequireAny>: granted
[Tue Jul 07 10:44:08.714312 2020] [authz_core:debug] [pid 1304:tid 140702826608384] mod_authz_core.c(809): [client myip:myport] AH01626: authorization result of Require all granted: granted
[Tue Jul 07 10:44:08.714319 2020] [authz_core:debug] [pid 1304:tid 140702826608384] mod_authz_core.c(809): [client myip:myport] AH01626: authorization result of <RequireAny>: granted
[Tue Jul 07 10:44:08.727681 2020] [wsgi:info] [pid 1303:tid 140702905882368] mod_wsgi (pid=1303): Create interpreter 'customspirit.co|'.
[Tue Jul 07 10:44:08.740572 2020] [wsgi:info] [pid 1303:tid 140702905882368] mod_wsgi (pid=1303): Adding '/home/ubuntu/Django' to path.
[Tue Jul 07 10:44:08.740929 2020] [wsgi:info] [pid 1303:tid 140702905882368] [remote myip:myport] mod_wsgi (pid=1303, process='customspiritco', application='customspirit.co|'): Loading Python script file '/home/ubuntu/Django/customspiritco/wsgi.py'.
[Tue Jul 07 10:44:08.741202 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport] mod_wsgi (pid=1303): Failed to exec Python script file '/home/ubuntu/Django/customspiritco/wsgi.py'.
[Tue Jul 07 10:44:08.741229 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport] mod_wsgi (pid=1303): Exception occurred processing WSGI script '/home/ubuntu/Django/customspiritco/wsgi.py'.
[Tue Jul 07 10:44:08.741396 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport] Traceback (most recent call last):
[Tue Jul 07 10:44:08.741421 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport]   File "/home/ubuntu/Django/customspiritco/wsgi.py", line 3, in <module>
[Tue Jul 07 10:44:08.741427 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport]     from django.core.wsgi import get_wsgi_application
[Tue Jul 07 10:44:08.741441 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport] ModuleNotFoundError: No module named 'django'
  • are you sure get_version() is from django.utils.version? You're using Django 2.2, but I don't see django.utils having the version submodule in https://docs.djangoproject.com/en/2.2/ref/utils/ – ewokx Jul 07 '20 at 08:28
  • I can see the version.py file in the utils folder plus the Django server will run without Apache which seems to indicate that the contents of the _init_.py file are correct – AHopefulFool Jul 07 '20 at 08:35
  • Yeah, the documentation and the code don't seem to correspond properly. Is your mod_wsgi compiled against the right python version? (in this case python3.6)? perhaps https://stackoverflow.com/questions/6454564/target-wsgi-script-cannot-be-loaded-as-python-module might help you? – ewokx Jul 07 '20 at 08:54
  • This might also help: https://modwsgi.readthedocs.io/en/develop/user-guides/installation-issues.html – ewokx Jul 07 '20 at 08:58
  • is django installed in the /home/ubuntu/Django/env environment or is it in the general overall system env? – ewokx Jul 07 '20 at 09:07
  • After much trial and error I have found that I did have the wrong mod_wgsi, now I have compiled it for my Python I have been met with a new error above – AHopefulFool Jul 07 '20 at 10:39

1 Answers1

0

After many hours of frustration it was all solved by creating a new virtual environment owned by the user ubuntu. Previously i must have made it using sudo and that had caused the issue of not finding the Django module