Flask website — 500 Internal Server Error

情到浓时终转凉″ 提交于 2019-12-22 12:49:08

问题


I cannot for the life of me figure of why this flask application I'm trying to launch is not working. I am running it on a $5 Digital Ocean droplet. Here's (hopefully) everything you need to know about it:

Directory layout (contained within /var/www/):

FlaskApp
    FlaskApp
        __init__.py
        static
        templates
        venv
    flaskapp.wsgi

__init__.py:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "yay it worked"

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

flaskapp.wsgi:

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

from FlaskApp import app as application
application.secret_key = 'Add your secret key'

FlaskApp.conf (contained in /etc/apache2/sites-availble):

<VirtualHost *:80>
        ServerName the.ip.blah.blah
        ServerAdmin admin@mywebsite.com
        WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
        <Directory /var/www/FlaskApp/FlaskApp/>
            Order allow,deny
            Allow from all
        </Directory>
        Alias /static /var/www/FlaskApp/FlaskApp/static
        <Directory /var/www/FlaskApp/FlaskApp/static/>
            Order allow,deny
            Allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

venv was created from calling virtualenv venv within /var/www/FlaskApp/FlaskApp/. I installed flask in venv using pip install flask after entering venv using source venv/bin/activate.

Wsgi has been enabled (a2enmod wsgi). FlaskApp.conf was enabled (a2ensite FlaskApp). And, finally, I restarted apache many times, but to no success (service apache2 restart).

I was following this guide on how to set up a flask application.

Here is a screenshot of what my error looks like:

Any help on getting this to work would be greatly appreciated.

Thanks in advance.

EDIT: I found the problem: ImportError: No module named flask. This is a little strange since I did do pip install flask within the virtualenv. When I just open a python console session in the virtualenv and try import flask I get no error, so not sure what's going on.

Also, how is this application even using venv? I don't see it getting accessed anywhere so how is it even using it? Perhaps this is why i'm getting the ImportError, because I only have flask installed on the virtualenv but it's not being used?


回答1:


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.

I have very little recent experience running Python on Apache (I come from an era of mod_python and cgi), but 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')



回答2:


I think the best way to solve your problem is to add tell your wsgi file about your virtual environment and activate it: put the following code in your your flaskapp.wsgi

activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

and restart apache. hope it will help! find more here



来源:https://stackoverflow.com/questions/36656702/flask-website-500-internal-server-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!