2
votes

I am having a problem deploying a flask application on apache2 using mod_wsgi. Error log and config files follow. I always get internal server error. This is very similar to How to solve import errors while trying to deploy Flask using WSGI on Apache2 but for some reason the solution proposed there did not work here.

apache error log

[Thu Aug 27 12:06:30.366817 2015] [:error] [pid 9330:tid 140623686452992] [remote 2.239.9.178:64904] mod_wsgi (pid=9330): Target WSGI script '/var/www/bitcones/bitcones.wsgi' cannot be loaded as Python module.
[Thu Aug 27 12:06:30.366867 2015] [:error] [pid 9330:tid 140623686452992] [remote 2.239.9.178:64904] mod_wsgi (pid=9330): Exception occurred processing WSGI script '/var/www/bitcones/bitcones.wsgi'.
[Thu Aug 27 12:06:30.366894 2015] [:error] [pid 9330:tid 140623686452992] [remote 2.239.9.178:64904] Traceback (most recent call last):
[Thu Aug 27 12:06:30.366913 2015] [:error] [pid 9330:tid 140623686452992] [remote 2.239.9.178:64904]   File "/var/www/bitcones/bitcones.wsgi", line 4, in <module>
[Thu Aug 27 12:06:30.366969 2015] [:error] [pid 9330:tid 140623686452992] [remote 2.239.9.178:64904]     from bitcones import bitcones as application
[Thu Aug 27 12:06:30.366981 2015] [:error] [pid 9330:tid 140623686452992] [remote 2.239.9.178:64904]   File "/var/www/bitcones/bitcones/bitcones.py", line 6, in <module>
[Thu Aug 27 12:06:30.367045 2015] [:error] [pid 9330:tid 140623686452992] [remote 2.239.9.178:64904]     from analysis import cone as _cone, flow
[Thu Aug 27 12:06:30.367056 2015] [:error] [pid 9330:tid 140623686452992] [remote 2.239.9.178:64904]   File "/var/www/bitcones/bitcones/analysis/cone.py", line 5, in <module>
[Thu Aug 27 12:06:30.367121 2015] [:error] [pid 9330:tid 140623686452992] [remote 2.239.9.178:64904]     from analysis.statistics import purity_statistics
[Thu Aug 27 12:06:30.367139 2015] [:error] [pid 9330:tid 140623686452992] [remote 2.239.9.178:64904] ImportError: No module named analysis.statistics

bitcones.wsgi

#!/usr/bin/python
import sys
sys.path.insert(0,"/var/www/bitcones")
from bitcones import bitcones as application

apache virtual host file

<VirtualHost *:80>
    ServerName <my-server-name>
    ServerAdmin <my-email>
    WSGIDaemonProcess bitcones user=<my-username> group=<my-username> threads=5
    WSGIScriptAlias / /var/www/bitcones/bitcones.wsgi

<Directory /var/www/bitcones>
    WSGIProcessGroup bitcones
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

part of my app tree (everything is under /var/www/bitcones/)

├── bitcones
│   ├── analysis
│   │   ├── <some_files>
│   │   └── statistics
│   │   │   ├── <some_files>
│   ├── bitcones.py
│   ├── static
│   │   ├── <some static content>
│   └── templates
│       └── <my_templates>.html
└── bitcones.wsgi

This should be sufficient to figure out why I'm having this import error. If any other file/configuration is needed please ask. I'm loosing my mind.

Thanks!

EDIT: I just want to add that I was following this guide: http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/

1
Is the cgi-script properly configured?facundofarias
I'm not using cgi, I'm using mod_wsgi which is an implementation of wsgi, as far as I understood. Now, is my mod_wsgi script properly configured? I don't know...that's my question ;)valenz
What Python version are you expecting to use and what is mod_wsgi using? You don't show any __init__.py files in your directories, which for most Python versions are required else your packages will not work. Beyond that, could be file system permissions, or SELinux restrictions.Graham Dumpleton
Also, why isn't from analysis.statistics import purity_statistics instead from ..analysis.statistics import purity_statistics? I would check how you are structuring your imports. It looks a bit like you have them written with an expectation that '/var/www/bitcones/bitcones' is also in sys.path. So look at how relative imports work such that a module within a package can import something from above that point in the package.Graham Dumpleton
python --version gives Python 2.7.6. How do I check which version of python my mod_wsgi is using? __init__.py files are in all the packages, I just did not report them in the question. Sorry! As for permission/restriction issues: what can I check? I have full access to the machine. As for your second comment: everything is working fine locally with the development web server. Thanks!valenz

1 Answers

1
votes

You set the Python version that mod_wsgi uses when you install libapache2-mod-wsgi (python 2) or libapache2-mod-wsgi-py3 (python 3). I would guess you're on Python 2 from what you describe, since using python 3 is a more deliberate choice than 2. I don't think that's your problem, though. I think it's an import problem, like Graham said.

I recommend using from bitcones.analysis.statistics import purity_statistics for your import statement.