10

I deploy my Django web sites on Apache2 with mod_wsgi on ubuntu.

In my Django views, I import a module that requires a specific path set in LD_LIBRARY_PATH.

When I set LD_LIBRARY_PATH in /etc/apache2/envvars as:

export LD_LIBRARY_PATH=/home/user/target_libdir:$LD_LIBRARY_PATH

it works.

However, on my server I run multiple django web sites, each in independent VirtualHost entry, with independent wsgi scripts.

The problem is that the web sites need to use different LD_LIBRARY_PATH versions.

So, how can I set LD_LIBRARY_PATH individually for every django web site?

jan
  • 1,593
  • 2
  • 12
  • 15

3 Answers3

6

You can't do it. The LD_LIBRARY_PATH is only read once on initial process start. It cannot be set once process is running nor can you set it again prior to a fork. You can set it prior to an exec, but mod_wsgi daemon processes are fork only and not an exec.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
4

Well there are situations where you simply can't set the LD_LIBRARY_PATH variable before the script is run and you still would like to import one or two of custom libraries - lets say from a home directory on your hosting server where you have very little access rights, or customize it for every site like in the question above.

In these cases where all sensible solutions are not available you can load the libraries using ctypes and than import the module that uses them. It is simple to adopt this idea to load all libraries from a custom folder for every site, like in the question above.

So for the issue I had with libpuzzle with one dependency I ended up doing:

from ctypes import *
lib1 = cdll.LoadLibrary('/home/username/lib/libpuzzle.so')
lib2 = cdll.LoadLibrary('/home/username/lib/libgd.so')

import pypuzzle
3

Update

The following apparently doesn't work. Leaving it here as a warning to others.


Might work if you set in in your .wsgi file. Assuming you have something like:

import os,sys

sys.path.append('/path/to/django')

os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
os.environ['LD_LIBRARY_PATH'] = '/path/to/library'

import django.core.handlers.wsgi

_application = django.core.handlers.wsgi.WSGIHandler()

def application(environ,start_response):
  return _application(environ,start_response)
MattH
  • 37,273
  • 11
  • 82
  • 84
  • 4
    I cannot get it working this way, and by the way I don't think this solution is supposed to work at all. If i understand correctly LD_LIBRARY_PATH the must be set before python is executed as explained here: http://stackoverflow.com/questions/1178094/change-current-process-environment and here http://stackoverflow.com/questions/856116/changing-ld-library-path-at-runtime-for-ctypes – jan Oct 21 '11 at 09:32