4

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.

5
  • Are importing backend somewhere in your files? Is backend only a folder or also a file? Because you're trying to import the module backend somewhere and it cannot find a module that's called backend. Commented Mar 15, 2018 at 13:54
  • That's the weird thing. backend is the name of my project's folder and i didn't import it anywhere Commented Mar 15, 2018 at 15:08
  • You also didn't import it like that in your wsgi.py? Commented Mar 15, 2018 at 15:09
  • The wsgi.py is the one django brings by default. I actually don't know what that file does. I'm going to edit the post and put the file so you can see what it has inside. Commented Mar 15, 2018 at 15:12
  • What os the full path of the Django settings file, ie., path ending in backend/settings.py? The python-path should include the parent directory of that backend directory in that path. Commented Mar 15, 2018 at 20:15

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

2 Comments

You are right the problem was it didn't find the backend but if remove that it said the same but just with settings. I posted the solution that worked for me in case other noobs are as lost as me. Thanks for your help!
No problem :) glad I was able to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.