0

I'm trying to deploy a flask application with Apache and mod_wsgi.

I followed the instructions on the official flask documentation.

I installed Apache and mod_wsgi (that I'm not really sure it's installed for python 3.5, but I can't find any way to check).

I created a virtual environment for my application myapp with all the necessary dependencies installed (using pip).

virtualenv -p python3 env

I created a myapp.wsgi file as suggested in the previous link:

activate_this = '/var/www/myapp/env/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

from project import app as application

I created a vhost for my application

<VirtualHost *:80>

    ServerName myapp.com

    WSGIScriptAlias / /var/www/myapp/myapp.wsgi

    <Directory /var/www/myapp>
            Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

When I try to access my application with the browser I get a 500 error and the logs error

[...] from project import app as application
[...] ImportError: No module named project

I think there is a problem where I activate the virtual environment.

Any idea to fix the situation?

heapOverflow
  • 1,195
  • 2
  • 15
  • 28
  • what is your project structure?? please add it to your question – Espoir Murhabazi Jan 04 '18 at 14:31
  • The documentation clearly tells you how to check what version of Python mod_wsgi is using. See http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html#python-installation-in-use – Graham Dumpleton Jan 04 '18 at 20:15
  • Your problem is like though because you haven't told mod_wsgi where you project code is. First step is that you should change to using mod_wsgi daemon mode. Then use the ``python-path`` option to ``WSGIDaemonProcess`` to tell mod_wsgi where your project code is. – Graham Dumpleton Jan 04 '18 at 20:16

1 Answers1

0

I was missing these 2 lines from my Apache configuration file:

WSGIPythonHome /var/www/myapp/env/bin/python3.5
WSGIPythonPath /var/www/myapp/env/lib/python3.5/site-packages

They need to be out of the VirtualHost section.

https://stackoverflow.com/a/27451634/2254402

heapOverflow
  • 1,195
  • 2
  • 15
  • 28
  • 1
    The ``WSGIPythonHome`` line is wrong. It is supposed to point to a directory which is the root of the Python virtual environment if using it, not the ``python`` executable. So would be ``WSGIPythonHome /var/www/myapp/env``. You can then also delete the ``WSGIPythonPath`` which preferably shouldn't be used to refer to a ``site-packages`` directory. You really should switch to daemon mode as well. Using embedded mode is a bad idea. For more on virtual environment setup see http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html – Graham Dumpleton Jan 05 '18 at 21:01