Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...