Make mod_wsgi use python2.7.2 instead of python2.6?

i am running Ubuntu 10.04.1 LTS and it came pre-packed with python2.6 but i need to replace it with python2.7.2.

(The reason is simple, 2.7 has a lot of features backported from 3 )

i had installed python2.7.2 using

./configure
make
make altinstall

the altinstall option installed it, without touching the system default version, to /usr/local/lib/python2.7 and placed the interpreter in /usr/local/bin/python2.7

Then to help mod_wsgi find python2.7 i added the following to /etc/apache2/sites-available/wsgisite

WSGIPythonHome /usr/local

i start apache and run a test wsgi app BUT i am greeted by python 2.6.5 and not Python2.7

Later i replaced the default python simlink to point to python 2.7

ln -f /usr/local/bin/python2.7 /usr/bin/python

Now typing 'python' on the console opens python2.7 but somehow mod_wsgi still picks up python2.6

Next i tried,

PATH=/usr/local/bin:$PATH
export PATH

then do a quick restart apache, but yet again its python2.6 !!


Here is my $PATH

/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

contents of /etc/apache2/sites-available/wsgisite

WSGIPythonHome /usr/local


<VirtualHost *:80>

    ServerName wsgitest.local
    DocumentRoot /home/wwwhost/pydocs/wsgi
    <Directory /home/wwwhost/pydocs/wsgi>
        Order allow,deny
        Allow from all
    </Directory>

    WSGIScriptAlias / /home/wwwhost/pydocs/wsgi/app.wsgi

</VirtualHost>

app.wsgi

import sys

def application(environ, start_response):
    status = '200 OK'
    output = sys.version

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

Apache error.log

'import site' failed; use -v for traceback
[Sun Jun 19 00:27:21 2011] [info] mod_wsgi (pid=23235): Initializing Python.
[Sun Jun 19 00:27:21 2011] [notice] Apache/2.2.14 (Ubuntu) mod_wsgi/2.8 Python/2.6.5 configured -- resuming normal operations
[Sun Jun 19 00:27:21 2011] [info] Server built: Nov 18 2010 21:20:56
[Sun Jun 19 00:27:21 2011] [info] mod_wsgi (pid=23238): Attach interpreter ''.
[Sun Jun 19 00:27:21 2011] [info] mod_wsgi (pid=23239): Attach interpreter ''.
[Sun Jun 19 00:27:31 2011] [info] mod_wsgi (pid=23238): Create interpreter 'wsgitest.local|'.
[Sun Jun 19 00:27:31 2011] [info] [client 192.168.1.205] mod_wsgi (pid=23238, process='', application='wsgitest.local|'): Loading WSGI script '/home/wwwhost/pydocs/$
[Sun Jun 19 00:27:50 2011] [info] mod_wsgi (pid=23239): Create interpreter 'wsgitest.local|'.

Has anybody ever managed to make mod_wsgi run on a non-system default version of python ?


compiling python 2.7

./configure \
   --prefix=/usr/local \
   --enable-unicode=ucs4 \
   --enable-shared \
   LDFLAGS="-Wl,-rpath /usr/local/lib"

make && make altinstall

don't forget --enable-shared or you may have problems later on.

compiling mod_wsgi for python 2.7

https://code.google.com/p/modwsgi/wiki/InstallationIssues

since you did a make altinstall to install python2.7 you won't have a python-devel package to install; so you would need the mod_wsgi to refer to proper python.

./configure --with-python=/usr/local/bin/python2.7 
# then edit Makefile if you want to change DESTDIR
make && make install

... try to start httpd ...

Starting httpd: httpd: Syntax error on line 221 of /etc/httpd/conf/httpd.conf: Syntax error on line 2 of /etc/httpd/conf.d/wsgi.conf: Cannot load /opt/mod_wsgi2.7/usr/lib64/httpd/modules/mod_wsgi.so into server: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

Because we are not using the same python and we didn't link the module with any particular option to make it look in the correct place, it cannot find libpython2.7.so.1.0, we can change that by making libtool check the correct place.

# use LDFLAGS to tell libtool resulting lib needs to
# look for shared libs in /usr/local/lib too.
./configure \
   --with-python=/usr/local/bin/python2.7 \
   LDFLAGS="-R/usr/local/lib"
# then edit Makefile if you want to change DESTDIR
# e.g. DESTDIR = /opt/mod_wsgi2.7
make && make install

... try to start http again ...

Starting httpd: httpd: Syntax error on line 221 of /etc/httpd/conf/httpd.conf: Syntax error on line 2 of /etc/httpd/conf.d/wsgi.conf: Cannot load /opt/mod_wsgi2.7/usr/lib64/httpd/modules/mod_wsgi.so into server: /opt/mod_wsgi2.7/usr/lib64/httpd/modules/mod_wsgi.so: cannot open shared object file: Permission denied

That last error is because my system is running selinux and the file has the default context. A quick look at stackoverflow tells me it is an selinux issue.

fixing selinux context

# ls -Z /opt/mod_wsgi2.7/usr/lib64/httpd/modules/mod_wsgi.so 
-rwxr-xr-x. root root unconfined_u:object_r:user_tmp_t:s0 /opt/mod_wsgi2.7/usr/lib64/httpd/modules/mod_wsgi.so

The fix is to use the correct context, which can be found on the original mod_wsgi module.

chcon --reference /etc/httpd/modules/mod_wsgi.so /opt/mod_wsgi2.7/usr/lib64/httpd/modules/mod_wsgi.so 

You need to recompile the mod_wsgi too. Just recompiling python isn't enough (don't forget to load the right version of mod_wsgi)

EDIT: click here for installation instructions


This is how you can configure mod_wsgi to use Python2.7

I happened to face this same issue. And I was looking at the option on uninstalling mod_wsgi and re-installing it with appropriate configs.

Reading one of the articles I realized there was no need to un-install my current mod_wsgi and I could just go ahead and re-install mod_wsgi3.4 (earlier i had v3.2) with the settings to use Python2.7 (seems like the installation process re-writes everything without any errors/conflicts).

Since I already had Python2.7 installed.

I reinstalled mod_wsgi-3.4 (without performing any un-installations)

[root@server ~]# cd ~

[root@server ~]# wget http://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz

[root@server ~]# tar xvf mod_wsgi-3.4.tar.gz

[root@server ~]# cd mod_wsgi-3.4

Configured mod_wsgi with the installed python2.7

[root@server ~]#./configure --with-python=/usr/local/bin/python2.7

[root@server ~]# make

[root@server ~]# make install

The below two commands are very important. Replace /usr/local/lib with the folder where you have installed libpython2.7.so.1.0 if it is not in /usr/local/lib.

[root@server ~]# LD_LIBRARY_PATH=/usr/local/lib /usr/local/bin/python

[root@server ~]# ldconfig

Restart Apache Server

[root@server ~]# service httpd restart

[root@server ~]# ldd /etc/httpd/modules/mod_wsgi.so

Output of the above command:Line2 indicates that your mod_wsgi is now using Python2.7 libraries. YAY!

    linux-vdso.so.1 =>  (0x00007fffc0aa9000)
    libpython2.7.so.1.0 => /usr/lib/libpython2.7.so.1.0 (0x00007f03a5b20000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f03a5903000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007f03a56fe000)
    libutil.so.1 => /lib64/libutil.so.1 (0x00007f03a54fb000)
    libm.so.6 => /lib64/libm.so.6 (0x00007f03a5277000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f03a4ee2000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f03a6133000)