Wednesday, August 19, 2009

mod_wsgi on OpenBSD

Ever since I started learning about OpenBSD a few years ago, I always tried to find easier ways of getting Python to play with the version of Apache that comes with OpenBSD out of the box. Mod_Python was a nice try but it doesn't work with Apache 1.3. So I've been settling with FastCGI for the time being. Works ok and seems to have some advantages but it can be difficult to get going, especially in a chrooted environment. Well, while browsing the django docs I noticed that the preferred setup switched from mod_python to mod_wsgi. This was a nice surprise since mod_wsgi works with Apache 1.3 and 2.0+ (if I'm reading their docs right). To top it off, I found a port entry by James Turner that looked good. It was a couple versions old but still good to try. I threw the port into an OpenBSD test environment and it installed without issues. But that's where the fun ended.

After I installed it and tried to start the Apache server, the errors started to flow. Kinda disappointing but in mod_wsgi's defense, it's difficult to account for everything type of apache setup when creating a mod. I dug around a bit and found that the issue is with the chrooting and the pthread library. In an earlier post by James Turner pointed this out and two possible ways of getting around this issue. One is by adding a line in the httpd configure file to include the pthread library during the build and the other is by using the LD_PRELOAD variable. I recently tried to do the first item but during the build process, I received building errors. I'm thinking that the error occurred due to my setup but I haven't had a chance to setup another test system since then. Option two works to a point. It's alot of text to type and if the system reboots, you'll see errors during the boot up process. You can make this easier by editing the rc and apachectl files to include the LD_PRELOAD before the command. Even this makes the upgrade process more complicated because you have to account for the version number of the pthread library. These modifications aren't the most ideal but they are not impossible to do.

All this aside, this hasn't been imported into the OpenBSD port system. Although I am not sure of the official reason behind this, I'm thinking that these problems do not help. Until it is and this little quark is fixed, I would suggest sticking with the fcgi route for Django on OpenBSD. If you are daring enough, you can try yourself, at your own risk of course.


  1. More recent versions of mod_wsgi will automatically link to any special system libraries that Python originally had a dependency on when it was built. Thus, if Python configuration is correct, I would anticipate that you shouldn't have this issue with latest version of mod_wsgi and it should automatically drag in pthread library by itself. You might give building from source code a try if you have developer variants of Apache and Python available, plus a compiler.

  2. @Graham
    In theory, you would be correct. But in my tests, I tried both versions 2.5 and 3.0c4 and they both failed the same way. I believe that the problem lies with the apache chroot process that the OpenBSD team setup with the system. It may be preventing mod_wsgi from correctly locating the library in the chroot environment.
    On the otherhand, I didn't try it with the apache2 port. Since it is not chrooted by default, that should work just fine. As soon as I start testing again I'll post more findings.

  3. Whether it uses pthread library will actually depend on what is listed in the SYSLIBS variable in the lib/pythonX.Y/config/Makefile file. If for some reason it isn't listed in there then it would have a problem.

  4. That may be it then. I checked the source and at first glance, I don't see any references to pthread in the SYSLIBS variable but I feel that I need to dig deeper before I get a grasp on it. If it is, that may be a better route than recompiling httpd...