简体   繁体   中英

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

I would like to deploy flask app on AWS EC2. But I have encountered 500 Internal Server Error .

First, I have installed apache webserver and 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.

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

This is the flask.wsgi file in the flaskapp directory.

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

from flaskapp  import app as application

I've make the mod_wsgi enable.

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.

$ sudo apachectl restart

When I go to AWS EC2 domain, I got an 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.

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.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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