0

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

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • 1
    Possible duplicate of [Flask ImportError: No Module Named Flask](https://stackoverflow.com/questions/31252791/flask-importerror-no-module-named-flask) – Aran-Fey Jan 27 '18 at 21:02
  • 1
    This is not a duplicate of linked issue. The answer already given is spot on in that issue was because OP installed packages to per user site-packages directory. The linked issue was due to different reasons. Do not close this as duplicate as will loose the correct answer for this variation. – Graham Dumpleton Jan 29 '18 at 02:31

1 Answers1

1

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
Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
Blender
  • 289,723
  • 53
  • 439
  • 496