0

I've been following the instructions on http://flask.pocoo.org/docs/0.11/deploying/mod_wsgi/ to get my Flask application to be deployed on Apache through WSGI on a Raspberry Pi running Raspbian, but whatever I try, I keep getting a 404-error on the location I've specified. I used the info on various webpages I could find (especially the instructions on http://www.ashokraja.me/post/Serving-a-Python-Flask-Web-Application-via-Apache-Webserver-in-Raspberry-Pi.aspx), but nothing seems to work out.

Logged in as root, I created a directory /var/www/flasktest in which I put two files:

flasktest.py, containing:

#!/usr/bin/python

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

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

and flasktest.wsgi containing:

#!/usr/bin/python

import sys
sys.path.insert(0, '/var/www/flasktest')

from flasktest import app as application

I then did a chmod ugo+x on both files (don't know if that's necessary, though).

I also created /etc/apache2/sites-available/001-flasktest.conf containing

<VirtualHost *:80>
  ServerName localhost

  WSGIDaemonProcess flasktest threads=5
  WSGIScriptAlias /flasktest/ /var/www/flasktest/flasktest.wsgi

  <Directory /var/www/flask/flasktest>
    WSGIProcessGroup flasktest
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
  </Directory>
</VirtualHost>

I executed a2enmod wsgi, a2ensite 001-flasktest.conf and service apache2 reload. Every command was executed as root user.

When I go now go to http://localhost/flasktest I get a 404-error. http://localhost gives me the default Apache-page, so apparently Apache is running correctly. As far as I can see, Apache does not generate any error messages at all.

I really don't know what goes wrong: what makes the 404 come up? Can somebody help me out here? Thanks in advance!

Phil Laners
  • 11
  • 1
  • 3
  • Do you perhaps need to put `app.run()` in your WSGI file? I'm not familiar with WSGI _or_ Apache, so I'm not much help here, but that could be something to try. – squirl Jul 12 '16 at 19:17
  • shouldn't you be going to just localhost rather than going to the absolute path where you files are saved ? After all your app.route is defined as '/' – bhaskarc Jul 12 '16 at 19:38

2 Answers2

1

Double-check your WSGIScriptAlias variable- That should be (according to the docs)

WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi 

Whereas you have

WSGIScriptAlias /yourapplicaiton/ /var/www/yourapplication/yourapplication.wsgi

As Philip Tzou also mentioned, your Directory path may not be correct, either.

Further, sometimes the server name localhost can give you problems. Use 127.0.0.1:80 instead.

sytech
  • 29,298
  • 3
  • 45
  • 86
  • The solution turned out to be in a combination of your two suggestions (although I did not have to specify the port number after 127.0.0.1), and in the mistake that @PhilipTzou pointed me at. Thanks a lot! – Phil Laners Jul 12 '16 at 20:01
  • Awesome :) glad to hear, happy to help. – sytech Jul 12 '16 at 20:02
0

It looks like you passed a wrong path to <Directory /> accidentally. Try change /etc/apache2/sites-available/001-flasktest.conf to this:

<Directory /var/www/flasktest>
...
</Directory>

Also you can take a look of apache error log to see what exactly happened.

Philip Tzou
  • 5,926
  • 2
  • 18
  • 27