Django wsgi internal server error

1,623 views
Skip to first unread message

Arjan van Eersel

unread,
Nov 26, 2015, 5:36:06 AM11/26/15
to modwsgi
I'm trying to deploy a Django site through apache 2 as a wsgi daemon. Yet I keep getting an error 500 message, but no message in the apache logs at all. How can I get more information on what causes this error?

I also made a quick helloworld wsgi app to test, here everything goes fine, so the problem seems to be related to the django app. The server uses virtualmin for management of the virtual host. And I've searched (and found) many related issues on the internet, still whatever I try I can't fix this problem. The irony is that I can run other instances of the same app without any problems on the same server. There are 3 other sites running on exactly the same app on the same server.

Virtual host settings:
SuexecUserGroup "#1237" "#1136"
ServerName aldeimbg.org
ServerAlias www.aldeimbg.org
ServerAlias admin.aldeimbg.org

#LogLevel debug

Alias /static  /home/aldeimbg/python/BalkanCMS/static
<Directory /home/aldeimbg/python/BalkanCMS/static>
   Order deny,allow
   Allow from all
</Directory>

Alias /media  /home/aldeimbg/python/UserContent/media
<Directory /home/aldeimbg/python/UserContent/media>
   Order deny,allow
   Allow from all
</Directory>

WSGIDaemonProcess aldeimbg.org python-path=/home/aldeimbg/env/lib/python2.7/site-packages
WSGIProcessGroup aldeimbg.org
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /home/aldeimbg/python/BalkanCMS/wsgi.py
WSGIPassAuthorization On

<Directory /home/aldeimbg/python/BalkanCMS>
   #Require all granted
   Order allow,deny
   Allow from all 
</Directory>

ErrorLog /var/log/virtualmin/aldeimbg.org_error_log
CustomLog /var/log/virtualmin/aldeimbg.org_access_log combined
ScriptAlias /cgi-bin/ /home/aldeimbg/cgi-bin/
ScriptAlias /awstats/ /home/aldeimbg/cgi-bin/
ScriptAlias /AutoDiscover/AutoDiscover.xml /home/aldeimbg/cgi-bin/autoconfig.cgi
ScriptAlias /Autodiscover/Autodiscover.xml /home/aldeimbg/cgi-bin/autoconfig.cgi
ScriptAlias /autodiscover/autodiscover.xml /home/aldeimbg/cgi-bin/autoconfig.cgi

<Directory /home/aldeimbg/cgi-bin>
allow from all
AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
</Directory>
RewriteEngine on
RewriteCond %{HTTP_HOST} =webmail.aldeimbg.org
RewriteRule ^(.*) https://aldeimbg.org:20000/ [R]
RewriteCond %{HTTP_HOST} =admin.aldeimbg.org
RewriteRule ^(.*) https://aldeimbg.org:10000/ [R]
RemoveHandler .php
RemoveHandler .php5
php_admin_value engine Off
IPCCommTimeout 31
FcgidMaxRequestLen 1073741824
<Files awstats.pl>
AuthName "aldeimbg.org statistics"
AuthType Basic
AuthUserFile /home/aldeimbg/.awstats-htpasswd
require valid-user
</Files>
Alias /dav /home/aldeimbg/public_html
Alias /pipermail /var/lib/mailman/archives/public
<Location /dav>
DAV on
AuthType Basic
AuthName "aldeimbg.org"
AuthUserFile /home/aldeimbg/etc/dav.digest.passwd
Require valid-user
ForceType text/plain
Satisfy All
RemoveHandler .php
RemoveHandler .php5
RewriteEngine off
</Location>
RedirectMatch /cgi-bin/mailman/([^/\.]*)(.cgi)?(.*) https://aldeimbg.org:10000/virtualmin-mailman/unauthenticated/$1.cgi$3
Redirect /mail/config-v1.1.xml /cgi-bin/autoconfig.cgi

wsgi.py:

"""
WSGI config for BalkanCMS project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
"""

import os
import sys

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "BalkanCMS.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

settings.py

DEBUG = True (tried with False too, but get the same error)

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['*',]

I can access the app through manage.py runserver without problems

