Can't load mod_wsgi compiled for Python 3

2020-03-30 05:32发布

问题:

I'm on CentOS and trying to configure Apache to use mod_wsgi compiled against Anaconda Python 3.5. Compiling mod_wsgi seems to go OK:

sudo yum install httpd-devel
sudo ./configure --with-python=/opt/anaconda/anaconda3/bin/python
sudo make

ls -l /etc/httpd/modules/mod_wsgi.so
-rwxr-xr-x. 1 root root 702205 Mar  2 23:12 /etc/httpd/modules/mod_wsgi.so

But when I start the web server it can't seem to find its libraries:

sudo service httpd start
Starting httpd: httpd: Syntax error on line 221 of /etc/httpd/conf/httpd.conf:
Syntax error on line 1 of /etc/httpd/conf.d/wsgi.conf: Cannot load
/etc/httpd/modules/mod_wsgi.so into server: libpython3.5m.so.1.0: cannot
open shared object file: No such file or directory

libpython3.5m.so.1.0 is definitely in /opt/anaconda/anaconda3/lib, so I suppose the mod_wsgi.so module doesn't know where to look for it. Yet the mod_wsgi config.status seems to confirm that

S["LDFLAGS"]=" -L/opt/anaconda/anaconda3/lib -L/opt/anaconda/anaconda3/lib/python3.5/config-3.5m "

so I'm at a loss... Can anyone point me in the right direction?

EDIT: OK, thanks to @mata, I think I've compiled mod_wsgi against the correct libraries. But now the server logs just fill up with the same repeated message:

Current thread 0x00007f60d68d07e0 (most recent call first):
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

So I'm guessing apache is looking in the wrong place for the python libraries. The 00-wsgi.conf file giving all the trouble reads:

WSGIPythonHome /opt/anaconda/anaconda3
WSGIPythonPath /opt/anaconda/anaconda3/lib/python3.5/site-packages
LoadModule wsgi_module modules/mod_wsgi.so

so I thought (from what I've read on other threads about this) that I'd told it where to look for my Python install.

回答1:

That means that the library libpython3.5m.so.1.0 can't be found at runtime because the directory /opt/anaconda/anaconda3/lib is not a place where the dynamic linker would look for it.

You can try to rebuild mod_wsgi using:

./configure LDFLAGS='-Wl,-rpath=/opt/anaconda/anaconda3/lib' --with-python=/opt/anaconda/anaconda3/bin/python

That will save the library path within the generated binary.

The other option would be to set the LD_LIBRARY_PATH environment variable for the apache process, which is not really a good method.
Or add the directory /opt/anaconda/anaconda3/lib to the library search path using a conf file in /etc/ld.so.conf.d/, that would be a global setting tough. See man ld-linux for more info.

Also, don't forget to correctly set the WSGIPythonHome directive in your config file.


edit:

I've done some experimenting and I could reproduce your second error message when the python3 binary is not found on the PATH.
In that case it seems setting the WSGIPythonHome directive is not enough, you need to set the PYTHONHOME environment variable before apache is started, or change PATH so the interpreter can be found. On CentOS changing /etc/sysconfig/httpd should do the trick, just add:

export PYTHONHOME=/opt/anaconda/anaconda3
# alternatively this should also work:
export PATH="$PATH:/opt/anaconda/anaconda3/bin"

Or create a symlink to the interpreter in a directory on the path, e.g. /usr/local/bin...
For reference, an extended explanation why this is needed can be found here