11

I am new to the whole mod_wsgi and serving files through apache. I am really comfortable with flask but this is something i can't get my head around. I did the hello-world program and successfully displayed hello world! Now i wanted to display a image file. So I updated my hello-world.py to:

from flask import *
yourflaskapp = Flask(__name__)

@yourflaskapp.route("/")
def hello():
    file="203.jpg"
    return render_template("hello.html",file=file)
#   return"HEY"
if __name__ == "__main__":
    yourflaskapp.run()

my directory structure is something like:/var/www/hello-world

/hello-world
    test.py
    yourflaskapp.wsgi
    /static
        -203.jpg
    /templates
        -hello.html

My template is simple:

<!DOCTYPE html>
<html><head><title>hi</title></head>
<body>
<img src="{{url_for('static',filename=file)}}"/>
</body></html>

and my apache conf file is:

<VirtualHost *:80>
     WSGIDaemonProcess yourflaskapp
     WSGIScriptAlias / /var/www/hello-world/yourflaskapp.wsgi
     Alias /static/ /var/www/hello-world/static
     Alias /templates/ /var/www/hello-world/templates
     <Directory /var/www/hello-world>
            WSGIProcessGroup yourflaskapp
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
     </Directory>
        <Directory /var/www/hello-world/static>
            Order allow,deny
            Allow from all
        </Directory>
        <Directory /var/www/hello-world/templates>
            Order allow,deny
            Allow from all
        </Directory>
     ErrorLog ${APACHE_LOG_DIR}/error.log
     LogLevel warn
     CustomLog ${APACHE_LOG_DIR}/access.log combined
 </VirtualHost>

Though when i open the browser and head to my ip, it doesn't show the image file. What am i doing wrong? Is there any other approach i should follow? and if anyone could recommend any good links from where i can understand working with flask+mod_wsgi+apache2

Aarushi
  • 544
  • 1
  • 5
  • 15

1 Answers1

7

It is generally always a good idea to have trailing slash balanced when mounting static files at a sub URL. So instead of:

Alias /static/ /var/www/hello-world/static

use:

Alias /static /var/www/hello-world/static
Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • 1
    what difference does it make? – Aarushi Jul 25 '14 at 05:50
  • 4
    Because Apache will remove from the path what matches on the LHS and the result gets appended to the RHS. Thus if have '/static/foo', after stripping '/static/' if gets 'foo', which when added to the RHS yields an attempt to open '/var/www/hello-world/staticfoo', which isn't correct as the slash has been dropped. I can't remember the exact scenario it occurs or whether it relates to specific Apache versions and so are simply suggesting you make it match to avoid problems. – Graham Dumpleton Jul 25 '14 at 06:57
  • Did this solution work? Cause I am facing the same issue. Thanks. – Ricardo Silva Nov 02 '17 at 16:52
  • 1
    @RicardoSilva If you haven't already, just create a new question for your problem and explain the details of how you have things. That is better than asking whether a suggest solution for someone else's issue worked as your issue could be completely different, but with no details no one can tell. – Graham Dumpleton Nov 03 '17 at 11:03