Django mod_python deployment error - django

I'm trying to deploy a django project via mod_python and I keep getting an error saying a handler module is missing.
My apache config:
<Location />
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE bookmarklet_server.settings
PythonOption django.root /
PythonDebug On
#PythonPath "['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/local/lib/python2.6/dist-packages']"
</Location>
The other path is from my attempt to just copy over the default pythonpath, but it didn't help.
A quick test in the python console shows the module should be accesible:
Python 2.6.4 (r264:75706, Nov 2 2009, 14:44:17)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django.core.handlers.modpython
no errors
However loading the site, this error is inadvertendly returned:
MOD_PYTHON ERROR
ProcessId: 8926
Interpreter: '<ip>'
ServerName: '<ip>'
DocumentRoot: '/htdocs'
URI: '/'
Location: '/'
Directory: None
Filename: '/htdocs'
PathInfo: '/'
Phase: 'PythonHandler'
Handler: 'django.core.handlers.modpython'
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)
File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1202, in _process_target
module = import_module(module_name, path=path)
File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 304, in import_module
return __import__(module_name, {}, {}, ['*'])
ImportError: No module named django.core.handlers.modpython

Perhaps django isn't in your Pythonpath?

Ok, managed to fix the problem.
This particular import issue was fixed by installing django rather than just having the svn trunk linked into dist-packages. Why this doesn't work for server processes I don't know.
But anyway, there were further issues which were solved by doing a CGI deployment via nginx and eventually the last problem (which also failed my wsgi deployment attempt) was that my /home dir, which is where the files were, was accessible only by myself.
I learned a lot today :)

Related

Mod_wsgi fails to load django.core.handlers.wsgi

Ok, after 5-6 hours of trying, I give up. I have searched the web, tried all solutions suggested, but nothing is solving my problem.
Goal: Set up Django on my Ubuntu 12.04 VPS.
Problem: Exception occurred processing WSGI script [...] ImportError: No module named django.core.handlers.wsgi in /etc/log/apache2/error.log.
Solutions tried: 1) Appending the site-packages directory to the sys path in Djangos' wsgi.py file, 2) re-installing mod_wsgi, 3) making sure mod_wsgi is compiled for the same Python version as Django is installed with, 4) chmod 777 for the site-packages directory.
Environment: Ubuntu 12.04 VPS, Django installation in virtualenv, Python version 2.7.3, Django version 1.6.1, mod_wsgi built from mod_wsgi-3.4.tar.gz.
Full error message:
mod_wsgi (pid=23691): Exception occurred processing WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py'.
Traceback (most recent call last):
File "/var/www/mySite/djangoSite/djangoSite/wsgi.py", line 7, in <module>
import django.core.handlers.wsgi
ImportError: No module named django.core.handlers.wsgi
mod_wsgi (pid=23691): Target WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=23691): Exception occurred processing WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py'.
Traceback (most recent call last):
File "/var/www/mySite/djangoSite/djangoSite/wsgi.py", line 7, in <module>
import django.core.handlers.wsgi
ImportError: No module named django.core.handlers.wsgi
Conf file from sites-available:
<VirtualHost *:80>
ServerAdmin admin#mysite.com
ServerName mysite.com
DocumentRoot /var/www/mysite.com/djangoSite
WSGIDaemonProcess djangoSite python-path=/var/www/mysite.com/djangoSite:~/Envs/myEnv/lib/python2.7/site-packages
WSGIProcessGroup djangoSite
WSGIScriptAlias / /var/www/mysite.com/djangoSite/djangoSite/wsgi.py
Alias /static/ /var/www/mysite.com/djangoSite/static/
<Directory /var/www/mysite.com/djangoSite/static>
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/mysite.com/djangoSite/djangoSite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
wsgi.py:
import os
import sys
sys.path.append('/var/www/mysite.com/djangoSite/')
sys.path.append('/var/www/mysite.com/djangoSite/djangoSite/')
activate_this = '/root/Envs/myenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoSite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Site structure
/var/www/
-- mysite.com
-- djangoSite
-- manage.py
-- djangoSite
-- settings.py, wsgi.py etc.
In your wsgi.py, try adding this to activate virtual env:
import os
import sys
sys.path.append('/Path_To/Virtual_Env/Project_Dir/')
#This is important if multiple apps are running (instead of setdefault)
os.environ["DJANGO_SETTINGS_MODULE"] = "app_name.settings"
activate_this = '/Path_To/Virtual_Env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
In you apache config, you mainly need only(No Deamon Process required):
WSGIScriptAlias / /var/www/mysite.com/djangoSite/djangoSite/wsgi.py
Alias /static/ /var/www/mysite.com/djangoSite/static/
See from the Create Virtual Host section of this link
Make sure you are using absolute paths in your Apache2 config file. I assume you have been able to run python manage.py shell to get a working Django shell? I would tackle the Apache2 config file; I'm sure your issue is there.
~/Envs/myEnv/lib/python2.7/site-packages
I think that's your problem right there: the ~ is, if it is expanded at all, expanded to the home of the www-data user. You can validate that by doing something like this:
import sys
print >> sys.stderr, sys.path
in your wsgi file; then check your error log. If it doesn't end up in the main error log, add an ErrorLog to your vhost to get per-vhost error logging.
Also, /root/ is not readable for the www-data user, you need to move your virtual env somewhere accessible.

