Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set LD_LIBRARY_PATH individually for django web sites with Apache and mod_wsgi

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?

like image 471
jan Avatar asked Oct 21 '11 09:10

jan


2 Answers

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.

like image 86
Graham Dumpleton Avatar answered Oct 21 '22 15:10

Graham Dumpleton


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
like image 23
pawel lukaszewicz Avatar answered Oct 21 '22 13:10

pawel lukaszewicz