Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving static files through apache

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

like image 514
Aarushi Avatar asked Jul 14 '14 15:07

Aarushi


1 Answers

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
like image 63
Graham Dumpleton Avatar answered Oct 07 '22 13:10

Graham Dumpleton