You are not logged in.

#1 2008-01-05 05:13:30

IAIHMB
Member
From: Hudson, Florida.
Registered: 2006-09-19
Posts: 1362

Using Django with mod_wsgi.

Using Django with mod_wsgi is pretty straight forward, you've just got to:

1) Install Apache/mod_wsgi:

http://forum.webfaction.com/viewtopic.php?id=1086

2) Install Django.

You could install Django in ~/lib/python2.4/ or ~/lib/python2.5/ but doing so makes it difficult to use multiple versions of Django so we're going to install it in ~/webapps/(Application Name)/.

Execute the following commands:

Code:

cd ~/webapps/(Application Name)/
svn export http://svn.djangoproject.com/svn/django/trunk/django/

If you want to use a different version of Django find it in http://code.djangoproject.com/svn/django/tags/releases/ and update the "svn export..." command accordingly.

3) Create a file with the following contents:

Code:

import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
sys.path.insert(0, '/home2/iaihmb/webapps/django/')

# This has to be done here otherwise Django won't be in a directory
# that's in PYTHONPATH.
from django.core.handlers.wsgi import WSGIHandler

application = WSGIHandler()

Replace:

-/home2/iaihmb/webapps/django/ with the absolute path to ~/webapps/(Application Name)/.
-myproject.settings with the PYTHONPATH to your Django project's settings module.

Name the file django.wsgi and place it in ~/webapps/(Application Name)/.

4) Add the following to ~/webapps/(Application Name)/httpd/conf/httpd.conf:

Code:

ServerLimit 3

<Directory /home2/iaihmb/webapps/django/>
    Order allow,deny
    Allow from all
</Directory>

WSGIScriptAlias / /home2/iaihmb/webapps/django/django.wsgi

Replace /home2/iaihmb/webapps/django/ with the absolute path to ~/webapps/(Application Name)/.

5) Restart Apache by executing the following commands:

Code:

cd ~/webapps/(Application Name)/
./httpd/bin/apachectl restart

That's it. smile

Last edited by IAIHMB (2008-01-05 05:15:24)


-David Sissitka

Offline

 

#2 2008-01-05 07:33:47

lostylost
Member
Registered: 2008-01-01
Posts: 4

Re: Using Django with mod_wsgi.

David, you rock. You do a great job here.

Offline

 

#3 2008-02-17 17:28:34

tlopez
Member
Registered: 2007-08-30
Posts: 21

Re: Using Django with mod_wsgi.

I'm not clear about the python path to myproject.settings. I've created myproject and have the lines in django.wsgi:

os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
sys.path.insert(0, '/home/me/webapps/(appname)/')

But the error_log shows that myproject.settings can't be found. The path is wrong? I'm following from this and the installing wsgi posts. Apache and wsgi have been installed successfully. Any idea what I'm missing on the PYTHONPATH? Django is installed under /home/me/webapps/(appname)/



Thanks,

tlopez

Offline

 

#4 2008-02-18 09:34:09

tlopez
Member
Registered: 2007-08-30
Posts: 21

Re: Using Django with mod_wsgi.

For other noobs, here's what I did wrong.
was:
sys.path.insert(0,'/home/me/webapps/(appname)/')

Should be:
sys.path.insert(0,'/home/me/webapps/(appname)/django/bin/')

Thats because I checked out django into the appname folder:
/home/me/webapps/(appname)/django

Therefore settings are in:
/home/me/webapps/(appname)/django/bin/

Hopefully this will help others

tlopez

Offline

 

#5 2008-04-03 13:27:25

eric
Member
Registered: 2007-10-23
Posts: 38

Re: Using Django with mod_wsgi.

I've got three django applications, all of which I'm trying to move over to using mod_wsgi. The first django application I installed came with an 'apache' directory, the next two got 'apache2' directories, with much less in them. I installed mod_wsgi in the first application and switched it over, apparently without problems. I went to install mod_wsgi in the other two applications, and saw there's no apxs to build against; if I leave that line out of the mod_wsgi configure, it finds the apxs in my first django application's apache installation (but seems to make just fine).

My question is, are my second two apache installations subordinate to the first? Can I safely point them at the mod_wsgi module in the first django/apache installation? Do I need to install mod_wsgi once for each django application I've got running?

Thanks,
Eric

Offline

 

#6 2008-04-03 21:07:49

