2

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
  • WSGIPythonPath?? code.google.com/p/modwsgi/wiki/VirtualEnvironments Commented Mar 11, 2014 at 11:24
  • 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? Commented Mar 11, 2014 at 21:20

2 Answers 2

0

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

sys.path.insert(1, '/path/to/virtualenv/lib/python2.7')
Sign up to request clarification or add additional context in comments.

1 Comment

You are answering a very old question and your proposed solution isn't correct either. It would have to be the site-packages directory, but that even is not recommended. Most up to date information on using virtual environments with mod_wsgi can be found at modwsgi.readthedocs.io/en/develop/user-guides/…
0

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.

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.