I am trying to set up a simple flask app on an ec2 instance with apache2 server and mod_wsgi. Seem to be having disproportionate amount of diffculty configuring the correct python path for mod_wsgi to use.
I have placed code snippets below.
The error I get the apache2 log is:
Traceback (most recent call last):
File "/var/www/html/flaskapp_tut/flaskapp_tut.wsgi", line 7, in <module>
from flaskapp_tut import app as application
] File "/var/www/html/flaskapp_tut/flaskapp_tut.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
flask is definitely installed via anaconda installation, however clearly the wrong version of python is being used by mod_wsgi.
The log files says its using: apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
However I am using python 3.x, and the anaconda installation show when I use the command "which python" ie /home/ubuntu/anaconda3/bin/python
the mod_wsgi documentation says you can configure the python path with: WSGIPythonHome /home/ubuntu/anaconda3/bin/python, however I do not know where to place this configuration.
Would appreciate any assistance. This seems as though it should be a lot more straightforard than it is, according to the steps I am using as a guide: http://www.datasciencebytes.com/bytes/2015/02/24/running-a-flask-app-on-aws-ec2/
flaskapp_tut.wsgi
#!/home/ubuntu/anaconda3/bin/python
import sys
sys.path.insert(0, '/var/www/html/flaskapp_tut')
sys.path.append('/home/ubuntu/anaconda3/bin/python')
from flaskapp_tut import app as application
flaskapp_tut.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello from Flask!'
if __name__ == '__main__':
app.run()
Settings within the 000-default.conf file
DocumentRoot /var/www/html
WSGIDaemonProcess flaskapp_tut threads=5
WSGIScriptAlias / /var/www/html/flaskapp_tut/flaskapp_tut.wsgi
<Directory flaskapp_tut>
WSGIProcessGroup flaskapp_tut
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
mod_wsgi/3.4 Python/2.7.6
That's a fairly old mod_wsgi, and what this is telling you is that the mod_wsgi you have installed is compiled against Python/2.7.6.
I recommend you get a current mod_wsgi, and make sure it's compiled against python-3.x.
Also, (and I don't think this will solve your problem, but it's worth mentioning) you can specify a python-path as an argument to WSGIDaemonProcess
. This may help you get it to at least see the right stuff (and may be cleaner in some scenarios than putting that sys.path.append()
in your code). See here: http://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html .
It works for me in this way (Debian 9):
First , install mod-wsgi compiled for python3
sudo apt-get install libapache2-mod-wsgi-py3
Second, add the python virtual env. lib. path in wsgi script: (start-jobs-backend.wsgi) . add also function application
#!/usr/bin/python3
import sys
import os
sys.path.insert(0, '/path/to/backend')
sys.path.insert(0, '/path/to/venv/lib/python3.7/site-packages')
def application(environ, start_response):
status = '200 OK'
output = b'Hello World! \n'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
from yourproject import app as application
Third , apache conf
<VirtualHost>
WSGIDaemonProcess backend user=www-data group=www-data threads=5
WSGIScriptAlias / /path/to/backend/start-jobs-backend.wsgi
DocumentRoot /path/to/backend
<Files start-jobs-backend.wsgi>
Require all granted
</Files>
<Directory /path/to/backend>
AddHandler wsgi-script .wsgi
WSGIProcessGroup backend
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Options Indexes FollowSymLinks MultiViews ExecCGI
Require all granted
</Directory>
</VirtualHost>
I spent one week to work it out for my first flask app with wsgi deployement option, hope it can help you .
I recently installed mod_wsgi by building it from source
mod_wsgi Quick Configuration Guide
mod_wsgi Configuring The Source Code
I want to use Python 3.7, so I installed that (from source), and the location of my Python3.7 executable (on Ubuntu 18.04) is:
/usr/local/bin/python3.7
This location can be found by typing:
which python3.7
During the configuring of mod_wsgi, this is what I ran exactly, to make sure that my mod_wsgi is compiled against the installed Python3.7. When I initially ran just ./configure
, mod_wsgi
would use the Python2.7 executable by default.
./configure --with-python=/usr/local/bin/python3.7
If you use virtual environment highly recommend to use following format:
import logging
import sys
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '/home/Flask/Api/')
sys.path.append('/home/Flask/fenv/lib/python3.6/site-packages/')
from run import app as application
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.