IAIHMB
Member
From: Hudson, Florida.
Registered: 2006-09-19
Posts: 1362

Re: Using Django with mod_wsgi.

If all of your installations of Apache were compiled the same way then one's mod_wsgi.so should work with the others.  smile


-David Sissitka

Offline

 

#7 2008-04-07 16:47:54

eric
Member
Registered: 2007-10-23
Posts: 38

Re: Using Django with mod_wsgi.

Okay, one more question. None of the django apps I've got going under mod_wsgi are able to reach the admin media. My other media is served from a different domain, so that works fine, but the admin media comes like this:

Alias /admedia "/home/username/webapps/djangomedia/admin"

Where that is a symlink to the actual media files in my django installation. Visiting anything under /admedia results in a server error, though I can't find that error in any error logs. According to this page:

http://code.djangoproject.com/wiki/djan … d_mod_wsgi

I should be able to get admin media served with a configuration like this:

Alias /media/ "<PATH TO YOUR DJANGO SRC>/trunk/django/contrib/admin/media/"
<Directory "<PATH TO YOUR DJANGO SRC>/trunk/django/contrib/admin/media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>

I don't know if that would solve my problem, because when I try to put something like that in, it tells me that Order is an unrecognized command. That's weird, because mod_access should be part of the base Apache configuration, and anyway I don't seem to have mod_access anywhere to load it.

Is my problem not what I think it is? What's going on here?

Thanks,
Eric

Offline

 

#8 2008-04-11 13:30:17

eric
Member
Registered: 2007-10-23
Posts: 38

Re: Using Django with mod_wsgi.

Never mind! I'm stupid.

Offline

 

#9 2008-04-29 17:54:59

IAIHMB
Member
From: Hudson, Florida.
Registered: 2006-09-19
Posts: 1362

Re: Using Django with mod_wsgi.

Django/mod_wsgi has been added to the control panel. smile


-David Sissitka

Offline

 

#10 2008-05-08 20:53:23

snewman
Member
Registered: 2008-05-08
Posts: 5

Re: Using Django with mod_wsgi.

What's the recommended procedure for hosting multiple Django projects on one Apache & mod_wsgi instance?

I added this to the end of my httpd.conf and it works fine, but I wanted to make sure it's the best way to do it:

(1234 is obviously a dummy port number for this example)

NameVirtualHost 127.0.0.1:1234
ServerAdmin me@myemail.com

<VirtualHost 127.0.0.1:1234>
    WSGIScriptAlias / /path/to/my/first_file.wsgi
    ServerName myservername.com
    ServerAlias myserveralias.com
    ErrorLog "logs/firstsite_errors_log"
</VirtualHost>

<VirtualHost 127.0.0.1:1234>
    WSGIScriptAlias / /path/to/my/second_file.wsgi
    ServerName myotherservername.com
    ServerAlias myotherserveralias.com
    ErrorLog "logs/secondsite_errors_log"
</VirtualHost>

Offline

 

#11 2008-05-09 05:17:39

eric
Member
Registered: 2007-10-23
Posts: 38

Re: Using Django with mod_wsgi.

That's what I'm doing (three medium-sized sites under one Apache/mod_wsgi/worker MPM installation) and it's working just fine for me. It might be me, but there seems to be a noticeable lag now in establishing the initial connection, though the response then comes through as quickly as ever.

Offline

 

#12 2008-05-09 09:19:03

sime
Member
Registered: 2006-10-24
Posts: 96

Re: Using Django with mod_wsgi.

snewman, that looks fine to me...

Regards,


WebFaction - Smarter web hosting
http://webfaction.com - http://twitter.com/webfaction

Offline

 

#13 2008-05-09 20:03:36

snewman
Member
Registered: 2008-05-08
Posts: 5

Re: Using Django with mod_wsgi.

eric wrote:

That's what I'm doing (three medium-sized sites under one Apache/mod_wsgi/worker MPM installation) and it's working just fine for me. It might be me, but there seems to be a noticeable lag now in establishing the initial connection, though the response then comes through as quickly as ever.

i noticed the same thing on my setup  It feels like a half to full-second lag on the initial request. It makes me wonder if I should explore another implementation.  mod_python is by far the easiest to set up and use, but I was constantly blowing past my memory allotment.

Offline

 

#14 2008-05-11 11:44:58

