2

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>

4 Answers 4

4

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 .

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

2

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.

4 Comments

Using python-path on WSGIDaemonProcess will not help as mod_wsgi must be compiled for the specific Python installation you want to use. You cannot mix stuff from two different versions and even mixing from different installations of same Python version can be a problem if they weren't configured in the same way. Anaconda Python is not likely to be compatible with system Python. Would need to rebuild mod_wsgi against Anaconda Python.
Thanks for your help. I have switched to using gunicorn and nginx. Seems easier to configure to my preferred python path.
Yes. Integrating it with gunicorn and nginx is recommended option. COming back to your question, you seems to have installed anaconda3 correctly (pointing to python3), but the mod_wsgi is not installed into python3. I think pip3 install mod_wsgi and/or sudo apt-get install python3-flask should have overcome this error.
This could be helpful if you're using mod_wsgi with apache2 in (enterprise) Amazon Linux2 stackoverflow.com/a/57498156/11890582
-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.