Problem setting up django on Apache - django

This is probably really simple. But I guess I'm too new to WSGI and Django to get it on my own. I have a brand new shiny Django project on a Ubuntu virtual machine hosted in /var/www/mysite. The project was created with
django-admin startproject mysite
I'm following a WSGI tutorial to get it set up, so I created an ./apache folder, inside of which I placed this file, django.wsgi:
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = '/var/www/mysite/mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I then added this config line to the Apache configuration:
WSGIScriptAlias / /var/www/mysite/apache/django.wsgi
When I try to hit the site, nothing is returned. The connection just hangs. This is in my access.log:
192.168.2.116 - - [15/Aug/2010:14:09:02 -500] "GET / HTTP/1.1" 500 639 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
So it's hitting the site. But someone isn't doing anything. There are no errors in the errors.log.
Anyone have any ideas?

The blatant mistake in original question is that:
os.environ['DJANGO_SETTINGS_MODULE'] = '/var/www/mysite/mysite.settings'
would need to be:
sys.path.append('/var/www')
sys.path.append('/var/www/mysite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
That is, Django settings module must be a Python package path, NOT an absolute file system path. The accepted answer doesn't even point that out and rather just gives another set of code to use with no explanation.
Further, you need to set Python module search path, ie., sys.path, so that Python can find your settings file listed in that environment variable.
Finally, if your browser was hanging you have done something else wrong as well, as what you did should have resulted in an error being returned and not the browser hanging. There would also have had to be an error in the Apache error log, so you are either looking in wrong place or don't know what to look for.

Aren't you missing path to your Django application? My app.wsgi has this:
import os, sys
sys.path.append('/usr/local/src/django-1.2') # django is outside default path
sys.path.append('/usr/local/src/django-photologue-2.2') # app i'm using
sys.path.append('/var/www/mysite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
# this is regular python import, so my settings are physically
# here: /var/www/mysite/settings.py
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Related

Error while deploying Django app on cpanel(shared hosting)

I m new to django. I created a web with dajngo,and successfully deployed it in the server
The python app has been successfully setup and virtual environment has been setup.
but while running the web it gives me "Server Error (500)" I don't know whats the problem.
I think error is in "wsgi.py" file but i'm unable to idenify it.
My wsgi file:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'karan_web.settings')
application = get_wsgi_application()
my "passenger_wsgi.py" file is:
import imp
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
wsgi = imp.load_source('wsgi', 'karan_web/wsgi.py')
application = wsgi.application
can someone help me with it;
Sorry for the late answer, I had figured out the as to this just forgot to post it.
As I stated in my question, the actual problem was in passenger_wsgi.py, the django server starts with the wsgi.py and act as the gate way to the Django server.
So whenever a Django project is uploaded on the hosting server, It creates a passenger_wsgi.py file and one default wsgi.py, address of which is provided in passenger_wsgi.py by default.
So we just need to change that address and provide the address of our own wsgi.py in the project
In my case it was
import os
import sys
from karan_web import wsgi
application = wsgi.application
just edit in passenger_wsgi.py the following code.
from karan_web.wsgi import application
You need to check if your code syntax is correct and running properly. If its still doesn't work try to delete and recreate your database in cpanel and check if you have made all necessary migrations, don't forget to restart your python app. If after all these it still doesn't work check if all your files have the correct file permission(766).

Django deployment, cannot import settings

I'm trying to deploy an application with django.
I've put my django_project directory in /home/XXX/websites/YYY.
The web server root is in /srv/http/YYY and it only contains a directory named static and an apache.wsgi file.
The content of apache.wsgi is as follow:
import os, sys
sys.path.append('/home/XXX/websites/YYY')
os.environ['DJANGO_SETTINGS_MODULE'] = 'YYY.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Whenever I reload apache modules and try to load a page, I got an import error :
ImportError: No module named YYY.settings
I don't understand. Since the settings.py file is in /home/XXX/websites/YYY/YYY/settings.py, why this don't work ?
I've manage to make this work by putting the django project in the web server root directory, but that wasn't the place where I wanted to put it.
Thanks for your help.

Relative path in settings.py in django

for various settings(MEDIA_ROOT,TEMPLATE_DIRS) in setting.py it is instructed to give absolute path.I have configured apache with mod_wsgi.I have a wsgi script in the folder named apache that redirects to settings.py.
import os
import sys
path = 'D:/Projects/Dan'
if path not in sys.path:
sys.path.append(path )
os.environ['DJANGO_SETTINGS_MODULE'] = 'Django.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
(don't misunderstand - Django is the name of my project.)
Okay my question is - now that we have imported the path to my project to system path in the wsgi script, isn't it more feasible that I give a relative path in settings.py,since that would make deploying easier.If I am wrong please tell me the standard procedure so that I can set all the path in one file other than 3 files(Apache - httpd.conf, mod_wsgi - django.wsgi, django - settings.py).
Use:
import os
this_directory = os.path.dirname(__file__)
and then:
absolute_directory = os.path.join(this_directory, 'relative.txt')
BTW, calling your project 'Django' is ill advised given that Django package itself is 'django'. You shouldn't rely on differences in case not causing confusion. In short, Python packages/module names should ever differ just by case. Your site project directory is treated as a Python package and thus the problem.

How do you specify which django version to use?

I am using ubuntu and I have installed django from the Ubuntu Software Center. For some projects I want to use the django cloned from the trunk instead of the default one. How can i do that ? Do I need to unistall the one provided by ubuntu?
Create a virtualenv for your django (with --no-site-packages) and activate it. Then install everything you need inside it.
No, simply make sure that the one you want to use shows up in an earlier directory in sys.path.
if wsgi in use
then set the path to the desired django installation in the django.wsgi file. eg
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
sys.path[:0] = ['/path/to/django/version/','/path/to/project/']
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
If modpython then set the path in the apache configuration file
PythonPath "['/path/to/django/version','/path/to/project'] + sys.path"
The docs might help:
http://docs.djangoproject.com/en/dev/howto/deployment/modpython/

Works using Django development server, but throws import error with Apache using mod_wsgi

I have a Django project that works fine with the development server that comes with it.
No errors are produced at all when I use "django manage.py runserver" and the app works fine, but when I try to use it with mod_wsgi and Apache the browser displays "Internal Server Error" with a 500 error code and it generates an import error in the Apache error log.
Here's the error in the log:
ImportError: No module named registration
I'm using the Django registration module which is located in a path like this:
/opt/raj/photos/registration
I know that the registration app is in the path because I can fire up a Python shell, import sys, and get a list of paths using sys.path.
Here are some of the paths output from Python shell:
sys.path
['', '/opt/raj/pyamf', '/opt/raj', '/opt/raj/pictures', '/opt/raj/pictures /registration', '/usr/lib/python2.6',....]
Any thoughts would be appreciated.
Is it in the pythonpath for the webserver? All those '/opt' paths are typically not in the standard python path, so something is adding those for you I would guess. Are you sure it also gets added for the webserver process, or is PYTHONPATH set in some shell configfile somewhere for your user only?
There is a PythonPath directive when using mod_python, is there something similar for mod_wsgi?
This is almost certainly a case of the path not being the same for the webserver as it is for you, so I would focus my search in those areas.