Pigletto
Member
From: Poland
Registered: 2007-05-06
Posts: 82
Website

Re: Using Django with mod_wsgi.

I've just setup my test site with mod_wsgi.
Before, I tried standard installation from webfaction that was mod_python with Apache in MPM prefork mode. This was stable but memory usage was/is huge. I have 5 applications on this Apache (with virtual hosts) and MaxClients set to only 3 - memory usage is about 160 MB so it is upper limit for my account. Too much...

I also tried mod_python with Apache in MPM worker. Memory usage was noticeably lower, but I had a lot of strange errors. I know that some of them were related to thread safety (I even found tickets about these at django ticket system, eg. template loader). Rest of them, I think, is related to thread safety too but I was unable to figure out where something might be wrong (errors from django template system).

Currently I'm trying mod_wsgi with Apache in MPM worker mode and with mod_wsgi running at daemon mode (processes=3 threads=1). So far it looks that memory usage is lower (but I've checked only my test site, not production one), but I also noticed this lag when processes are restarted.
Snewman: my apache.conf with virtual hosts is similiar to yours, but it was also my first mod_wsgi setup smile

In general I've found mod_wsgi to be easier to set up than mod_python, also mod_wsgi site is full of great documentation that is very interesting even if you're not using mod_wsgi.

Offline

 

#15 2008-06-28 07:23:44

leehinde
Member
Registered: 2008-05-11
Posts: 4

Re: Using Django with mod_wsgi.

so, following along, I get this error when I try and start apache:

syntax error on line 16 of. ..httpd.conf:
Invalid command 'Order', perhaps misspelled or defined by a module not included in the server configuration


This is what I have:


<Directory /home/msainson/webapps/results/>
    Order allow,deny
    Allow from all
</Directory>


Line 16 is the line that starts with (the correctly spelled) Order.

Offline

 

#16 2008-06-29 07:01:53

IAIHMB
Member
From: Hudson, Florida.
Registered: 2006-09-19
Posts: 1362

Re: Using Django with mod_wsgi.

In Apache 2.0 Order is a mod_access directive, do you have a "LoadModule mod_access modules/mod_access.so" line in your httpd.conf? If not, add one and restart Apache.

Hope it helps. smile


-David Sissitka

Offline

 

#17 2008-08-01 04:29:27

pablo
Member
From: Guatemala
Registered: 2007-08-18
Posts: 5
Website

Re: Using Django with mod_wsgi.

Here is my configuration for running multiple proyects with the same django/mod_wsgi instance:

Code:

# optimization
StartServers 1
MinSpareServers 1
MaxSpareServers 5
MaxClients 5
MaxRequestsPerChild 300
ServerLimit 4
Keepalive off
HostnameLookups Off

NameVirtualHost 127.0.0.1:7268
ServerAdmin mail@mail.com

<VirtualHost 127.0.0.1:7268>
    WSGIScriptAlias / /path/to/site1.wsgi
    ServerName site1.com
    ServerAlias www.site1com
    ErrorLog "logs/site1_errors_log"
</VirtualHost>

<VirtualHost 127.0.0.1:7268>
    WSGIScriptAlias / /path/to/site2.wsgi
    ServerName site2.com
    ServerAlias *.site2.com
    ErrorLog "logs/site2_errors_log"
</VirtualHost>

-------
webfaction username: movaxes

Last edited by pablo (2008-08-01 04:29:58)


webfaction username: movaxes
http://pablorosales.com

Offline

 

#18 2008-08-01 09:58:56

likebike
Member
Registered: 2008-05-05
Posts: 263

Re: Using Django with mod_wsgi.

Thanks pablo!  That's great!

~Chris Sebastian

pablo wrote:

Code:

# optimization
StartServers 1
MinSpareServers 1
MaxSpareServers 5
MaxClients 5
MaxRequestsPerChild 300
ServerLimit 4
Keepalive off
HostnameLookups Off

NameVirtualHost 127.0.0.1:7268
ServerAdmin mail@mail.com

<VirtualHost 127.0.0.1:7268>
    WSGIScriptAlias / /path/to/site1.wsgi
    ServerName site1.com
    ServerAlias www.site1com
    ErrorLog "logs/site1_errors_log"
</VirtualHost>