Error: Target WSGI script not found or unable to stat when run django on apache

i have a problem when run django on apache:
htdocs/blog/apps/homepage/urls.py:
url(r'^$', 'index', name="h_index"),
url(r'^about/$', 'about', name="h_about"),
url(r'^contact/$', 'contact', name="h_contact"),
url(r'^archive/$', 'archive', name="h_archive"),
htdocs/blog/urls.py
(r'^', include('apps.homepage.urls')),
django.wsgi:
import os
import os.path
import sys
sys.path.append('D:/Coding/xampp/htdocs')
sys.path.append('D:/Coding/xampp/htdocs/blog')
os.environ['DJANGO_SETTINGS_MODULE'] = 'blog.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
httpd.conf:
Alias /static/ "D:/Coding/xampp/htdocs/blog/static/"
WSGIScriptAlias /blog/ "D:/Coding/xampp/htdocs/blog/django.wsgi"
when i run "localhost/blog", it's working. But run "localhost/blog/about/" or other, it's error:
[error] [client ::1] Target WSGI script not found or unable to stat: .../htdocs/blog/django.wsgiabout, referer: http://localhost/blog/
Please notice how your apache configuration doesn't match the documented syntax for mod_wsgi's WSGIScriptAlias.
WSGIScriptAlias /blog/ "D:/Coding/xampp/htdocs/blog/django.wsgi"
should be:
WSGIScriptAlias /blog "D:/Coding/xampp/htdocs/blog/django.wsgi"
(notice no trailing slash after the 2nd token, "/blog")
I just resolved the same issue just now and found this thread Googling. Hope this helps you and future users.
For more information:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines
I would suspect you are not loading the wsgi module. If you look closely at your error message Apache is treating it like a file and adding about to the name of the wsgi script file it looks for - thus it doesn't find it.

Apache Django "client denied by server configuration" error

I found a similar question here, but didn't help me.
I basically have apache setup on my OS X. I also have Django installed.
However, when i try to load the page through the browser locally, i get:
Forbidden
You don't have permission to access / on this server.
I have an original httpd.conf with the only modification of enabling vhosts in which I have:
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "/Dropbox/project/www/"
ServerName touch.loc
# ServerAlias www.dummy-host.example.com
ErrorLog "/Dropbox/project/www/log/error.log"
CustomLog "/Dropbox/project/www/log/access.log" common
</VirtualHost>
And the error.log file gives:
[Tue May 03 20:22:56 2011] [error] [client 127.0.0.1] Directory index forbidden by Options directive: /Dropbox/project/www/
I read around and it looks like i have to add the following to the httpd.conf:
<Directory /Dropbox/project/www >
Order deny,allow
Allow from all
</Directory>
In which case i get:
[Tue May 03 20:27:55 2011] [error] [client 127.0.0.1] client denied by server configuration: /Dropbox/project/www/
Can someone help me fix this annoyance? How can I further Investigate it?
Does it have to do with users/groups?
UPDATE:
I then added the Options +Indexes and the permissions opened. However when i try to load the page, file structure appears instead of the wsgi file to pickup and load the website. What would be the reason for that?
here is my .wsgi file:
import os
import sys
sys.stdout = sys.stderr
# Add the virtual Python environment site-packages directory to the path
import site
site.addsitedir('/usr/lib/python2.6/dist-packages')
#If your project is not on your PYTHONPATH by default you can add the following
sys.path.append('/Dropbox/project/www/')
sys.path.append('/Dropbox/project/www/project')
# Avoid ``[Errno 13] Permission denied: '/var/www/.python-eggs'`` messages
os.environ['PYTHON_EGG_CACHE'] = '/Dropbox/project/www/mod_wsgi/egg-cache'
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
You seem to be missing anything in your Apache configuration that actually serves the Django application. You need to read the Django mod_wsgi documentation - the first line of code there is what you're missing.
Note that your code should not live under the DocumentRoot.
The problem I see is in Dropbox folder
I have made so:
chmod o+x /home/your_name/Dropbox
That resolves permissions

django_cms ImproperlyConfigured: Error importing middleware cms.middleware.media

