如何在 AWS EC2 上部署 Flask 应用程序 - 内部服务器错误?

How to deploy flask app on AWS EC2 - Internal Server Error?

我想在 AWS EC2 上部署 Flask 应用程序。但是我遇到了500 Internal Server Error.

首先,我安装了 apache 网络服务器和 mod_wsgi。

$ sudo apt-get update
$ sudo apt-get install apache2
$ sudo apt-get install libapache2-mod-wsgi
$ sudo apt-get install libapache2-mod-wsgi-py2

我已经安装了 pip3 和 flask。

$ sudo apt-get install python3-pip
$ sudo pip3 install flask

这是 flaskapp 目录中的 flask.wsgi 文件。

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

from flaskapp  import app as application

我已经启用 mod_wsgi。

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

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

终于,我重启了apache2。

$ sudo apachectl restart

当我访问 AWS EC2 域时,我收到 500 内部服务器错误

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

我的 flaskapp 应该 运行 python3。

我不知道如何处理这个问题。

类似类型的问题已

引用自回答:

The problem is essentially that you are installing Flask, and possibly other required libraries, in a virtual environment but the python (wsgi interface) is running with the system python which does not have these extra libraries installed.

apparently one way to handle this is to use the site package to add the site-packages from your venv to the Python that is executed. This would go in your .wsgi file.

import site

site.addsitedir('/path/to/your/venv/lib/pythonX.X/site-packages')