0

I am getting 500 internal server error when running my server with Flask,apache2, and Python 3.4.2.

At first I installed Flask without creating virtual environment, so I thought that that could be a reason. However, I later created virtual environment for my app using:

python3.4 -m venv venv
source venv/bin/activate
(venv) pip3 install Flask

I checked by running my app using python3 __init.py and it was working on local. Then I reloaded my apache2 server, and still getting the same error. Please see below output from error log file:

mod_wsgi (pid=25667): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=25667): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
Traceback (most recent call last):
File "/var/www/FlaskApp/flaskapp.wsgi", line 11, in <module>
from XYZ import app as application
File "/var/www/FlaskApp/XYZ/__init__.py", line 1, in <module>
from flask import Flask, render_template
ImportError: No module named flask

Any suggestions how to fix it?

FlaskApp.conf file in the etc/apache2/sites-available

<VirtualHost *:80>

ServerName xyz.com

ServerAlias www.xyz.com

ServerAdmin contact@xyz.com
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/XYZ/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/XYZ/static
<Directory /var/www/FlaskApp/XYZ/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Here's line from apache2 error log file:

AH00489: Apache/2.4.10 (Ubuntu) mod_wsgi/3.5 Python/2.7.8 configured -- resuming normal operations

It looks like mod_wsgi is trying to use 2.7.8 as opposed to 3.4. How do I fix that?

user3151858
  • 790
  • 4
  • 13
  • 26
  • I checked in my flaskapp folder - there is folder named venv that has lib folder which has flask in it. Why is it not working? – user3151858 Jan 26 '15 at 03:51
  • possible duplicate of [Running Python from a virtualenv with Apache/mod\_wsgi, on Windows](http://stackoverflow.com/questions/4614121/running-python-from-a-virtualenv-with-apache-mod-wsgi-on-windows) – spirulence Jan 26 '15 at 03:52
  • I noticed interesting thing - when I check virtual environment folder there is no Flask under site-packages even though it shows as if it got installed when I do pip3 install Flask. I will try using pip. – user3151858 Jan 26 '15 at 05:11
  • I tried using pip - this time venv has flask in it but still getting the same error - 500 and no flask module in apache2 log file. – user3151858 Jan 26 '15 at 05:58
  • Delete the virtual environment made using Python 3.x.x and make one with Pyhton 2.x.x – Wally Jan 26 '15 at 10:18
  • I wanted to add more color - when I create virtual environment I am not getting message: "New python executable in venv/bin/python". When I used Python 2.x I has such message, I am not sure if it's just Python 3.4 thing but thought it might help with figuring out what happens. – user3151858 Jan 26 '15 at 14:36
  • I tried to do everything from scratch again - does not work. – user3151858 Jan 26 '15 at 14:36
  • I am thinking of using activate_this.py script. I am not familiar with it - I read docs on it but still confused how to generate activate_this.py file? – user3151858 Jan 26 '15 at 15:41
  • My flaskapp.wsgi file looks as follows: – user3151858 Jan 26 '15 at 15:47
  • #! /usr/bin/python3.4 import sys import logging logging.basicConfig(stream=sys.stderr) sys.path.insert(0,"var/www/FlaskApp") from XYZ import app as application application.secret_key = "secret" – user3151858 Jan 26 '15 at 15:48
  • I checked bin folder in /var/www/FlaskApp/XYZ/venv/bin/activate_this.py - there is no file called activate_this.py there, there is file called activate. How do I fix that? – user3151858 Jan 26 '15 at 18:18
  • On flask irc channel I was told that web server (mod-wsgi) needs to know to use specific virtual environment as in original configuration it uses python 2.7. I tried to fix it by adding the WSGIPythonHome /path/to/my/virt/env to global apache2.conf file but it does not work either. Any ideas/suggestions? – user3151858 Jan 26 '15 at 19:14
  • As a background, the reason I am trying to use python 3.4 is because my code in it. – user3151858 Jan 26 '15 at 20:23
  • What operating system are you running and How was Apache installed? I guess apache's user isn't able to see the virtualenv. But first answer the two questions. – E. Celis Jan 26 '15 at 23:52
  • 1. I am using Ubuntu 12.10 – user3151858 Jan 27 '15 at 01:10
  • 2. sudo apt-get install apache2 – user3151858 Jan 27 '15 at 01:10
  • correction - Ubuntu 14.10 – user3151858 Jan 27 '15 at 01:26

3 Answers3

2

I think I finally solved it! I added the following line of code to FlaskApp.conf file located in etc/apache2/sites-available:

WSGIPythonPath /var/www/FlaskApp/XYZ/venv/:/var/www/FlaskApp/XYZ/venv/lib/python3.4/site-packages

This line should go ahead of VirtualHost!

Then I restarted apache and got my site working, except only with www, non-www does not work.

user3151858
  • 790
  • 4
  • 13
  • 26
2

I changed my xx.wsgi from using execfile() to using exec(). Here is what it looks like when it finally worked.

activate_this = '/opt/flask/project_name/py3venv/bin/activate_this.py'
exec(open(activate_this).read(), dict(__file__=activate_this))

import sys
sys.path.insert(0, '/opt/flask/project_name')

from project_app_name import app as application
FrozZerrer
  • 292
  • 1
  • 5
  • 16
0

mod_wsgi likely doesn't know about your virtualenv, and simply needs to be told to use that rather than the default system environment.

This info in the Django docs should help.

If it doesn't, please post your flaskapp.wsgi and httpd.conf files.

Hamms
  • 5,016
  • 21
  • 28