Serving a Hello World page using Flask and Apache / mod_wsgi on an Ubuntu server

NOTE. I found five questions related to broken implementations of "hello world on flask / mod_wsgi". I looked through all of them and cannot find a solution to my own broken implementation. Therefore, please do not rush to close this question as a duplicate.

I am running a cloud-based Ubuntu VM. He is currently launching the LAMP stack. In my webroot, I have media and several HTML files, such as http://pipad.org/ball/multiball.html - everything works fine.

Now I'm trying to use the http://pipad.org/foo page dynamically generated by the Python backend using Flask and Apache / mod_wsgi.

Unfortunately, going to my browser at http://pipad.org/foo causes the URL not to be found.

Here are my files:

pi@PiDroplet:~/web/piFlask$ pwd
/home/pi/web/piFlask

pi@PiDroplet:~/web/piFlask$ ls -l
total 24
-rw-r--r-- 1 root root  628 May 15 02:37 apache.conf
-rwxrwxr-x 1 pi   pi    153 May 15 02:39 piFlask.py
-rwxrwxr-x 1 pi   pi    379 May 15 02:38 piFlask.wsgi
drwxrwxr-x 5 pi   pi   4096 May 14 09:06 venv

piFlask.py

from flask import Flask
app = Flask(__name__)

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

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

piFlask.wsgi

# http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/#creating-a-wsgi-file
# http://www.enigmeta.com/2012/08/16/starting-flask/

import os, sys

PROJECT_DIR = '/home/pi/web/piFlask'

activate_this = os.path.join(PROJECT_DIR, 'bin', 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
sys.path.insert(0, PROJECT_DIR)

from piFlask import app as application

apache.conf

pi@PiDroplet:~/web/piFlask$ cat apache.conf 
# Look in http://wiki.apache.org/httpd/DistrosDefaultLayout & find equiv to /usr/local/apache2/conf/httpd.conf
# for me (Ubuntu) it /etc/apache2/apache2.conf
#
# append: 
#    Include /path/to/this/file

# http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/#configuring-apache
<VirtualHost *>
    ServerName pipad.org

    WSGIDaemonProcess piFlask user=pi group=pi threads=5
    WSGIScriptAlias /foo /home/pi/web/piFlask/piFlask.wsgi

    <Directory /home/pi/web/piFlask>
        WSGIProcessGroup piFlask
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

And according to the comment in the above configuration file, ...

pi@PiDroplet:~/web/piFlask$ tail -n 2 /etc/apache2/apache2.conf 

Include /home/pi/web/piFlask/apache.conf

Can anyone see what I am missing?

+4
source share
2 answers

You probably need to set the variable APPLICATION_ROOTin '/foo'in the Flask config .

piFlask.py

from flask import Flask
app = Flask(__name__)
app.config['APPLICATION_ROOT'] = '/foo'

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

This should fix:

<VirtualHost *:80>

Note:

  • http://pipad.org/ - right now gives me access to your directory structure one level above piFlask.
  • Flask "/". , , "/foo" . this.
+2

All Articles