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.

21 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.

  6. Ben (@The_BenJones)

    Hi Shacker,

    First of all, thank you VERY much for this post. It would have taken me forever to figure out how to get mod_wsgi into the easy_apache build process. However, I’m still unable to get anything working. I’m a complete newb at actually deploying a django site, so perhaps you would have some insight I’m missing? Here’s the output I get at the site’s url:

    *list of files/directories at this index* and at the bottom:

    Apache/2.2.24 (Unix) mod_ssl/2.2.24 OpenSSL/0.9.8e-fips-rhel5 mod_wsgi/3.3 Python/2.4.3 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 PHP/5.3.24 Server at nameOfMySite.com Port 80

    The one thing that I’m noticing is that it says the Python version is 2.4.3. Although, when I invoke python in the command line when I’m connected through SSH it fires up the Python 2.7.4 shell. If the python version isn’t the problem, it must be my apache .conf include file that I’m screwing up, but I basically copied it directly from the Django documentation. Here’s what it looks like:

    WSGIScriptAlias / /home/sitefolder/public_html/djangoProjectName/wsgi.py
    WSGIPythonPath /home/sitefolder/public_html

    Order deny,allow
    Allow from all

    LogLevel info

    Can you see anything that I might be screwing up here?

  7. Ben (@The_BenJones)

    Whoops, the .conf file wasn’t pasted properly. It looks like this:

    WSGIScriptAlias / /home/ocrmusic/public_html/ocrmusicsubmit/wsgi.py
    WSGIPythonPath /home/ocrmusic/public_html

    Order deny,allow
    Allow from all

    LogLevel info

  8. Ben (@The_BenJones)

    Oh, ok, please scrap these last two comments. Your blog must block anything that looks like html/xml tags like the ones in Apache files. Let me try quotes:

    WSGIScriptAlias / /home/ocrmusic/public_html/ocrmusicsubmit/wsgi.py
    WSGIPythonPath /home/ocrmusic/public_html

    “”
    “”
    Order deny,allow
    Allow from all
    “”
    “”

    LogLevel info

  9. Ben (@The_BenJones)

    Ok, here’s the .conf file, but I’m using asterisks instead of greater than/less than signs.

    WSGIScriptAlias / /home/ocrmusic/public_html/ocrmusicsubmit/wsgi.py
    WSGIPythonPath /home/ocrmusic/public_html

    *Directory /home/ocrmusic/public_html/ocrmusicsubmit*
    *Files wsgi.py*
    Order deny,allow
    Allow from all
    */Files*
    */Directory*

    LogLevel info

  10. Ben (@The_BenJones)

    I’m sorry, I’m honestly not trying to spam you. I would rather not broadcast the name of the site to the world on this blog, so if you could only approve my first comment and maybe this one, that would be great. Here’s the .conf file, with asterisks instead of greater than/less than signs:

    WSGIScriptAlias / /home/siteFolder/public_html/djangoProjectName/wsgi.py
    WSGIPythonPath /home/siteFolder/public_html

    *Directory /home/siteFolder/public_html/djangoProjectName*
    *Files wsgi.py*
    Order deny,allow
    Allow from all
    */Files*
    */Directory*

    LogLevel info

  11. shacker

    Ben, it seems that your problem is that mod_wsgi is reporting the wrong python version. That has nothing to do with your apache configuration – it’s only related to how mod_wsgi was compiled – the wrong python was discovered during compilation. That would be related to the ld.so.conf.d section of the instructions above. Can you confirm that you did that part correctly?

  12. Ben (@The_BenJones)

    Hey Scot,

    I went through those steps again, but it doesn’t seem to have made a difference. Here’s the /etc…..conf.d folder:

    python27.conf qt-x86_64.conf storman.conf xulrunner-64.conf

    here’s /usr/local/lib:

    libpython2.7.so@ libpython2.7.so.1.0* mysql.sock@ php/ php.ini php.ini,v pkgconfig/ python2.7/

    I’m guessing the one with the asterisk next to it is a duplicated since I ran through that step a second time.

    Should I try it the way you originally had it before Spencer’s suggestion? And/or should i try the .bash_profile method you mentioned above?

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

  13. shacker Post author

    The asterisk just means it’s executable – that’s not actually part of the filename – just something that’s appended by ls.

    I assume you did run ldconfig afterwards..

    It really does sound like you’re doing everything correctly; not sure what to tell you at this point. I would post your question on the mod_wsgi google group:

    https://groups.google.com/forum/?fromgroups#!forum/modwsgi

    I personally haven’t tried building it for a couple of years – it’s possible something has changed in the meantime. If you figure it out, please post here!

  14. Silko Yuri

    hello
    I have same problem with easyapache
    I do of all by this instruction.
    -bash-3.2# python -V
    Python 2.7.3

    but easyapache says:

    — Begin step ‘Configuring mod_wsgi’ —
    checking Apache version… 2.2.24
    checking for python… /usr/bin/python
    configure: creating ./config.status
    config.status: creating Makefile
    — End step ‘Configuring mod_wsgi’ —

    — Begin step ‘Making mod_wsgi’ —
    /usr/local/apache/bin/apxs -c -I/usr/include/python2.4 -DNDEBUG -D_GNU_SOURCE -Wc,-fPIC mod_wsgi.c -L/usr/lib64 -L/usr/lib64/python2.4/config -lpython2.4 -lpthread -ldl -lutil -lm
    /usr/local/apache/build/libtool –silent –mode=compile gcc -prefer-pic -m64 -fPIC -DPIC -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -pthread -I/opt/pcre/include -I/usr/local/apache/include -I/usr/local/apache/include -I/usr/local/apache/include -fPIC -I/usr/include/python2.4 -DNDEBUG -D_GNU_SOURCE -c -o mod_wsgi.lo mod_wsgi.c && touch mod_wsgi.slo
    /usr/local/apache/build/libtool –silent –mode=link gcc -o mod_wsgi.la -rpath /usr/local/apache/modules -module -avoid-version mod_wsgi.lo -L/usr/lib64 -L/usr/lib64/python2.4/config -lpython2.4 -lpthread -ldl -lutil -lm
    — End step ‘Making mod_wsgi’ —

    on older server it is ok, but on new server is not ok (

  15. shacker Post author

    Silko, maybe I’m not reading your comment correctly but as far as I can tell you haven’t given us any hint whatsoever what kind of problem you’re having – you’re just showing everything working fine on your older server.

  16. Yuri Silko

    Hello again.
    I check this on old server and on them same situation. May be new version cpanel/whm overwrite config files. :( I compiled mod_wsgi manually and overwrite mod_wsgi.so file, what was be compliled automatically.

  17. James Acres

    Hi All,

    I also had the same issue with the mod_wsgi EasyApache module now picking up the wrong version of python on a rebuild, I have a solution!

    Edit /var/cpanel/easy/apache/custom_opt_mods/Cpanel/Easy/ModWSGI.pm

    Modify the configure command on line 26 to add the following argument:
    –with-python=/usr/local/bin/python

    so it is now:
    qw(./configure –with-apxs=/usr/local/apache/bin/apxs –with-python=/usr/local/bin/python)

    This forces the module to use the python at /usr/local/bin/python rather than /usr/bin/python.

    I hope this helps! I rebuilt my WHM server only to find mod_wsgi was now compiled with the wrong version, and now it all works fine after this fix, I feel your pain!

    Cheers,
    James.

Leave a Reply