Cannot solve mod_wsgi exception in Django setup

26,783

Solution 1

If the quoted configuration about is what you are using, the error is rather obvious actually. You have:

WSGIDaemonProcess fred threads=15 display-name=%{GROUP} python-path=/home/fred/public_html/cgi-bin/fredapp/apache/
WSGIProcessGroup scratchf

It should be:

WSGIDaemonProcess fred threads=15 display-name=%{GROUP} python-path=/home/fred/public_html/cgi-bin/fredapp/apache/
WSGIProcessGroup fred

That is, the name of the process group must match.

You should though have seen an error message:

No WSGI daemon process called 'scratchf' has been configured

This would likely be before the logged error:

Exception occurred processing WSGI script

This is why it is important that you supply all the error log messages and don't assume that they aren't relevant.

Alternatively, you have quoted configuration different to what you are using or not all of the configuration.


UPDATE 1

It looks like you may have ErrorDocument directive enabled in Apache to redirect errors to a specific URL. Because however you have mounted Django at root of web server and not excluded those error URLs from being passed through to Django, then when an error is generated Django gets the redirect for the error document but it cannot resolve the URL and subsequently generates a 404. Because Apache saw a 404 for error page redirect, it then returns a 500 default error page. The end result is that true original error and any information is lost.

Thus, go into Apache configuration and comment out the ErrorDocument directives.


UPDATE 2

Change configuration to:

WSGIScriptAlias /fredapp /home/fred/public_html/cgi-bin/fredapp/apache/django.wsgi

You should not have trailing slash on the second value on line. Missed that you were actually trying to mount at sub URL and not at root of web server.

Solution 2

Is it possible that your starting directory is not the one project is in?

Today I was also setting up Apache+mod_wsgi+Django app and after adding to django.wsgi:

 os.chdir('/home/user/my_django_project')

everything started to work like a charm.

Share:
26,783
barsoomcore
Author by

barsoomcore

Updated on January 24, 2020

Comments

  • barsoomcore
    barsoomcore over 4 years

    I'm working with my hosting provider to get a Django application up and running, but neither of us are very experienced and we've basically hit a complete dead end.

    I don't have direct access to the conf file but here's how its contents have been described to me:

    <IfModule mod_wsgi.c>
    WSGIScriptAlias /fredapp/ /home/fred/public_html/cgi-bin/fredapp/apache/django.wsgi
    WSGIDaemonProcess fred threads=15 display-name=%{GROUP} python-path=/home/fred/public_html/cgi-bin/fredapp/apache/
    WSGIProcessGroup fred
    WSGIApplicationGroup %{GLOBAL}
    </IfModule>
    
    Alias /robots.txt /home/fred/public_html/fred-site/robots.txt
    Alias /favicon.ico /home/fred/public_html/fred-site/favicon.ico
    
    Alias /settings/media/ /home/fred/public_html/fred-site/media/
    

    My "django.wsgi" script is nothing fancy:

    import os, sys
    sys.path.append('/home/fred/public_html/cgi-bin/')
    sys.path.append('/home/fred/public_html/cgi-bin/fredapp/')
    os.environ['DJANGO_SETTINGS_MODULE'] = 'fredapp.settings'
    
    import django.core.handlers.wsgi
    
    application = django.core.handlers.wsgi.WSGIHandler()
    

    So my understanding is that all this means that if a request comes in for domain.com/fredapp/ that it should be turned over to the application via django.wsgi. However, the only response I get is:

    [Fri Jan 22 18:46:08 2010] [error] [client xx.xxx.xx.xx] File does not exist: /home/fred/public_html/domain.com/500.shtml
    [Fri Jan 22 18:46:08 2010] [error] [client xx.xxx.xx.xx] mod_wsgi (pid=26760): Exception occurred processing WSGI script '/home/fred/public_html/cgi-bin/fredapp/apache/django.wsgi'.
    [Fri Jan 22 18:46:03 2010] [error] [client xx.xxx.xx.xx] File does not exist: /home/fred/public_html/domain.com/404.shtml
    [Fri Jan 22 18:46:03 2010] [error] [client xx.xxx.xx.xx] File does not exist: /home/fred/public_html/domain
    

    This is running under Apache on Linux. I have tried running each line of the .wsgi script in the Python interpreter on the server, and none of them return any errors. I also tried the sys.stdout = sys.stderr trick and got no further output than what is above. The File does not exist errors have to do with the rest of the site's set-up and occur on any request. I haven't finished setting all that up properly (error pages and index pages and so on) because I'm just trying to get the app itself to run.

    I've gotten this app up and running under Apache on my own machine, though NOT in Daemon mode, but it's my first Django app, and I don't think my hosting provider has ever configured one before, so we're flying a little blind. If anyone has any suggestions, I'd be very grateful. Thank you!

  • barsoomcore
    barsoomcore over 14 years
    Thanks for the suggestion -- I tried it, but it didn't work. Still getting the same exception.
  • Graham Dumpleton
    Graham Dumpleton over 14 years
    It is bad practice to rely on current working directory for web applications because you cannot guarantee what it may be under different hosting solutions. Changing working directory like you have will not necessarily work as technically you could have other applications running in same process that do the same thing and you can screw them up or they will screw you up. Thus, always use absolute path names and ensure you set Python module search path correctly rather than relying on imports relative to current working directory.
  • user1066101
    user1066101 over 14 years
    And what? What happened? What was the resolution? If you like the question, upvote it. This isn't an answer.
  • Tomasz Zieliński
    Tomasz Zieliński over 14 years
    @Graham Dumpleton: I realize this is bad practice, but I have some code that creates some log files in current directory. It's to be refactored, but for a while I have to live with this hack.
  • barsoomcore
    barsoomcore over 14 years
    My error in transcribing, sorry. The process and process group names do match -- I've updated the configuration quote to reflect that. It's certainly possible that this quote of the configuration is incomplete, however -- as I mentioned, I don't have access to the actual file so I can't verify this information. I'll add all the output of the error log as well.
  • barsoomcore
    barsoomcore over 14 years
    Thank you. Can you clarify what you mean by "have mounted Django at root of web server"? My web server root is domain.com/ , whereas the Django app is mounted at domain.com/fredapp/ . It's very likely I'm misunderstanding, however, so if you can be more clear here, I'd appreciate it. I will check with my provider wrt ErrorDocument directives. Thanks for the input.
  • barsoomcore
    barsoomcore over 14 years
    Well, my provider insists there are no ErrorDocument directives. At this point I think I'll seek a new provider for this app.
  • Evgeny
    Evgeny almost 12 years
    @Graham, I was puzzled a bit with a similar issue - I had mismatching "name" in the WSGIDaemonProcess and WSGIProcessGroup and there was nothing in the error logs. Just an "Error 500". Could you make it clear somewhere near the beginning of the doc about WSGIDaemonProcess? - it's not clear at all from the text what "name" is expected to be. Also - is there any way to get something more useful in the logs (besides the "Error 500" message?) in this case?
  • Graham Dumpleton
    Graham Dumpleton almost 12 years
    If the exception propagates to WSGI server mod_wsgi will log it. If getting a 500 then it could be that Django is generating it and mod_wsgi will have no knowledge of original problem. In which case you need to set up ADMINS in Django settings to email you the details. Nothing mod_wsgi could do.
  • jnhghy - Alexandru Jantea
    jnhghy - Alexandru Jantea almost 10 years
    @S.Lott: well it is a kind of an answer as users how want to verify if they can fix the issue by setting the correct file rights can search the OS specific command on google... I know the answer can give a lot more info, but I wouldn't say it's not an answer...