Apache version info:
Server version: Apache/2.2.22 (Ubuntu)
Server built:   Jul 24 2015 17:25:42
Server's Module Magic Number: 20051115:30
Server loaded:  APR 1.4.6, APR-Util 1.3.12
Compiled using: APR 1.4.6, APR-Util 1.3.12
Architecture:   64-bit
Server MPM:     Prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APACHE_MPM_DIR="server/mpm/prefork"
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=128
 -D HTTPD_ROOT="/etc/apache2"
 -D SUEXEC_BIN="/usr/lib/apache2/suexec"
 -D DEFAULT_PIDLOG="/var/run/apache2.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_LOCKFILE="/var/run/apache2/accept.lock"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="mime.types"
 -D SERVER_CONFIG_FILE="apache2.conf"

mod_wsgi version
ii  libapache2-mod-wsgi                   3.3-4ubuntu0.2                        Python WSGI adapter module for Apache

Python version: 2.7.3

Graham Dumpleton

unread,
Nov 26, 2015, 6:30:09 AM11/26/15
to mod...@googlegroups.com
As first step, enable DEBUG mode in Django settings module to see if that fixes the issue or at least causes a description of the error in the browser.

It is quite common to get a 500 error response delivered back from Django with no record of the exception being logged. This is because Django doesn’t log exception details by default.

Graham

--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To post to this group, send email to mod...@googlegroups.com.
Visit this group at http://groups.google.com/group/modwsgi.
For more options, visit https://groups.google.com/d/optout.

Arjan van Eersel

unread,
Nov 26, 2015, 6:40:25 AM11/26/15
to modwsgi
Debug mode is enabled, still get only the error 500.

Is there a way to get or log those exception details? These details should put me on the right track to solve the issue. 

Arjan

Graham Dumpleton

unread,
Nov 26, 2015, 6:53:37 AM11/26/15
to mod...@googlegroups.com
Lets now confirm that the 500 is coming from Django or whether is from Apache.

Use a WSGI middleware wrapper described in:


to capture the request and response.

Is the application actually returning a 500, or perhaps instead returning a different error code.

There are cases where for a non 200 error code other than 500 that Apache can try and map it to an ErrorDocument but if that is mapping back into the application and also has an error, Apache will fall back to generating a 500 response.

So lets just make sure we know what is going in and out of the application and that it looks like what is expected.

Also check what gets logged in the Apache access log at the same time.

Am sleeping now, so will see what you find in the morning.

Graham

Arjan van Eersel

unread,
Nov 27, 2015, 12:10:58 AM11/27/15
to modwsgi
Nothing seems to happen, no output appears in the apache logs, but I might be applying the middleware in a wrong way. 
Here is my wsgi.py file:

"""
WSGI config for BalkanCMS project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
"""

import os
import sys

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "BalkanCMS.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

# Logging WSGI middleware.

import pprint

class LoggingMiddleware:
     def __init__(self, application):
        self.__application = application

    def __call__(self, environ, start_response):
        errors = environ['wsgi.errors']
        pprint.pprint(('REQUEST', environ), stream=errors)

        def _start_response(status, headers, *args):
            pprint.pprint(('RESPONSE', status, headers), stream=errors)
            return start_response(status, headers, *args)

        return self.__application(environ, _start_response)

application = LoggingMiddleware(application)

Graham Dumpleton

unread,
Nov 27, 2015, 11:17:20 PM11/27/15
to mod...@googlegroups.com
Ensure LogLevel in Apache configuration, at global as well as virtual host scope, is ‘info’ and not ‘warn’.

Then capture any extra messages from mod_wsgi around time of request. If mod_wsgi isn’t logging anything then it isn’t even getting to mod_wsgi.

Graham

Arjan van Eersel

unread,
Nov 28, 2015, 6:23:55 AM11/28/15
to mod...@googlegroups.com
Now I do get some response:
[Sat Nov 28 13:19:21 2015] [info] mod_wsgi (pid=21273): Shutdown requested 'aldeimbg.org'.
[Sat Nov 28 13:19:21 2015] [info] mod_wsgi (pid=21273): Stopping process 'aldeimbg.org'.
[Sat Nov 28 13:19:21 2015] [info] mod_wsgi (pid=21273): Destroying interpreters.
[Sat Nov 28 13:19:21 2015] [info] mod_wsgi (pid=21273): Cleanup interpreter ''.
[Sat Nov 28 13:19:21 2015] [info] mod_wsgi (pid=21273): Terminating Python.
[Sat Nov 28 13:19:21 2015] [info] mod_wsgi (pid=21273): Python has shutdown.
[Sat Nov 28 11:19:24 2015] [info] mod_wsgi (pid=700): Attach interpreter ''.
[Sat Nov 28 11:19:24 2015] [info] mod_wsgi (pid=700): Adding '/home/aldeimbg/env/lib/python2.7/site-packages' to path.
[Sat Nov 28 11:21:23 2015] [debug] mod_deflate.c(700): [client 195.230.7.52] Zlib: Compressed 538 to 325 : URL /
[Sat Nov 28 11:21:23 2015] [debug] mod_deflate.c(700): [client 195.230.7.52] Zlib: Compressed 538 to 325 : URL /favicon.ico, referer: http://www.aldeimbg.org/

--
You received this message because you are subscribed to a topic in the Google Groups "modwsgi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/modwsgi/YFMbpUIZ3Kg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to modwsgi+u...@googlegroups.com.

Graham Dumpleton

unread,
Nov 29, 2015, 6:32:22 PM11/29/15
to mod...@googlegroups.com
That log output doesn’t even show mod_wsgi attempting to load the WSGI script file. So if that corresponds to the time of the request, then the error is happening before mod_wsgi gets a chance to do anything.

Now you said you tried a WSGI hello world. Was that with a separate VirtualHost configuration, or did you simply swap the contents of the Django WSGI script file with a hello world?

If the issue is with the Apache configuration, then swapping the Django WSGI script file with a WSGI hello world should fail as well.

Graham

Arjan van Eersel

unread,
Dec 5, 2015, 1:17:33 AM12/5/15
to modwsgi
Hello,

Sorry for the late reply, I was away and not able to reply earlier.

I simply switched the wsgi file in the apache configuration of the virtualhost, so I changed wsgi.py to helloworld.py. That helloworld script worked fine, because when I went to the site's url I saw the message of the helloworld script on the screen.

Arjan van Eersel

unread,
Dec 9, 2015, 1:26:15 AM12/9/15
to modwsgi
I have been able to solve the issue, by changing the apache virtual conf to this:

WSGIDaemonProcess aldeimbg.org user=aldeimbg group=aldeimbg threads=25 python-path=/home/aldeimbg/python:/home/aldeimbg/env/lib/python2.7/site-packages
WSGIProcessGroup aldeimbg.org
WSGIScriptAlias / /home/aldeimbg/python/BalkanCMS/wsgi.py
WSGIPassAuthorization On

<Directory /home/aldeimbg/python/BalkanCMS>
Order deny,allow
Allow from all
</Directory>

Alias /static  /home/aldeimbg/python/BalkanCMS/static
<Directory /home/aldeimbg/python/BalkanCMS/static>
Order deny,allow
Allow from all
</Directory>

Alias /media  /home/aldeimbg/python/UserContent/media
<Directory /home/aldeimbg/python/UserContent/media>
Order deny,allow
Allow from all
</Directory>

I came to this insight thanks to the info on this page: https://code.google.com/p/modwsgi/wiki/IntegrationWithDjango.

The difference with the old apache configuration is the addition of these arguments to the WSGIDaemonProcess definition: user=aldeimbg group=aldeimbg threads=25. When I remove them and try again, the error 500 appears again, so obviously I'm obliged to explicitly define the user, group and threads.

Graham, I'd like to thank you very much for your help, which was extremely useful to find out where to start searching for a solution regarding this problem.

Arjan


On Monday, November 30, 2015 at 1:32:22 AM UTC+2, Graham Dumpleton wrote:

Graham Dumpleton

unread,
Dec 9, 2015, 6:33:44 PM12/9/15
to mod...@googlegroups.com
What version of mod_wsgi are you using now?

What are User/Group directives in Apache configuration set to?

Some versions of mod_wsgi can have an issue in daemon mode when the user Apache runs as has no actual home directory. I thought there would have been a clear error message in the logs.

Am sure that more recent mod_wsgi versions are meant to deal with this properly. Is possible if you are using an old mod_wsgi version, that Linux distros have back ported a change which has caused this problem for normal case because they didn’t bother to pickup later fix to make it more forgiving.

Graham
Reply all
Reply to author
Forward
0 new messages