简体   繁体   English

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

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

I would like to deploy flask app on AWS EC2. 我想在AWS EC2上部署flask应用程序。 But I have encountered 500 Internal Server Error . 但是我遇到了500 Internal Server Error

First, I have installed apache webserver and mod_wsgi. 首先,我安装了apache webserver和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

I've installed pip3 and flask. 我已经安装了pip3和flask。

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

This is the flask.wsgi file in the flaskapp directory. 这是flaskapp目录中的flask.wsgi文件。

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

from flaskapp  import app as application

I've make the mod_wsgi enable. 我启用了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>

Finally, I have restarted apache2. 最后,我重新启动了apache2。

$ sudo apachectl restart

When I go to AWS EC2 domain, I got an 500 Internal Server Error . 当我转到AWS EC2域时,出现500 Internal Server Error

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.

My flaskapp should run on python3. 我的flaskapp应该在python3上运行。

I don't know how to handle this issue. 我不知道该如何处理。

Similar type of question has been answered before . 之前已经回答过类似类型的问题。

Quoting from the answer: 引用答案:

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. 问题本质上是您在虚拟环境中安装了Flask以及其他可能的必需库,但是python(wsgi接口)与未安装这些额外库的系统python一起运行。

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. 显然,解决此问题的一种方法是使用site包,将site-packages从venv添加到执行的Python中。 This would go in your .wsgi file. 这将放在您的.wsgi文件中。

import site

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM