Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django WGSI paths

I am having problems setting up wgsi with django. I'm following this http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/ . Yet I am still really confused as to where to put the .wsgi file and if I need to set the sys.path. I have tried it both directly outside and inside the web root and I can't get anything to work as expected.

# /home/ben/public_html/django_test/testproject/apache/django.wsgi:

import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'testproject.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Relivant apache conf:

DocumentRoot "/home/ben/public_html/django_test/testproject/"
WSGIScriptAlias / "/home/ben/public_html/django_test/testproject/apache/django.wsgi"

Apache Logs Error (standard apache 500 page):

ImportError: Could not import settings 'testproject.settings' (Is it on sys.path? ...

I can at get django to at least throw an error of it's own by using this:

import os
import sys

path = '/home/ben/public_html/django_test/testproject'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

which resulted in this django error page:

ImportError at /admin/
No module named testproject.urls
like image 964
Keyo Avatar asked Nov 16 '10 12:11

Keyo


2 Answers

I put the wsgi at same level than settings.py, and looks like this:

import os
import sys

sys.path.insert(0,os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2]))


os.environ['DJANGO_SETTINGS_MODULE'] = 'yourprojectname.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

this is the apache conf file:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName www.yourprojectname.com
    Alias /media/ /home/diegueus9/workspace/yourprojectname/media/

    <Directory /home/diegueus9/workspace/yourprojectname/media/>
        Order deny,allow
        Allow from all
    </Directory>

    WSGIScriptReloading On
    WSGIDaemonProcess yourprojectname 
    WSGIProcessGroup yourprojectname
    WSGIApplicationGroup yourprojectname
    WSGIPassAuthorization On

    WSGIScriptAlias / /home/diegueus9/workspace/yourprojectname/yourfile.wsgi
    ErrorLog /var/log/apache2/yourprojectname-error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel debug

    CustomLog /var/log/apache2/yourprojectname-access.log combined

</VirtualHost>
like image 175
diegueus9 Avatar answered Sep 19 '22 22:09

diegueus9


As a quick fix you can, next to adding this to sys.path:

path = '/home/ben/public_html/django_test/testproject'

also add this:

path2 = '/home/ben/public_html/django_test'

You can also change Python path in your Apache VirtualHost:

WSGIDaemonProcess [...] python-path=/home/ben/public_html/django_test

But you should also review mod_wsgi docs and learn what Graham Dumpleton advises there.

like image 26
Tomasz Zieliński Avatar answered Sep 21 '22 22:09

Tomasz Zieliński