2

I'm trying to run a really simple Python web application developed with the Flask microframework. I'm on a VPS with Ubuntu 10.04 LTS. I've followed this instructions in order to get the comforting "Hello world!", and it was a successful experience. But now, I'm setting up a slightly more complicated configuration and I cannot get this to work properly.

The Apache Virtual Host configuration for my application is:

<VirtualHost *:80>
    ServerAdmin    user@example.org
    ServerName    domain.com
    ServerAlias    www.domain.com

    DocumentRoot    /srv/www/domain.com/public_html/

    LogLevel    info    # I've put this for debugging purposes.
    ErrorLog    /srv/www/domain.com/logs/error.log
    CustomLog    /srv/www/domain.com/logs/access.log combined

    WSGIDaemonProcess   username   user=username    group=username   threads=5   processes=5
    WSGIScriptAlias /myapplication/     /srv/www/domain.com/myapplication/myapplication.wsgi

    <Directory /srv/www/domain.com/myapplication/myapplication>
        WSGIProcessGroup    username
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>

    Alias /css    /srv/www/domain.com/public_html/css
    Alias /js    /srv/www/domain.com/public_html/js
    Alias /images    /srv/www/domain.com/public_html/images
</VirtualHost>

And my WSGI configuration file is:

# -*- coding: utf-8 -*-
from myapplication import app as application

Is this something wrong with the above configurations?

Finally, Apache gives me a 500 error page. So I check the logs:

[info] mod_wsgi (pid=3132, process='', application='domain.com|/myapplication/'): Loading WSGI script '/srv/www/domain.com/myapplication/myapplication.wsgi'.
[error] mod_wsgi (pid=3132): Target WSGI script '/srv/www/domain.com/myapplication/myapplication.wsgi' cannot be loaded as Python module.
[error] mod_wsgi (pid=3132): Exception occurred processing WSGI script '/srv/www/domain.com/myapplication/myapplication.wsgi'.
[error] Traceback (most recent call last):
[error] File "/srv/www/domain.com/myapplication/myapplication.wsgi", line 3, in <module>
[error] from myapplication import app as application
[error] ImportError: No module named myapplication

The tree structure resembles this:

/myapplication
    myapplication.wsgi
    myapplication (python package)

Apparently, Python couldn't find the myapplication module in sys.path. But I've checked with prints and it's right there. I know this issue might be trivial to resolve, but I'm a little confused. How could I get in the right direction to solve this?

  • Just out of question, does it work if you explicitly set `sys.path` in `myapplication.wsgi`? – larsks Apr 23 '12 at 20:14
  • @larsks Oh. It worked! Thanks so much! Now I'm trying to figure out how could this be. Now, of course, I'm running into other issues, like routing. Requests for '/' work fine, but anything "below" (e.g. /login) doesn't. I've [looked around](http://stackoverflow.com/questions/9680073/how-do-i-use-flask-routes-with-apache-and-mod-wsgi) for solutions and I'm already calling my application object properly! I should update my question. – Horacio Bertorello Apr 23 '12 at 20:22
  • You should probably open a new question. Also, note that I'm neither a flask nor a WSGI expert; there may very well be a better place to set Python's search path. – larsks Apr 23 '12 at 20:24
  • @larsks I'll open a new question. Thanks for your time! :-) – Horacio Bertorello Apr 23 '12 at 20:36
  • Well, I managed to resolve the "routing issue". It wasn't an issue. All my URLs were _hardcoded_ in my template files. That's embarrasing. I'll just move forward. – Horacio Bertorello Apr 26 '12 at 14:13

1 Answers1

3

@larsks pointed me to a temporal but working solution. Now, myapplication.wsgi looks like:

import os
import sys

sys.path.append('/srv/www/domain.com/myapplication/')
os.environ['PYTHON_EGG_CACHE'] = '/srv/www/domain.com/myapplication/.python-egg'

from myapplication import app as application

Thanks!