Configure python path using mod_wsgi

14,646

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.

Share:
14,646
Mike Dale
Author by

Mike Dale

Updated on June 04, 2022

Comments

  • Mike Dale
    Mike Dale almost 2 years

    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>
    
  • Graham Dumpleton
    Graham Dumpleton almost 8 years
    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.
  • Mike Dale
    Mike Dale almost 8 years
    Thanks for your help. I have switched to using gunicorn and nginx. Seems easier to configure to my preferred python path.
  • P K
    P K over 4 years
    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.
  • P K
    P K over 4 years
    This could be helpful if you're using mod_wsgi with apache2 in (enterprise) Amazon Linux2 stackoverflow.com/a/57498156/11890582