First time here? Check out the FAQ!
0

Installation under Apache/mod_wsgi: "ImportError: No module named site"

I try to setting apache webserver for running askbot in my local machine but i get this error in /var/log/httpd/error_log:

[Mon Mar 12 16:21:24 2012] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Mon Mar 12 16:21:24 2012] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Mar 12 16:21:24 2012] [notice] Digest: generating secret for digest authentication ...
[Mon Mar 12 16:21:24 2012] [notice] Digest: done
[Mon Mar 12 16:21:25 2012] [notice] Apache/2.2.22 (Unix) mod_ssl/2.2.22 OpenSSL/1.0.0g DAV/2 mod_wsgi/3.3 Python/2.7.2 configured -- resuming normal operations
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site

This is my configuration for apache

#NOTE: the directory paths used here may be adjusted

#the following two directories must be both readable and writable by apache
WSGISocketPrefix /var/run/wsgi
#WSGIPythonEggs /usr/lib/python2.7/site-packages/

#the following directory must be readable by apache
WSGIPythonHome /usr/lib/python2.7

#NOTE: all urs below will need to be adjusted if
#settings.FORUM_SCRIPT_ALIAS is anything other than empty string (e.g. = 'forum/')
#this allows "rooting" forum at http://example.com/forum, if you like

#replace with 127.0.0.1 with real IP address
<VirtualHost 127.0.0.1:80>
    ServerAdmin you@example.com
    DocumentRoot /home/relax/Temp/askmath/
    ServerName example.come

    #aliases to serve static media directly
    #will probably need adjustment
    Alias /static/ /home/relax/Temp/askmath/static/
    Alias /upfiles/ /home/relax/Temp/askmath/askbot/upfiles/
    <DirectoryMatch "/home/relax/Temp/askmath/askbot/skins/([^/]+)/media">
    Order deny,allow
    Allow from all
    </DirectoryMatch>
    <Directory "/home/relax/Temp/askmath/askbot/upfiles">
    Order deny,allow
    Allow from all
    </Directory>
    #must be a distinct name within your apache configuration
    WSGIDaemonProcess askbot2
    WSGIProcessGroup askbot2
    WSGIScriptAlias / /home/relax/Temp/askmath/django.wsgi
    #make all admin stuff except media go through secure connection
    <LocationMatch "/admin(?!/media)">
    RewriteEngine on
    RewriteRule /admin(.*)$ https://example.com/admin$1 [L,R=301]
    </LocationMatch>
    CustomLog /home/relax/Temp/askmath/log/access_log common
    ErrorLog /home/relax/Temp/askmath/log/error_log
    LogLevel debug
</VirtualHost>
#again, replace the IP address
<VirtualHost 127.0.0.1:443>
    ServerAdmin you@example.com
    DocumentRoot /home/relax/Temp/askmath
    ServerName example.com
    <LocationMatch "^(?!/admin)">
    RewriteEngine on
    RewriteRule django.wsgi(.*)$ http://example.com$1 [L,R=301]
    </LocationMatch>
    SSLEngine on
    #your SSL keys
    SSLCertificateFile /etc/httpd/conf/server.crt
    SSLCertificateKeyFile /etc/httpd/conf/server.key
    Alias /admin/media/ /usr/lib/python2.7/site-packages/Django-1.3.1-py2.7.egg/django/contrib/admin/media
    WSGIScriptAlias / /home/relax/Temp/askmath/django.wsgi
    CustomLog /home/relax/Temp/askmath/log/access_log common
    ErrorLog /home/relax/Temp/askmath/log/error_log
</VirtualHost>

Adding

import sys
sys.stderr.write('\n'.join(sys.path))

at the end of the django.wsgi and running "python2 django.wsgi" i get this paths

/home/relax/Temp ...
(more)
Near the soul's avatar
25
Near the soul
asked 2012-03-12 10:27:39 -0600, updated 2012-03-12 19:43:48 -0600
edit flag offensive 0 remove flag close merge delete

Comments

There isn't any problem with the apache config file (aside from assuring that every path exists and the effective user of apache server has access to them). I would check what the paths (the contents of sys.path) are inside the /home/relax/Temp/askmath/django.wsgi

Evgeny's avatar Evgeny (2012-03-12 19:06:08 -0600) edit

I add a debugging statements into the wsgi script and run it with python. I list all paths in the main question. I hope this can be helpful to find a solution. I'm going crazy ;)

Near the soul's avatar Near the soul (2012-03-12 19:47:00 -0600) edit

Looks ok. Where does this exception occur? Thats where I would look into the paths, from the information you gave it is not clear in which file the error is thrown. Usually, when the exception like the one you mentioned in the title happens there is a file name and the line number where that happened. Can you clarify?

Evgeny's avatar Evgeny (2012-03-12 19:50:39 -0600) edit

I don't know :(! i simply copy the log. Anyway change WSGIPythonHome to /usr and adding <Directory "/home/relax/Temp/askmath/"> Order allow,deny Allow from all </Directory> after WSGIScriptAlias / /home/relax/Temp/askmath/django.wsgi resolve partially the problem ;). Now i can see the site on my browser but without static content (css, image, etc). Any suggestion? Thanks

Near the soul's avatar Near the soul (2012-03-12 21:08:09 -0600) edit

Ok, much better for static files you need to run in the beginning python manage.py collectstatic from the project directory.

Evgeny's avatar Evgeny (2012-03-12 21:21:17 -0600) edit
add a comment see more comments

1 Answer

0

Possibly this will help you http://serverfault.com/questions/285229/python-django-wsgi-apache-importerror-no-module-named-site

It is likely that mod_wsgi was compiled for a different version of python intepreter.

You can verify that by finding the mod_wsgi.so file and typing ldd mod_wsgi.so - you'll see the list of libraries linked to the apache module.

It might be helpful to stick some debugging statements into the wsgi script, for example:

import sys
sys.stderr.write('\n'.join(sys.path))

If you notice that there are paths to an unrelated python version - that would indicate the diagnosys I mentioned.

Evgeny's avatar
13.2k
Evgeny
answered 2012-03-12 10:43:56 -0600, updated 2012-03-12 12:35:08 -0600
edit flag offensive 0 remove flag delete link

Comments

Seems that mod_wsgi works well because i try the test at https://wiki.archlinux.org/index.php/Mod_wsgi#Test_mod_wsgi and everything works fine. Where can i put the statements you mentioned?

Near the soul's avatar Near the soul (2012-03-12 16:23:01 -0600) edit

the debugging statements would go to file django.wsgi - still it is possible that your mod_wsgi is compiled to work with python 3, or some other than that you've installed for django.

Evgeny's avatar Evgeny (2012-03-12 16:41:51 -0600) edit

To be clear - maybe mod_wsgi works well but only with the version of python it has been compiled with.

Evgeny's avatar Evgeny (2012-03-12 17:10:40 -0600) edit

@Katy23 - are you also on archlinux together with @Near the soul? Please try the second trick, insert the sys.stderr.write... statement into the django.wsgi script and read the error logs. When you edit the wsgi script, restart the server just in case - auto-reload works only in daemon mode of mod_wsgi.

Evgeny's avatar Evgeny (2012-03-12 17:22:54 -0600) edit

Yes i'm with @Near the soul on the same machine. We try the second trick but we don't know where read the error log :( ... anyway i can confirm that the mod_wsgi is compiled for the right python version (2.7)

Katy23's avatar Katy23 (2012-03-12 17:33:55 -0600) edit
add a comment see more comments