I'm moving an application that uses django_cms from one server, where everything worked, to another and have spent the last 4 hours trying to find the cause of this error. A suggestions very welcome!
mod_wsgi (pid=21972): Exception occurred within WSGI script '/var/www/vhosts/compdoctest.com/django/compdoc/django.wsgi'.
Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/django/core/handlers/wsgi.py", line 230, in __call__
self.load_middleware()
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 42, in load_middleware
raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
ImproperlyConfigured: Error importing middleware cms.middleware.media: "No module named media"
The offending line is the last one in the middleware list in settings.py
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.multilingual.MultilingualURLMiddleware',
'cms.middleware.media.PlaceholderMediaMiddleware',
)
If I remove the final line then the code continues and falls over later saying that item is required in middleware.
I'm using slightly different version of django, 1.2.1 final on the old working server and 1.2.3 final on the new server.
All the things I've tried:
The same version of django_cms - 2.1.0 beta 3 - that was used on the old server
The latest version on github - 2.1.0.beta2-550 Putting the cms, mptt, menus, publisher folders in the
app From python importing the
googled (nobody having the same problem that I can find)
middleware file directly (no problem)
result of opening in python:
python manage.py shell
Python 2.5.2 (r252:60911, Jan 20 2010, 23:14:04)
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import cms.middleware.media
>>> cms.middleware.media.__file__
'/var/www/vhosts/compdoctest.com/django/compdoc/cms/middleware/media.pyc'
>>> from django.forms.widgets import Media
>>> import cms.middleware.media
>>>
Thanks to my friend Bartosz Ptaszynski for pointing me in the right direction on this one. It was a path problem. Added this to the top of the settings.py file and it all magically started working.
sys.path.insert(0, '/path_to_app/app/')
And as he pointed out:
Exception occurred within WSGI script means that the path while running under the web server was wrong it's a completely different environment than the manage.py shell
I had the same problem. But since it only occurs when using mod_wsgi, another solution is adding the path in the apache config (rather than editing the syspath inside setting.py):
# mod_wsgi settings
WSGIDaemonProcess name user=user group=group python-path=/app_path/app/:/app_path/lib/python2.6/site-packages/:/app_path/
WSGIProcessGroup polykum
Including the site packages in the path is also given in the example of Jon Black.

Django MOD_PYTHON ERROR

I have had django running with mod_python for awhile now but today I get the following error
MOD_PYTHON ERROR
ProcessId: 4551
Interpreter: 'thatch'
ServerName: 'thatch'
DocumentRoot: '/var/www/thatch'
URI: '/'
Location: '/'
Directory: None
Filename: '/var/www/thatch/'
PathInfo: ''
Phase: 'PythonHandler'
Handler: 'django.core.handlers.modpython'
Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)
File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 1202, in _process_target
module = import_module(module_name, path=path)
File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 304, in import_module
return __import__(module_name, {}, {}, ['*'])
ImportError: No module named django.core.handlers.modpython
This is my virtual host configuration
<VirtualHost *:80>
ServerName thatch
DocumentRoot /var/www/thatch
<Location "/">
SetHandler python-program
PythonPath "['/var/www/thatch'] + sys.path"
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE thatch.settings
PythonOption django.root /var/www/thatch
PythonDebug On
</Location>
<Location "/media">
SetHandler None
</Location>
<LocationMatch "\.(jpg|gif|png|css|js)$">
SetHandler None
</LocationMatch>
</VirtualHost>
Any ideas why this is happening?
I can only guess what your problem might be, but here are two standard checks:
Import modpython in shell, from a couple different working directories
Change users (if you have any besides root set up) and repeat step 1
If #2 looks like more trouble than it's worth, then try very, very carefully examining permissions for each folder along the path to modpython to make sure that "other" has read access at every stage along the way. If you've moved django into your home directory (like I did once...not sure why, but got the same error) that'll be the problem. You either need to move it back into a directory where apache can read or you need to add read permission for other.
In my case pytonpath and access rights was ok.
This error disappeared after recompilation python.
Configuration:
Gentoo
Django 1.1
Apache:
www-servers/apache-2.2.15 USE="doc ssl suexec threads -debug -ldap (-selinux) -static" APACHE2_MODULES="alias auth_basic authn_alias authn_anon authn_default authn_file authz_default authz_host authz_owner autoindex cache dav dav_fs dav_lock deflate dir disk_cache env expires ext_filter file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias -actions -asis -auth_digest -authn_dbd -authn_dbm -authz_dbm -authz_groupfile -authz_user -cern_meta -charset_lite -dbd -dumpio -ident -imagemap -log_forensic -proxy -proxy_ajp -proxy_balancer -proxy_connect -proxy_ftp -proxy_http -substitute -version" APACHE2_MPMS="worker -event -itk -peruser -prefork"
Python 2.6.4