fir3net
PPS-Firenetbanner-780.5x190-30-03-17

How do I compile mod_wgsi for Python 2.7

Recently I found myself in a situation where I needed to recompile mod_wgsi against a newer version of Python.

This involves recompiling Python with the correct flags, then recompiling mod_wsgi using the newly compiled Python. Below shows the steps,

Check Version

First of all check what version of Python mod_wsgi was compiled with.

[root@server]# ldd /usr/lib64/httpd/modules/mod_wsgi.so  
    libpython2.4.so.1.0 => /usr/lib64/libpython2.4.so.1.0 (0x00002b3149c4e000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b3149f82000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00002b314a19d000)
    libutil.so.1 => /lib64/libutil.so.1 (0x00002b314a3a2000)
    libm.so.6 => /lib64/libm.so.6 (0x00002b314a5a5000)
    libc.so.6 => /lib64/libc.so.6 (0x00002b314a828000)
    /lib64/ld-linux-x86-64.so.2 (0x000000364b200000)

 

Compile Python

Next, compile Python. The option to build a shared library is also used to prevent future issues (details can be found here).

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

cd ~
wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz
tar xvf Python-2.7.5.tgz
cd Python-2.7.5

./configure --enable-shared --with-threads
make
make install

Once you have complete these steps you may get the error,

/usr/local/bin/python2.7: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

There are 2 ways to fix this. You can :

1. run 'echo export LD_LIBRARY_PATH=/usr/local/lib >> /etc/profile' or

2. configure the following softlinks

ln -s /usr/local/lib/libpython2.7.so.1.0 /usr/lib/
ln -s /usr/local/lib/libpython2.7.so /usr/

 

Compile WSGI

 Finally we compile wsgi,

cd ~
wget http://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz
tar xvf mod_wsgi-3.4.tar.gz
cd mod_wsgi-3.4

./configure  --with-python=/usr/local/bin/python2.7
make
make install

 

References

http://cysec.org/blog/2011/06/10/compiling-mod_wsgi-for-a-custom-python-build/index.html

http://stackoverflow.com/questions/7880454/python-executable-not-finding-libpython-shared-library

About the Author

RDonato

R Donato

Ricky Donato is the Founder and Chief Editor of Fir3net.com. He currently works as a Principal Network Security Engineer and has a keen interest in automation and the cloud.

You can find Ricky on Twitter @f3lix001