trouble running flask on apache/mod_wsgi


Question

I'm trying to run a basic helloworld with apache2 and mod_wsgi but depsite following the tutorial from flask documentation all i got is an error 500.

Everything is in /var/www/myapp

myapp.wsgi

from yourapplication import app as application

/etc/apache2/site-availables/default

<VirtualHost *:80>
ServerName mydomain

WSGIDaemonProcess myap user=web group=www-data  threads=5
WSGIScriptAlias / /var/www/mydomain/myap.wsgi

<Directory /var/www/myapp>
    WSGIProcessGroup myap
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

hello.py (flask app)

#!/usr/bin/env python
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def hello():
    return render_template('hello.html')

 if __name__ == '__main__':
     app.run()
1
1
9/5/2013 4:31:23 PM

Accepted Answer

First, you should configure some kind of logging so you can see the actual exception behind the 500 response.

Based on your code, you may be generating an ImportError in your WSGI file when you attempt to import app from yourapplication instead of hello. Try:

from hello import app as application

You also have a couple of spots where you use myap instead of myapp, if what you posted here matches what's on your server. Either way, logging your errors should confirm it.

2
6/19/2012 1:19:47 PM

I notice 3 things:

  1. Use the same name for your .wsgi and .py
  2. Import your own program in the .wsgi module
  3. You misspelled myapp to myap.wsgi in the apache configuration

Please make sure your code works using the Flask's webserver before trying to deploy to Apache. Your final code should look like this to work:

hello.wsgi

import sys
sys.path.insert(0, '/path/to/your/application')
from hello import app as application

httpd.conf

<VirtualHost *:80>
ServerName mydomain

WSGIDaemonProcess myapp user=web group=www-data  threads=5
WSGIScriptAlias / /var/www/mydomain/myapp.wsgi

<Directory /var/www/mydomain>
    WSGIProcessGroup myapp
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

And finally, if any error occur you can track them in your Apache's folder, inside Logs/error_log. Turn on your debug mode in Flask as Graham said and you will see any error in your application and better explain here. Basically you just have to change this in your hello.py:

if __name__ == '__main__':
    app.run(debug= True)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon