Deploy wsgi application with apache mod_wgsi not working

Hi
I’m trying to run a django app called bicaehfid using apache mod_wsgi but I get the error 500:
ModuleNotFoundError: No module named ‘bicaehfid.settings’

I am using ubuntu 20.04 and apache2

I have granted permissions to www-data and checked my sys.path:
In [5]: sys.path
Out[5]:
[’/home/unidad_sig/proyectoBicaehfid/bicaehgis/bicaehfid’,
‘/usr/lib/python38.zip’,
‘/usr/lib/python3.8’,
‘/usr/lib/python3.8/lib-dynload’,
‘’,
‘/home/unidad_sig/proyectoBicaehfid/bicaehgis/.venvbica/lib/python3.8/site-packages’,
‘/home/unidad_sig/proyectoBicaehfid/bicaehgis/.venvbica/lib/python3.8/site-packages/IPython/extensions’,
‘/home/unidad_sig/.ipython’,
‘/home/unidad_sig/proyectoBicaehfid/bicaehgis/bicaehfid/bicaehfid’]

In my wsgi.py:

import os
from django.core.wsgi import get_wsgi_application
# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bicaehfid.settings')
os.environ["DJANGO_SETTINGS_MODULE"] = "bicaehfid.settings"
application = get_wsgi_application()

(I tried the original configuration and the configuration for multiple apps, although I only have one so far; I also tried setting DJANGO_SETTINGS_MODULE manually in /etc/environment).

In /etc/apache2/apache2.conf:

WSGIScriptAlias / /home/unidad_sig/proyectoBicaehfid/bicaehgis/bicaehfid/bicaehfid/wsgi.py
WSGIPythonHome /home/unidad_sig/proyectoBicaehfid/.venvbica/
WSGIPythonPath /home/unidad_sig/proyectoBicaehfid/bicaehgis/

In my default virtual host:

DocumentRoot /home/unidad_sig/proyectoBicaehfid/bicaehgis/bicaehfid/
<Directory /home/unidad_sig/proyectoBicaehfid/bicaehgis/bicaehfid>
    Order deny,allow
    Allow from all
    Require all granted
</Directory>

The full log error shows that it tries to run wsgi.py but does not reach the settings

I don`t know what I’m missing, can anyone help, please?

Thank you

  • Your WSGIPythonPath is one directory above where it needs to be.

Based on what I see from your directory structure, it should be:
WSGIPythonPath /home/unidad_sig/proyectoBicaehfid/bicaehgis/bicaehfid
(Note, no trailing slash - see the very first example at How to use Django with Apache and mod_wsgi | Django documentation | Django)

  • You don’t want to have this:

You do not want any part of your Django system exposed as directly-accessible files.

(See the examples at: Quick Configuration Guide — mod_wsgi 4.9.0 documentation)

  • Make sure that you do not have mod_python active in your apache configuration. The two do not play well together.

Thank you!
That worked

PS: I’m aware that I have to change my directories, I was just trying to make it work for the first time. Thanks for the tip, anyway

Depending upon file permissions, you might need the Directory entry. It’s the DocumentRoot entry that should be removed.

OK, that DocumentRoot wasn’t necessary, I changed it to /var/www/html and still works. Thank you again!