<VirtualHost 127.0.0.1:7268>
    WSGIScriptAlias / /path/to/site2.wsgi
    ServerName site2.com
    ServerAlias *.site2.com
    ErrorLog "logs/site2_errors_log"
</VirtualHost>

Offline

 

#19 2008-12-29 19:38:22

pmott
Member
From: Wakefield, UK
Registered: 2007-01-02
Posts: 33

Re: Using Django with mod_wsgi.

I found that:

      svn export http://svn.djangoproject.com/svn/django/trunk/django/

did not work but that

     svn export http://code.djangoproject.com/svn/django/trunk/django/

did. I assume there has been a chnage at djangoproject.com

Peter

Offline

 

#20 2009-04-08 07:18:42

donothurry
Member
Registered: 2009-04-05
Posts: 2

Re: Using Django with mod_wsgi.

Is it necessary for me to install mod_wsgi for my version of Apache?  I signed up for hosting two days ago.  I think mod_wsgi is already installed.

Can I delete the myproject Django project and its myproject.wsgi file so that my site's root Django project is named something else?

Thanks!
Mike

Offline

 

#21 2009-04-08 16:24:18

seanf
Administrator
Registered: 2008-02-02
Posts: 1899
Website

Re: Using Django with mod_wsgi.

Hi Mike -

If you've installed a Django/mod_wsgi app via our control panel, then you do not need to install a separate mod_wsgi application.

Yes, you can delete myproject* from you Django app and use your own project. You might want to keep myproject.wsgi around for reference, since you'll need to have a wsgi script for your new Django project.

Hope that helps!

Regards,

Sean F, WebFaction Support
--
WebFaction - Agile hosting for everyone
http://webfaction.com - http://twitter.com/webfaction

Offline

 

#22 2009-04-30 17:49:03

airjaw
Member
Registered: 2009-04-30
Posts: 10

Re: Using Django with mod_wsgi.

Is there an updated installation guide for django/mod_wsgi/apache2 ?

I get this error : syntax error on line 16 of. ..httpd.conf:
Invalid command 'Order', perhaps misspelled or defined by a module not included in the server configuration

I've also followed this: In Apache 2.0 Order is a mod_access directive, do you have a "LoadModule mod_access modules/mod_access.so" line in your httpd.conf? If not, add one and restart Apache.

and now get this: /home/airjaw/webapps/django/apache2/modules/mod_access.so: cannot open shared object file: No such file or directory 

a look into my /modules directory and there is no mod_access.so

Any help would be appreciated. Thanks.

Edit:: just found this from another post::

"Ah - N00b mistake, Doh - mod_access is now "LoadModule authz_host_module modules/mod_authz_host.so" in Apache 2.2.  Hope that helps someone else.  Still getting 500 Internal Server error though."

Last edited by airjaw (2009-04-30 17:58:59)

Offline

 

#23 2009-04-30 18:05:36

seanf
Administrator
Registered: 2008-02-02
Posts: 1899
Website

Re: Using Django with mod_wsgi.

airjaw wrote:

Is there an updated installation guide for django/mod_wsgi/apache2 ?

I get this error : syntax error on line 16 of. ..httpd.conf:
Invalid command 'Order', perhaps misspelled or defined by a module not included in the server configuration

I've also followed this: In Apache 2.0 Order is a mod_access directive, do you have a "LoadModule mod_access modules/mod_access.so" line in your httpd.conf? If not, add one and restart Apache.

and now get this: /home/airjaw/webapps/django/apache2/modules/mod_access.so: cannot open shared object file: No such file or directory 

a look into my /modules directory and there is no mod_access.so

Any help would be appreciated. Thanks.

Edit:: just found this from another post::

"Ah - N00b mistake, Doh - mod_access is now "LoadModule authz_host_module modules/mod_authz_host.so" in Apache 2.2.  Hope that helps someone else.  Still getting 500 Internal Server error though."

Ok, so you found the mod_authz_host stuff smile

The traceback for your current 500 error is in ~/webapps/django/apache2/logs/error_log - basically, you need to fix your sys.path in your WSGI script so that Python can find all of your libraries (or install the libraries if they don't exist).

Offline

 

#24 2010-03-17 02:17:39

mae
Member
Registered: 2006-10-15
Posts: 445

Re: Using Django with mod_wsgi.

FYI, this is no longer needed. Please note the Control Panel has scripts to set this up.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson