Configuring and installing python2.6.7 and mod_wsgi3.3 on RHEL for production
Question: This is a long question detailing all that I did from the start. Hope it
helps. I am working on a django application and need to deploy it on to the
production server. The production server is a virtual server managed by IT,
and I do not have the root access. They have given me rights to manage the
installations of my modules in /swadm and /home/swadm. So I have planned to do
create the following arrangement:

  * `/swadm/etc/httpd/conf` where I maintain httpd.conf
  * `/swadm/etc/httpd/user-modules` where I maintain my apache modules (mod_wsgi)
  * `/swadm/var/www/django/app` where I maintain my django code
  * `/swadm/usr/local/python/2.6` where I will maintain my python 2.6.7 installation with modules like django, south etc.
  * `/home/swadm/setup` where I will be storing the required source tarballs and doing all the building and installing out of.
  * `/home/swadm/workspace` where I will be maintaining application code that is in development.

The system has python2.4.3 and python2.6.5 installed but IT recommended that I
maintain my own python installation if I required lot of custom modules to be
installed (which I would be).

So I downloaded python2.6.7 source. I needed to ensure python is installed
such that its shared library is available. When I ran the configure script
with only the option `--enable-shared` and
`--prefix=/swadm/usr/local/python/2.6`, it would get installed but
surprisingly point to the system's installation of python2.6.5.

    
    
    $ /swadm/usr/local/python/2.6/bin/python
    Python 2.6.5 (r265:79063, Feb 28 2011, 21:55:45)
    [GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

So I ran the configure script following instructions from [Building Python
with --enable-shared in non-standard
location](http://koansys.com/tech/building-python-with-enable-shared-in-non-
standard-location) as

    
    
    ./configure --enable-shared --prefix=/swadm/usr/local/python/2.6  LDFLAGS="-Wl,-rpath /swadm/usr/local/python/2.6/lib"
    

Also making sure I had created the directories beforehand ( as the link
suggests) to avoid the errors expected. Now typing
`/swadm/usr/local/python/2.6/bin/python` would start the correct python
version 2.6.7. So I moved on to configuring and installing mod_wsgi. I
configured it as

    
    
    ./configure --with-python=/swadm/usr/local/python/2.6/bin/python
    

the Makefile that was created tries to install the module into
`/usr/lib64/httpd/modules` and I have no write permissions there, so I
modified the makefile to install into `/swadm/etc/httpd/user-modules`. (There
might be a command argument but I could not figure it out). The module got
created fine. A test wsgi script which I used was

    
    
    import sys
    
    def application(environ, start_response):
        status = '200 OK'
        output = 'Hello World!'
        output = output + str(sys.version_info)
        output = output + '\nsys.prefix = %s' % repr(sys.prefix)
        output = output + '\nsys.path = %s' % repr(sys.path)
        response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
        start_response(status, response_headers)
        return [output]
    

And the output shown was, surprisingly

    
    
    Hello World!(2, 6, 5, 'final', 0)
    sys.prefix = '/swadm/usr/local/python/2.6'
    sys.path = ['/swadm/usr/local/python/2.6/lib64/python26.zip',    '/swadm/usr/local/python/2.6/lib64/python2.6/', '/swadm/usr/local/python/2.6/lib64/python2.6/plat-linux2', '/swadm/usr/local/python/2.6/lib64/python2.6/lib-tk', '/swadm/usr/local/python/2.6/lib64/python2.6/lib-old', '/swadm/usr/local/python/2.6/lib64/python2.6/lib-dynload']`
    

So you see somehow the mod_wsgi module still got configured with the system's
python 2.6.5 installation and not my custom one. I tried various things
detailed in the [mod_wsgi
documentation](http://code.google.com/p/modwsgi/wiki/InstallationIssues)

  * Set `WSGIPythonHome` in httpd.conf to `/swadm/usr/local/python/2.6` and `WSGIPythonPath` to `/swadm/usr/local/python/2.6/lib/python2.6`
  * Created a symlink in the python config directory to point to the libpython2.6.so file

$ ln -s ../../libpython2.6.so

When I do `ldd libpython2.6.so` this is what I see:

    
    
    $ ldd libpython2.6.so
     linux-vdso.so.1 =>  (0x00007fffc47fc000)
     libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b666ed62000)
     libdl.so.2 => /lib64/libdl.so.2 (0x00002b666ef7e000)
     libutil.so.1 => /lib64/libutil.so.1 (0x00002b666f182000)
     libm.so.6 => /lib64/libm.so.6 (0x00002b666f385000)
     libc.so.6 => /lib64/libc.so.6 (0x00002b666f609000)
     /lib64/ld-linux-x86-64.so.2 (0x00000031aba00000)
    

And `ldd mod_wsgi.so` gives

    
    
    $ ldd /swadm/etc/httpd/user-modules/mod_wsgi.so
     linux-vdso.so.1 =>  (0x00007fff1ad6e000)
     libpython2.6.so.1.0 => /usr/lib64/libpython2.6.so.1.0 (0x00002af03aec7000)
     libpthread.so.0 => /lib64/libpthread.so.0 (0x00002af03b270000)
     libdl.so.2 => /lib64/libdl.so.2 (0x00002af03b48c000)
     libutil.so.1 => /lib64/libutil.so.1 (0x00002af03b690000)
     libm.so.6 => /lib64/libm.so.6 (0x00002af03b893000)
     libc.so.6 => /lib64/libc.so.6 (0x00002af03bb17000)
     /lib64/ld-linux-x86-64.so.2 (0x00000031aba00000)
    

I have been trying re-installing and re-configuring python and mod_wsgi but to
no avail. Please let me know where I am going wrong. (Sorry for the very long
post)

**TLDR** ; System with non-root access has default python installation. I am
maintaining my own python and python modules. mod_wsgi configured and built
with the custom python, still points to the system's python when I run a test
script that prints out the sys version_info and path.

**UPDATE** : On Browsing through the stackoverflow (should have done it
earlier) I found this answer by Graham Dumpleton on [mod_wsgi python2.5 ubuntu
11.04 problem](http://stackoverflow.com/questions/6438260/mod-wsgi-
python2-5-ubuntu-11-04-problem) which solved the error for me. Now when I do
`ldd mod_wsgi.so` I see that it is linked to the correct shared library of
python. I now installed Django and MySQLdb using my custom python install. And
Now I am facing this error:

    
    
    The following error occurred while trying to extract file(s) to the Python egg
    cache:
    [Errno 13] Permission denied: '/var/www/.python-eggs'
    The Python egg cache directory is currently set to:
    /var/www/.python-eggs
    Perhaps your account does not have write access to this directory?  You can
    change the cache directory by setting the PYTHON_EGG_CACHE environment
    variable to point to an accessible directory.
    

So I did change the value of `PYTHON_EGG_CACHE` by doing `export
PYTHON_EGG_CACHE=/swadm/var/www/.python-eggs`. but I am still getting the same
error. I am investigating more. Will update when I solve this.


Answer: Egg cache issue solved by setting environment variable in WSGI script:

<http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Access_Rights_Of_Apache_User>

or in Apache configuration:

<http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIPythonEggs>
http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess

Which of latter two is used depends on whether using emebedded mode or daemon
mode.

