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
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:
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/mytestThe setup that worked is:
Apache configuration:
wsgi.py:
settings.py, installed_aps:
urls.py:
views.py:
It should rather be:
instead of: