Thursday, November 2, 2017

Missing stack trace in Python Flask on CentOS

To redirect Flask app traceback (exception, logging.error() etc.) to Apache's error log, add

import app
app.logger.addHandler(logging.StreamHandler(sys.stderr))

Otherwise all I get was a "500 Internal Server Error" with no debug information anywhere in the server.

For some strange reason, with Apache 2.4 on CentOS 7 using mod_wsgi and Python Flask, traceback actually goes to /var/log/httpd/ssl_error_log instead of /var/log/apache2/error.log on Ubuntu Server (even with SSL).

There are some other strange things with CentOS too, like the site would be accessible via http, but when https is used it would throw "Internal Server Error" unless the permissions of the folder that hold the Flask code are changed to allow R and X for user apache. It makes sense that Apache requires R and X, but that doesn't explain why it would work for http but not https.