Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

python - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...