2

Hello dear SO users and welcome to my first ever SO question,

I am currently looking to deploy a Flask application using Apache and mod_wsgi. Everything was coded with Python 3.4 so I had to follow several tutorials / questions to get mod_wsgi working with my version of Python (because apt-get install gets you the 3.4 version which is Python 2.7 compatible, you have to compile it after getting it with pip in a Python 3 virtualenv for it to be the 4.x version).

The Apache server error.log shows Apache/2.4.10 (Debian) mod_wsgi/4.4.13 Python/3.4.2 configured so this seems to be working fine.

My problem lies in the importation of the application in the .wsgi file. Tutorials such as this one usually do from FlaskApp import app as application considering their file tree, and this works as intended with a minimal application, I tried it without any problem.

My application is a little more complicated and uses a manage.py file to run on localhost, which may be part of the problem once trying to deploy on Apache.

From what I have found on countless SO Questions and other sites' questions, this most likely comes from:

  • A permission problem (if the user executing the current script doesn't have reading right on the script he's looking to import)
  • A sys.path problem (if the top-level folder is not in the sys.path, Python 3 cannot see it)

Mainly out of frustration, I ended up using chmod 744 LongAppName in /var/www/ resulting in drwxrwxr-x rights. My file tree is the following:

drwxrwxr-x LongAppName
    drwxr-xr-x LongAppName
        drwxr--r-- ShorterAppName
            -rw-r--r-- app.py
            -rw-r--r-- commands.py
            -rw-r--r-- __init__.py
            drwxr--r-- static
            drwxr--r-- templates
            <irrelevant files>
        -rw-r--r-- __init__.py
        -rw-r--r-- manage.py
        drwxr-xr-x venv
        <more irrelevent files>
    -rwxr-xr-x longappname.wsgi

So for the first possible fix, permissions seem fine to me. Regarding the second one, I'm printing sys.path in the .wsgi file and it outpouts ['/var/www/LongAppName', <among others>] which shows that the top-level folder is in the sys.path.

My longappname.wsgi is as follows:

#!/var/www/LongAppName/LongAppName/venv/bin/python3
import os, sys, logging
#Doing some prints to check if we're in the venv and such.
PROJECT_DIR = '/var/www/LongAppName/'
if PROJECT_DIR not in sys.path:
    sys.path.insert(0, PROJECT_DIR)

def execfile(filename):
    globals = dict( __file__ = filename )
    exec( open(filename).read(), globals )

activate_this = os.path.join( PROJECT_DIR+'LongAppName/', 'venv/bin', 'activate_this.py' )
execfile( ativate_this )
#Printing sys.path and sys.executable here
logging.basicConfig(stream=sys.stderr)

from LongAppName.ShorterAppName.app import manager as application
#The previous line changed a lot, I tried importing app, changing the path but this one was the first one I wrote.

Python can't seem to find LongAppName.ShorterAppName.app and I don't know why.

From the number of attempts I have made to test different alternatives to the code I wrote in the first place, I'm starting to lose track of what was good and what might cause the problem so I require your help to figure this out.

Thanks in advance!

Community
  • 1
  • 1
Batman
  • 282
  • 3
  • 12

1 Answers1

0

Some of your directory permissions are now suspect. You have:

    drwxr--r-- ShorterAppName

Better to be using:

    drwxr-xr-x ShorterAppName

Same for static and templates.

What we really need to see is the exact Python exception details with traceback from the Apache error logs.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • Thank you for your response, the directories indeed needed to be executable, the error message changed after I used `chmod a+x` on all subdirectories of LongAppName. The error message I am now getting is: `[Wed Aug 19 08:23:38.650234 2015] [wsgi:error] [pid 1953:tid 139798123620096] [client 192.168.0.17:51452] TypeError: __call__() takes from 1 to 2 positional arguments but 3 were given` I am not sure which __call__() it is, but it most likely comes from the way the app is launched using the **manage.py** file. Would you like seeing the contents of the file or should I ask another question? – Batman Aug 19 '15 at 06:28
  • It would be necessary to see the full traceback from the exception so it can be seen where this is occurring. – Graham Dumpleton Aug 19 '15 at 07:31
  • Here is the traceback: `[mpm_event:notice] [pid 2998:tid 140561778562944] AH00489: Apache/2.4.10 (Debian) mod_wsgi/4.4.13 Python/3.4.2 configured -- resuming normal operations [core:notice] [pid 2998:tid 140561778562944] AH00094: Command line: '/usr/sbin/apache2' [wsgi:error] [pid 3001:tid 140561554056960] [client 192.168.0.17:52081] mod_wsgi (pid=3001): Exception occurred processing WSGI script '/var/www/LongAppName/longappname.wsgi'. [wsgi:error] [pid 3001:tid 140561554056960] [client 192.168.0.17:52081] TypeError: __call__() takes from 1 to 2 positional arguments but 3 were given` – Batman Aug 19 '15 at 07:45
  • 1
    Whatever 'manager' is, it is not usable as a WSGI application. You would normally use the ``Flask()`` application object. – Graham Dumpleton Aug 19 '15 at 07:56
  • I was indeed thinking this might be the problem. `manager` is created in my **app.py** like this: `from flask import Flask app = Flask(__name__) app.debug = True from flask.ext.script import Manager manager = Manager(app) ` – Batman Aug 19 '15 at 07:59
  • Ah, sorry for the inline code, I had never noticed that newline characters are not displayed in comments – Batman Aug 19 '15 at 08:01
  • What is Manager doing then? – Graham Dumpleton Aug 19 '15 at 09:12
  • [Manager](http://flask-script.readthedocs.org/en/latest/) probably shouldn't be called by the **.wsgi** file. I tried to import `app` instead of `manager` and the application runs on the server. So thank you for that, it solved my original problem. But importing `app` breaks other things as I get cross-origin requests blocked for some ajax requests and apache **error.log** says my database is read-only when I try to add an object. (I think importing `app` is the problem because it runs fine with `manager` on localhost) I will further read flask-script doc to try and find out why. Thanks. – Batman Aug 19 '15 at 10:19
  • If your database is not writable, sounds like you are using SQLite. Note that your code will run as special Apache user and so will not have ability to write to database file or directory, both of which are required. Ensure you are using daemon mode of mod_wsgi and set the user/group for daemon mode so that code runs as user with write access. – Graham Dumpleton Aug 19 '15 at 10:26