Understanding and Resolving WSGI Application Not Found Error

Hi everyone,

I’m new to Django and have recently set up my first project. While attempting to deploy my Django application using Apache and mod_wsgi, I encountered the following error message:

Target WSGI script '/path/to/myproject/wsgi.py' does not contain WSGI application 'application'

This error has been a roadblock in getting my site up and running, and despite searching through various resources, I haven’t been able to resolve it. Here’s the setup I followed:

  1. Project Structure:

    myproject/
        ├── myproject/
        │   ├── __init__.py
        │   ├── settings.py
        │   ├── urls.py
        │   └── wsgi.py
        ├── manage.py
        └── app/
            ├── __init__.py
            ├── models.py
            ├── views.py
            └── ...
    
  2. Apache Configuration:

    <VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName example.com
        ServerAlias www.example.com
    
        DocumentRoot /path/to/myproject
        WSGIScriptAlias / /path/to/myproject/myproject/wsgi.py
    
        <Directory /path/to/myproject/myproject>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    
        Alias /static /path/to/myproject/static
        <Directory /path/to/myproject/static>
            Require all granted
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    
  3. wsgi.py File:

    import os
    from django.core.wsgi import get_wsgi_application
    
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
    application = get_wsgi_application()
    

Troubleshooting Steps I’ve Taken:

  • Verified that the DJANGO_SETTINGS_MODULE is correctly pointing to myproject.settings.
  • Ensured that wsgi.py is correctly placed in the project directory.
  • Checked that all necessary permissions are set for the project directory and files.
  • Restarted the Apache server multiple times after making configuration changes.

Despite these efforts, the error persists. I suspect that there might be a misconfiguration or a missing step that I have overlooked. I would appreciate any guidance or suggestions on how to resolve this issue.

Questions:

  1. Could there be an issue with how my project directory is structured?
  2. Are there any additional configurations needed in the wsgi.py file?
  3. Have I missed any crucial steps in setting up the Apache configuration?

Idioms Thank you in advance for your help!
Looking forward to your insights!

Best regards,
Emily

Welcome @modismos !

First, I suggest you use the real file contents involved here and not try to edit them. (e.g., use the real directories instead of trying to anonymizing them by using names like /path/to/.) This avoids having to go back-and-forth to correct typos that may occur. Using the real names also sometimes reveal other issues that may not otherwise be obvious.

Specifically:

Keep in mind that Apache needs to walk the complete directory tree starting from root (/) in order to locate these files. Setting up these projects in /home directories generally ends up being problematic as a result. My recommendation is that projects get deployed to something in /var/www or /opt to ensure no issues are created by the use of higher-level directories.

Side note: I don’t see any configuration for either a virtual environment or for using it in daemon mode. I always recommend both of these as part of any deployment. (While I consider them important, I don’t believe either of these would be causing this specific issue. I suggest you do them both, but as a general practice and not as an attempt to get this deployment up-and-running.)

Appologise in advance if this would be better in a new topic, but it seems like such a similar issue i hope keeping it here makes sense.

I have two different django applications running on the same server. One of the sites is working, but the new one i am trying to set up is giving me this same error:

Target WSGI script 'C:/vault90/vault90/wsgi.py' does not contain WSGI application 'application'.

The file at this location looks like this however:

import os
import sys
import logging
import traceback
from django.core.wsgi import get_wsgi_application

# Add your project directory to the sys.path
sys.path.append('C:/vault90')

# Set the settings module for the project
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'vault90.settings')

# Setup logging for WSGI errors
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

try:
	# Get the WSGI application
	application = get_wsgi_application()
except Exception:
    logger.error('WSGI failed to start', exc_info=True)
    traceback.print_exc()

and this is my vhosts.conf:

<VirtualHost *:80>
    ServerName example2.com
    ServerAlias www.example2.com
    DocumentRoot "C:/vault90"

    # Point to the WSGI application
    WSGIScriptAlias / C:/vault90/vault90/wsgi.py

    LogLevel info wsgi:debug
    <Directory C:/vault90/vault90>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    # Serve static files
    Alias /static/ C:/vault90/static/
    <Directory C:/vault90/static>
        Require all granted
    </Directory>

    # Logs for debugging
    ErrorLog "C:/Apache24/logs/vault90_error.log"
    CustomLog "C:/Apache24/logs/vault90_access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot "C:/tbg"

    # Point to the WSGI application
    WSGIScriptAlias / C:/tbg/tbg/wsgi.py

    LogLevel info wsgi:debug
    <Directory C:/tbg/tbg>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    # Serve static files
    Alias /static/ C:/tbg/static/
    <Directory C:/tbg/static>
        Require all granted
    </Directory>

    # Log files
    ErrorLog "C:/Apache24/logs/tbg_error.log"
    CustomLog "C:/Apache24/logs/tbg_access.log" common
</VirtualHost>

and my python path is here:

LoadFile "C:/Users/joeb/AppData/Local/Programs/Python/Python313/python313.dll"
LoadModule wsgi_module "C:/Users/joeb/AppData/Local/Programs/Python/Python313/Lib/site-packages/mod_wsgi/server/mod_wsgi.cp313-win_amd64.pyd"
WSGIPythonHome "C:/Users/joeb/AppData/Local/Programs/Python/Python313"

I am sorry this is not super organized. but happy to edit if there is feedback or more details i should provide to make this more a logical oriented issue to resolve.

Welcome @Rev !

Sorry, actually it doesn’t. We prefer separating individual issues to avoid confusion in conversations. It can be difficult to follow the diagnostic process if it’s intermixed with a conversation on another topic. Also, posts in this thread notifies everyone else participating in the thread, which may then involve notifications on a topic they’re not following.

I do need to ask you to open a new topic for this.