首页 > 解决方案 > 无法访问部署在 Apache2 上的本地静态资源 Flask 应用程序

问题描述

我已经在 Ubuntu 16.04 x64(阿里巴巴云)的 Apache2 上使用 mod_wsgi 部署了一个简单的 Flask 应用程序。我在同一个 ubuntu 服务器实例上使用 mysql 作为我的数据库服务器。

我将本地静态资源文件保存在应用程序的静态目录中。每当我调用远程资源时,它都很好,但我的本地资源(即 css 文件、图像等)并没有发生同样的事情。

我知道默认情况下,静态目录对于烧瓶来说是静态的。在我的本地计算机中,一切正常。这是我尝试从 jinja2 模板获取静态资源的方式。

<div id="loader-div2"><img src={{ url_for('static',filename='simple_gif.gif') }}></div>

在我看来(myapp.py)到 jinja2 模板,这里是 url 端点:

app = Flask(__name__,static_url_path='/static')
@app.route('/')
def home():
    return render_template('my_template.html')
# I've called this at the end
app.run(debug=False)

在我的 wsgi 文件中

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

from myapp import app as application

文件夹结构

/var/www/FlaskApp
|-myapp.py
|-flaskapp.wsgi
| |-static
|    |-simple_gif.gif
| |-templates
|   |-my_template.html

问题回顾: 无法访问 Apache2 服务器中的静态资源,但可以在本地机器中访问。如果我尝试执行http://XX.XX.XX.XX/static/simple_gif.gif ,我总是会得到 404 状态。出于同样的原因,我也无法访问那里的 css 文件。我被严重困在这里。任何帮助将不胜感激。

提前致谢

编辑 这是 apache 的配置

<VirtualHost *:80>
ServerName 127.0.0.1
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/>
  Order allow,deny
  Allow from all
  Require all granted
</Directory>
Alias /static /var/www/FlaskApp/static
<Directory /var/www/FlaskApp/static/>
  Order allow,deny
  Allow from all
  Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined  </VirtualHost>

Edit2 我已经解决了这个问题。这是另一个让 apache 感到困惑的 apache conf 文件。非常感谢您的关注。

标签: flaskapache2ubuntu-16.04mod-wsgi

解决方案


推荐阅读