【问题标题】:How to display images with Flask with Ubuntu and apache server on AWS EC2如何在 AWS EC2 上使用 Flask 和 Ubuntu 和 apache 服务器显示图像
【发布时间】:2021-11-18 16:46:37
【问题描述】:

我想在网站上显示图片 mouse.jpg。显示网站,但不显示图像。

我在 DigitalOcean 上使用此 guide 进行部署。它使用 apache、wsgi 和 virtualenv。

我在 stackoverflow 上找到了this,但无法将答案转移到我的问题上。这是关于写入文件而不是显示。

我还找到了 thisthat,但它们不包括像 apache 或 nginx 这样的 Web 服务器。

在 AWS 上,我设置了安全组并允许访问端口 80 和 8080。

这是我在 ec2 上的主目录中的

nextstep
├── index.html
├── nextstep.py
├── nextstep.wsgi
├── static
│   └── mouse.jpg
└── templates
    ├── nextstep.html

nextstep.py 看起来像

from flask import Flask, render_template
import os

PEOPLE_FOLDER = os.path.join('static')

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = PEOPLE_FOLDER

@app.route('/')
def hello_world():
    full_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'mouse.jpg')
    return render_template('nextstep.html',user_image = full_filename)

if __name__ == '__main__':
  app.run()

nextstep.wsgi 看起来像:

import sys
sys.path.insert(0, '/var/www/html/nextstep')

from nextstep import app as application

/etc/apache2/sites-available$ cat 000-default.conf 看起来像:

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    WSGIDaemonProcess flaskapp threads=5
    WSGIScriptAlias /test /var/www/html/flaskapp/flaskapp.wsgi

    <Directory flaskapp>
        WSGIProcessGroup flaskapp
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>

    WSGIDaemonProcess nextstep threads=5
    WSGIScriptAlias /nextstep /var/www/html/nextstep/nextstep.wsgi

    <Directory nextstep>
        WSGIProcessGroup nextstep
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>

在 EC2 /var/www/html 的树上是:

.
├── flaskapp -> /home/ubuntu/flaskapp
├── index.html
└── nextstep -> /home/ubuntu/nextstep

nextstep.html 看起来像:

<html>
<body>
Hello World from nextstep.html
{{ user_image }}
<a href="nextstep2">next step 2</a>
<img src="{{ user_image }}" alt="User Image">
<img src="/var/www/html/nextstep/static/mouse.jpg">
<img src="~/nextstep/static/mouse.jpg">
<img src="/home/nextstep/static/mouse.jpg">
<img src="home/nextstep/static/mouse.jpg">
<img src="static/mouse.jpg">
<img src="/static/mouse.jpg">
<img src="mouse.jpg">
<img src="/mouse.jpg">

</body>
</html>

【问题讨论】:

    标签: html python-3.x apache flask amazon-ec2


    【解决方案1】:

    我在这里找到了解决方案:Setting up static folder path in Flask

    nextstep.py 中是:

    app = Flask(__name__, static_url_path="", static_folder="static")
    

    nextstep.html 中是:

    <img src="mouse.jpg">
    

    【讨论】:

      猜你喜欢
      • 2017-04-10
      • 2019-02-13
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多