Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_wsgi: Exception occurred processing WSGI script (django deployment)

I'm trying to deploy a django project with the following Apache configuration:

Apache virtualhost configuration

<Virtualhost *:80>
    DocumentRoot /var/www/project/backend
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    WSGIDaemonProcess backend python-home=/var/www/project/myenv python-path=/var/www/gestor_documental/backend

    WSGIProcessGroup backend
    WSGIScriptAlias / /var/www/project/backend/backend/wsgi.py process-group=backend
    Alias /static/ /var/www/project/backend/static/

    <Directory /var/www/project/backend>
            Require all granted
    </Directory>
</Virtualhost>

wsgi.load file

LoadModule wsgi_module "/var/www/project/myenv/lib/python3.5/site-packages/mod_wsgi/server/mod_wsgi-py35.cpython-35m-x86_64-linux-gnu.so"
WSGIPythonHome "/var/www/project/myenv"

The wsgi.py is the one django brings by default

wsgi.py

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings")

application = get_wsgi_application()

This is the project tree:

project
|- backend
|  |- api (django app)
|  |- backend
|     |- ...
|     |- settings.py
|     |- wsgi.py
|-- myenv (virtualenv)

And this is the error log I keep getting when i try to load the web:

mod_wsgi (pid=38953): Failed to exec Python script file '/var/www/project/backend/backend/wsgi.py'.
mod_wsgi (pid=38953): Exception occurred processing WSGI script '/var/www/project/backend/backend/wsgi.py'.
Traceback (most recent call last):
 File "/var/www/project/backend/backend/wsgi.py", line 16, in <module>
  application = get_wsgi_application()
 File "/var/www/project/myenv/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
  django.setup(set_prefix=False)
 File "/var/www/project/myenv/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
  configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
 File "/var/www/project/myenv/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
  self._setup(name)
 File "/var/www/project/myenv/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
  self._wrapped = Settings(settings_module)
 File "/var/www/project/myenv/lib/python3.5/site-packages/django/conf/__init__.py", line 97, in __init__
  mod = importlib.import_module(self.SETTINGS_MODULE)
 File "/var/www/project/myenv/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 944, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
 File "<frozen importlib._bootstrap>", line 986, in _gcd_import
 File "<frozen importlib._bootstrap>", line 969, in _find_and_load
 File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'backend'

I've followed every single tutorial and try tons of configurations but still getting the same error.

I'm using python 3.5.2 in a virtualenv and Apache 2.4.18

I've installed mod_wsgi via pip3.

Could someone help me with this and tell me what am i doing wrong?

Thank you.

like image 821
Alberto Avatar asked Nov 05 '25 21:11

Alberto


2 Answers

I solved it changing the wsgi.py file adding the next lines:

import sys

sys.path.append('/var/www/project/backend')

I don't know if this is the correct answer but it actually worked with this workaround.

like image 58
Alberto Avatar answered Nov 08 '25 14:11

Alberto


Its this line in the wsgi.py:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings")

There's no backend.settings in project/backend/backend. It's trying to find a third backend folder, so remove backend from that line.

like image 41
King Reload Avatar answered Nov 08 '25 14:11

King Reload



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!