pinkraindancer
pinkraindancer

Reputation: 23

Configuring Apache to server fails with Error 500. error.log says "ImportError: No module named flask"

SSH to a VPS running ubuntu and installed flask, Apache, and WSGI with the following:

sudo apt-get update
sudo apt-get install python-pip
pip install --user Flask

sudo apt-get install apache2
sudo apt-get install libapache2-mod-wsgi

cloned git repo that had the hello.py program with the following code:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello World!"

if __name__ == '__main__':
    app.run(port=5000, debug=True)

created hello.wsgi file in VPS by running the following:

nano hello.wsgi

import sys
sys.path.insert(0, "/var/www/firstapp")
from hello import app as application

created apache configuration pointing to above .wsgi by running the following in VPS:

cd /etc/apache2/sites-available
nano hello.conf

<VirtualHost *>

ServerName example.com

WSGIScriptAlias / /var/www/firstapp/hello.wsgi
WSGIDaemonProcess hello
<Directory /var/www/firstapp>
WSGIProcessGroup hello
WSGIApplicatioinGroup %{GLOBAL}
Order deny,allow
Allow from all

</Directory>
</VirtualHost>

Configured Apache to serve Flask application by doing the following in VPS:

sudo a2dissite 000-default.conf
sudo a2ensite hello.conf
sudo servive apache2 reload

if I go to the VPS public IP the application fails with Error 500, when i run "sudo tail -f /var/log/apache2/error.log" I get the error "from flask import Flask ImportError:No module named flask

Upvotes: 0

Views: 276

Answers (1)

Blender
Blender

Reputation: 298472

pip install --user installs your packages into a user-specific directory that Apache doesn't know about. I would recommend you use a virtualenv but you can still use your user's site-packages if you can find its path:

python -c 'import site; print(site.USER_BASE)'

Then you configure mod_wsgi to use the path:

WSGIDaemonProcess hello python-home=/path/to/your/env

Although it is recommended that you use a virtual environment and not a per user site-packages directory, if you really must use the per user site-packages directory, use something like:

WSGIDaemonProcess hello python-path=/home/user/.local/lib/python2.7/site-packages

Upvotes: 1

Related Questions