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 thesys.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!
Some of your directory permissions are now suspect. You have:
Better to be using:
Same for static and templates.
What we really need to see is the exact Python exception details with traceback from the Apache error logs.