Building mod_wsgi with EasyApache for WHM/cPanel

Note: These instructions are for root owners of WHM/cPanel systems, not end users.

If you want to run Django sites on a cPanel server, you’ll probably want to use the mod_wsgi Apache module. There are plenty of instructions out there on compiling mod_wsgi, but if you create it outside of the cPanel system, mod_wsgi.so will vanish each time you run easy_apache to upgrade your apache and php.

The key is to install this mod_wsgi for cPanel module. But before you go there, you’re going to want a more recent version of Python installed, since RedHat and CentOS still ship with Python 2.4, which will be deprecated by Django soon. However, you can’t overwrite the system-provided Python because yum and Mailman depend on it.

Download Python 2.7 (or whatever the latest is) into /usr/local/src. It’s critical that you build Python with shared libraries enabled, since mod_wsgi will be wanting to use them. So unpack the Python archive and cd into it, then:

./configure --enable-shared
make install

You’ll get a new build of python in /usr/local/bin, without disrupting the native version in /usr/bin. Any user wanting python2.7 to be their default can add this to their .bash_profile:

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

You’ll also get new libpython shared objects in /usr/local/lib. When you go to build mod_wsgi, easy_apache will need to look for python libs in that location. I found that copying the libs into standard library locations such as /lib and /usr/lib as suggested here didn’t do the trick. What did work was to add a system configuration file pointing to the new libs. Do this:

cd /etc/ld.so.conf.d
echo "/usr/local/lib/" > python27.conf
ldconfig

Now you’re ready to build mod_wsgi through easy_apache. Download custom_opt_mod-mod_wsgi.tar.gz from this ticket at google code and run:

tar -C /var/cpanel/easy/apache/custom_opt_mods -xzf custom_opt_mod-mod_wsgi.tar.gz

That unpacks the module into the right location so that easy_apache will find it and present it as a build option. Run easy_apache as usual (either via script or through WHM) and select the mod_wsgi option. When complete, you’ll find mod_wsgi.so along with all your other modules in /usr/local/apache/modules. The best part is, this will now become part of the default easy_apache build process, so Django sites won’t break when you rebuild apache+php in the future.

Many thanks to challgren for creating the module and to Graham Dumpleton for all of his mod_wsgi evangelism and support.

8 thoughts on “Building mod_wsgi with EasyApache for WHM/cPanel

  1. Martin Emerson

    Thanks for the post. I wanted to get up and running with Python 2.7 and Django on my cPanel managed VPS and was really stuck. Your post got me a bit farther but unfortunately I’m still stuck.

    I followed your instructions exactly but still ended up with the apparently infamous error:

    python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

    I tried the config file approach outlined above and also tried copying the library files to the more standard locations just to see what happened, nothing worked. I was wondering if you had any other possible suggestions.

  2. shacker Post author

    Martin, I’m not sure what to think. The config file did the trick for me very nicely. I found the devs in the comments on the ticket linked above to be really helpful. I suggest posting your question there, and mention this article. I’m curious to hear what they have to say.

  3. spencer

    Some people may need to change the following line:
    echo “/usr/local/lib/libpython2.7.so” > python27.conf

    to:
    echo “/usr/local/lib/” > python27.conf
    ldconfig may be expecting a path to a directory in the config file rather than a path to a file.

  4. Ked

    I had the same issue as Martin, Spencer’s suggestion fixed it.

    I simply edited /etc/ld.so.conf.d/python27.conf and changed it to:

    /usr/local/lib/

    Then reran ldconfig. EasyApache then compiled in wsgi support :)

    Thanks for a great HowTo and thanks to Spencer for the fix!

  5. jason

    I followed your instructions and it compiled well. I tried to run a hello world py script and it gives a 500 error.