skip to Main Content

I am trying to move my django project to production using apache2 and mod_wsgi. I keep getting a wsgi_error "ModuleNotFoundError" in the apache logs.

I run on ubuntu 22.04.

After going through the various posts on the web I still am not getting anywhere, so I have now started from scratch by creating a new example django app. The same error occurs.

PS, When I use "runserver" the django app displays fine in the browser. Also a simple wsgi "hello_world" app runs fine in apache. So I think the basic setup is fine.

I have created a django test application "mytest" with django-admin startproject mytest

The project is created in /home/user/myprojects/mytest, the group of all directories is www-data

In mytest I have the usual django structure

home/user/myprojects/mytest
    manage.py
    mytest
      asgi.py
      __init__.py
      settings.py
      urls.py
      wsgi.py

The wsgi.py is:
(The django/python runs in a virtual environment I found that I needed to add code to the wsgi to actually activate the virtual environment)

"""
WSGI config for mytest project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/
"""

python_home='/home/user/.virtualenvs/myproject'
activate_this= python_home + '/bin/activate_this.py'
with open(activate_this)as f:
    code=compile(f.read(),activate_this,'exec')
    exec(code, dict(__file__=activate_this))

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mytest.settings')

application = get_wsgi_application()

The apache 000-default.conf is:

        WSGIDaemonProcess mytest
        WSGIProcessGroup mytest
        WSGIScriptAlias /mytest /home/user/myprojects/mytest/mytest/wsgi.py
        <Directory /home/user/myprojects/mytest/mytest>
                Require all granted
        </Directory>

With this setup if I run "python manage.py runserver my_ip_addr" (my_ip_addr added to allowed hosts in the settings file) I can browse to my_ip_addr and the "install worked succesfully" is displayed

Running through standard web (apache) shows:
ModuleNotFoundError: No module named ‘mytest’

If I take out the django and get_wsgi_aplication part and add code to display Hello_World, like:

import os
def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

Then indeed hello_world is displayed

However, I cannot find why the wsgi mytest application does not run

2

Answers


  1. Chosen as BEST ANSWER

    I think I solved it finally, due to this post:

    Django+Apache ModuleNotFoundError: No module named 'myproject'

    I had to add the project folder paths to the wsgi.py as follows:

    sys.path.append('home/user/project/mytest')
    sys.path.append('home/user/project/mytest/mytest')
    

    The link to the django-served page is then: http://ip_addr/mytest/mytest

    The the comment by @NixonSparrow about the WSGIScriptAlias also made sense: changing that to WSGIScriptAlias / /home/user/project/mytest/mytest/wsgi.py then changed the browser link to http://ip_addr/mytest

    The setup that worked is:

    Apache configuration:

    WSGIScriptAlias / /home/user/project/mytest/mytest/wsgi.py
    <Directory /home/user/project/mytest/mytest>
            Require all granted
    </Directory>
    

    wsgi.py:

    python_home='/home/user/.virtualenvs/project'
    activate_this= python_home + '/bin/activate_this.py'
    with open(activate_this)as f:
        code=compile(f.read(),activate_this,'exec')
        exec(code, dict(__file__=activate_this))
    
    import os
    import sys
    sys.path.append('home/user/project/mytest')
    sys.path.append('home/user/project/mytest/mytest')
    
    from django.core.wsgi import get_wsgi_application
    
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mytest.settings')
    
    application = get_wsgi_application()
    

    settings.py, installed_aps:

    INSTALLED_APPS = [
        'mytest',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]
    

    urls.py:

    urlpatterns = [
        path('mytest/',views.index,name='index'),
        path('admin/', admin.site.urls),
    ]
    

    views.py:

    from django.shortcuts import render
    from django.http import HttpResponse
    # Create your views here.
        def index(request):
            return HttpResponse("<h1>MyTest Hello World ....</h1>")
    

  2. It should rather be:

    WSGIScriptAlias / /home/user/myprojects/mytest/mytest/wsgi.py
    

    instead of:

    WSGIScriptAlias /mytest /home/user/myprojects/mytest/mytest/wsgi.py
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search