1

Created a new django project with /tmp$ django startproject testsite

configured apache2 to run the application in /tmp/testsite/testsite/wsgi.py

Inside the wsgi.py there's a simple line import django (this is everything at the moment, because i'm trying to debug).

I did configure the httpd.conf to run the wsgi file, which (i think) works, because the logs loog like this:

Wed Feb 26 16:55:07 2014] [error] [client 127.0.0.1] mod_wsgi (pid=12621): Target WSGI script '/tmp/testsite/testsite/wsgi.py' cannot be loaded as Python module.
[Wed Feb 26 16:55:07 2014] [error] [client 127.0.0.1] mod_wsgi (pid=12621): Exception occurred processing WSGI script '/tmp/testsite/testsite/wsgi.py'.
[Wed Feb 26 16:55:07 2014] [error] [client 127.0.0.1] Traceback (most recent call last):
[Wed Feb 26 16:55:07 2014] [error] [client 127.0.0.1]   File "/tmp/testsite/testsite/wsgi.py", line 26, in <module>
[Wed Feb 26 16:55:07 2014] [error] [client 127.0.0.1]     import django
[Wed Feb 26 16:55:07 2014] [error] [client 127.0.0.1] ImportError: No module named django

I then checked if i have the required libraries on the sys.path, with a print - this showed that at the point of running the import statement, the sys.path was ok (In more detail, i had /...../sistem-packages in sys.path

Also looked at this link and tried a chmod a+x wsgi.py, but the same error still appeared.

For reference, here are my files:

httpd.conf

WSGIScriptAlias /vwh /tmp/testsite/testsite/wsgi.py
WSGIPythonPath /tmp/testsite:/home/local/smth/user/ve/tmp1/lib/python2.7/site-packages

<Directory /tmp/testsite/testsite> 
<Files wsgi.py>
Order deny,allow
#Require all granted
Allow from all
</Files>
</Directory>

wsgi.py

import os
import sys
from subprocess import call

#print call(["which", "python"])     #also this gives a VERY weird result... prints `0`

import django

Also, tried printing the output of subprocess.call(['which','python']) but that has the weirdest result - returns 0 and i dont' know how to interpret it

[EDIT] I am the virtualenv what was installed at path /home/local/smth/user/ve/tmp1

[EDIT] Yes - i can import django from an interpreter, when the specified path is in the sys.path, and django.__file__ returns /home/local/smth/user/ve/tmp1/local/lib/python2.7/site-packages/django/__init__.pyc

Community
  • 1
  • 1
vlad-ardelean
  • 7,480
  • 15
  • 80
  • 124
  • 1
    On the `subprocess.call` thing--that's what it returns. The return code of your process (typically 0 if the process returned sucessfully). If you want it to output the stdout from that process you have to take additional steps. Anyways, obvious question but is the full `django` package actually installed in `/home/local/smth/user/ve/tmp1/lib/python2.7/site-packages`? Furthermore your mod_wsgi version might be relevant. – Iguananaut Feb 26 '14 at 15:25
  • Does `import django` work in a Python shell in your virtual environment? If it does, what's in `django.__file__`? – lanzz Feb 26 '14 at 15:26
  • @Iguananaut edited the answer. importing django works in the console. – vlad-ardelean Feb 26 '14 at 15:59
  • @Iguananaut trying to find the version of mod_wsgi (installed today if that's relevant)... will edit the question when i find out the version – vlad-ardelean Feb 26 '14 at 16:05
  • 1
    You wrote "`django.__file__` returns `/home/local/smth/user/ve/tmp1/local/lib/python2.7/site-packages/django/__init__.pyc`". But your `WSGIPythonPath` contains `/home/local/smth/user/ve/tmp1/lib/python2.7/site-packages`. Note the missing `local/` in the path. – Iguananaut Feb 26 '14 at 16:59
  • @Iguananaut thx for the remark, changed that so now the 2 paths are the same.... problem is it still doesn't work :| – vlad-ardelean Feb 27 '14 at 08:30
  • What about other modules in your path? Can they be imported? Or is it just django? – Iguananaut Feb 27 '14 at 13:15
  • @Iguananaut "i" can import them but "apache" can't. Meaning, i can do everything, with my user, but apache with its `www-data` user can read almost nothing. i solved the problem with giving others access. thx for helping! – vlad-ardelean Feb 27 '14 at 13:31

1 Answers1

0

The thing that did it was setting the proper access bits. Basically even though the proper site-packages was on in sys.path, the www-data user, for apache, didn't have privileges for reading or executing those files.

Moreover, since those files were in the home location for my user, i gave other users the ability to read and execute my home folder.

Now this is most likely a bad idea, and what you'd normally want to do is create a group of users, add the apache user to that group, and allow the users in that group to read and execute the files that one needs.

vlad-ardelean
  • 7,480
  • 15
  • 80
  • 124