2
votes

In CentOS 6.4, I created python virtual environment in /var/www/html/venv folder. Then after activating the virtual environment, I installed all necessary python libraries for my flask application. I checked, that Flask libraries were in /var/www/html/venv/lib/python2.7/site-packages folder. I've already installed and loaded mod_wsgi. Now in my flask app, which is in /var/www/html/truckman/wsgi folder, I created truckman.wsgi file with the following content:

activate_this = '/var/www/html/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

import sys
sys.path.insert(0, '/var/www/html/truckman/wsgi/')

from app import app as application
import config
application.config.from_object(config.Dev)

Also, in /etc/httpd/conf/httpd.conf I added:

<VirtualHost *>
    WSGIScriptAlias / /var/www/html/truckman/wsgi/truckman.wsgi
    <Directory /var/www/html/truckman/wsgi>
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

Now, in /var/www/html/truckman/wsgi folder, I created run.py file with the following content:

from app import app as application
import config
application.config.from_object(config.Dev)
if __name__ == "__main__":
    application.run(port=5001)

Now I tested my app with flask's development server; if I execute "python run.py", my app works as expected. I can browse to localhost:5001, and the app's initial page shows up.

Then I tested my app with mod_wsgi: First killed run.py process, and restarted httpd service, and after that browsed to localhost; but it returned: "500 Internal Server Error". In /etc/httpd/logs/error_log file I found the following error message: "ImportError: No module named flask". What's wrong with my settings?

2
Is mod_wsgi compiled for Python 2.7? Are all the directories and files where your virtualenv located accessible/readable to the user that Apache runs as?Graham Dumpleton

2 Answers

0
votes

Try adding the path to your python 2.7 folder in your virtual environment.

sys.path.insert(1, '/path/to/virtualenv/lib/python2.7')
0
votes

The .conf should be something like this:

<VirtualHost *>
    ServerName example.com
    WSGIScriptAlias / /var/www/firstapp/hello.wsgi
    WSGIDaemonProcess hello python-path=/var/www/firstapp:/var/www/firstapp/env/lib/python2.7/site-packages
    <Directory /var/www/firstapp>
       WSGIProcessGroup hello
       WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

Preferably in /etc/apache2/sites-available/hello.conf, so this part can be the root of the problem.

You can check a working example code in: Hello world from flask.