[Answered ]-Buildout config for multiple Django WSGI files

1đź‘Ť

âś…

Since it is not possible to set environment variable in apache config and retrieve it in wsgi script (explained in: mod-wsgi wiki) it seems that the most elegant solution is to have separate wsgi scripts.

(There was a related discussion on SO: Django – Can't pass Environment Variable to Apache/Passenger on the WSGI Interface)

Now if you are about to create wsgi scripts manually you will have to deal with sys.path manually. So it seems that to it is easier to have few sections of django.recipe.


It is also possible not to use django-recipe at all. At least I prefer this because then I have full freedom to setup my wsgi/manage scripts. And it is not that hard to configure buildout in a way that it will wrap your manually written scripts into bin folder with sys.path automatically configured.

Here is how to achieve it:

  • Create your wsgi, manage scripts manually at myproject.scrpits.* as described in django documentation. However, wrap the “active” part to a def main(): method. Then, your scripts can be used as modules.

  • Create proper setup.py script for your project. It will install scripts which you created in the first step. Entry-points part is important here:

    from distutils.core import setup
    setup(name='mygroject',
          packages=['myproject'],
          entry_points="""
          [console_scripts]
          manage = myproject.scripts.manage:main
          wsgi = mygroject.scripts.wsgi:main
          """
         )
    
  • Configure buildout. python:scripts is important here:

    [buildout]
    ...
    # Add directory where your setup.py can be found.
    # I assume that you placed your setup.py in same directory as your buildout file.
    develop=.
    
    [python]
    recipe = zc.recipe.egg:scripts
    # django is no longer instlled by django-recipe so has to be listed in eggs.
    # myproject also has to be listed here.
    eggs= ... django myproject
    # Now scripts from the setup.py:
    scripts = manage wsgi
    # fallowing will create bin/python, might be useful:
    interpreter = python
    
  • Now buildout will generate scripts in bin folder which you defined in steps 1-2.

👤Ski

1đź‘Ť

Every wsgi script that you run behind your webserver is a separate entity. So: is the django settings module that you use the only difference? I mean, are you really sure it is safe to run two or more separate sites in the same directory? Take care of shared directories that should not really be shared and so. The – possibly safer – alternative to what you’re doing is to just have two separate buildouts.

On the other hand, you say you’ve got it working now by using two djangorecipe parts. Well, what’s the real problem? OK, django is duplicated. Couple of megabytes: how bad is that? Upload three big photos to your website and that takes the same amount of space. So: isn’t your current solution good enough already?

Third comment: I think your setup is pretty much unique, so getting support for that in djangorecipe itself will be hard. You could try and get it working, I think the recipe is on launchpad, ready to fork.

Leave a comment