Posted Thursday, 04 March 2010 at 03:36 by Andrew Liu
Tagged: python | web applications | linux | fedora | web development
Read more blogs...
OS: Fedora 10
Ideally, we want to run WSGI applications under our Apache web server, rather than on standalone ports. We do so by using mod_wsgi, the Apache module developed by Graham Dumpleto. This tutorial is largely based on components from this post by Albert Valverde. Our WSGI application we are trying to run is a Pylons installations.
Firstly, we need to grab the mod_wsgi module. We can grab it from the SVN repository. If you dont have SVN installed, you can grab a default installation of it.
|
> yum install svn Loaded plugins: refresh-packagekit Complete! |
Try to compile the source code.
|
> cd modwsgi > cd mod_wsgi > ./configure checking for apxs2... no
|
More often than not, it will complain that you don't have "apxs" installed. We will need the source code for apache for this. We can grab the httpd-devel package, then finish off the standard "./configure; make; make install" routine to build the module.
|
> yum install httpd-devel Loaded plugins: refresh-packagekit Complete! > make ... > make install /usr/sbin/apxs -i -S LIBEXECDIR=/usr/lib/httpd/modules -n 'mod_wsgi' mod_wsgi.la |
The reason I didnt bother with a --prefix option here is that I know I will only ever have one instance of httpd on my server, so I might as well install the modules in the "standard" place. I doubt I will be ever using multiple mod_wsgi modules (will I? I hope not!). This is one of those modules that I'd rather just let run, install, and know that my box has it.
Once installed, we can change the "httpd.conf" file (the configuration file for apache) to add the new module. I normally stick it in the "LoadModule" section, so I dont get confused in the future. Keeping all the LoadModule bits in one area is a bit tidier.
|
> vi /etc/httpd/conf/httpd.conf ... 197 LoadModule mem_cache_module modules/mod_mem_cache.so |
Restart httpd.
|
> /etc/rc.d/init.d/httpd restart |
If you get the following error:
| Cannot load /etc/httpd/modules/mod_wsgi.so into server: \ /etc/httpd/modules/mod_wsgi.so: cannot restore segment prot after reloc: \ Permission denied |
Then most likely you have SELinux enabled. SELinux is a security feature of Linux, and to be honest, everytime I've encountered it, I've just turned it off. It just stops everything and anything from working. A bit like Norton, or Windows Vista security alerts. Damn annoying. Turn it off.
|
> vi /etc/selinux/config 1 # This file controls the state of SELinux on the system. |
You may also need to double check your iptables configuration. Again, I tend to turn off iptables as I have a firewall/router in front of my servers that handles firewalling.
|
> chkconfig --list iptables
> chkconfig iptables off > chkconfig --list iptables |
While we're here, might as well do the same to ip6tables too.
|
> chkconfig --list ip6tables
> chkconfig ip6tables off > chkconfig --list ip6tables |
You'll need to restart your system if you turn off SELinux, for that to take effect. Now you should be able to restart httpd without any dramas.
|
> /etc/rc.d/init.d/httpd restart Stopping httpd: [FAILED] |
With the module loaded, we need to hook up apache and your WSGI application so they can talk.
Your WSGI application (in my case Pylons) runs fine by itself on its own specified port. But for it to run under mod_wsgi, it needs almost like a "command" for mod_wsgi to run. Assuming your Pylons installations is located at "/apps/myapp", lets create a new file here called "myapp.wsgi". This will be the entry point for mod_wsgi.
|
> vi /apps/myapp/myapp.wsgi 1 #!/usr/bin/env python |
This seems to do the trick for me. I'm not exactly sure of the meanings of all this, but I'll try to explain.
a) The import site / addsitedir section emulates the virtual environment that we are using. Effectively, this loads our application specific site-packages. A bit like our "source activate" command if we were running Pylons as a standalone server.
b) stdout / stderr just dumps all errors into standard out. Hopefully we'll pick these up in /etc/httpd/logs/error_log (or equivalent).
c) sys.path.append is a bit like a "cd /apps/myapp/myapp". Everything will be running from this directory.
d) os.environ seems to require a python egg cache to extract things to. It appears to be a temporary directory. So we need to create this - I just created it with permissions 777 (ick) and left it empty. That seems to do the trick.
e) application object is then created.
One last thing I needed to do (which you should do here as well) is configure the debugging properties of your Pylons application. Pylons complains that because Apache runs using multi-processes, its ErrorMiddleware doesn't work properly. Personally, I'm not a big fan of the interactive debugger, and while it does show me a lot of useful things, you can more often than not access variable values anyway (if the error can be reproduced of course). Anyway, edit your "development.ini" file as follows:
|
> vi /apps/myapp/myapp/development.ini ... 17 |
The full_stack option means that the interactive debugger will show you the value of all variables at all points in the stacktrace of an error - is that really necessary? To me, no.
Lastly, we need a final apache configuration to point to this WSGI application, in particular the "myapp.wsgi" file we created in the previous step. I put this into "/etc/httpd/conf.d/wsgi.conf".
|
> vi /etc/httpd/conf.d/wsgi.conf 1 WSGISocketPrefix /var/run/httpd |
The first line tells the WSGI module to place sockets into the /var/run/httpd directory. If you do not have this line, it seems to put the sockets into a directory where it cannot access, causing the following error in your /etc/httpd/logs/error_log:
| (13)Permission denied: mod_wsgi (pid=26962): Unable to connect to WSGI \ daemon process '<process-name>' on '/etc/httpd/logs/wsgi.26957.0.1.sock' \ after multiple attempts. |
The WSGIDaemonProcess sets a few configuration parameters for this application.
The WSGIScriptAlias tells apache that any file that is requested within the directory "/myapp" is to be served by the WSGI application, and it points to the entry point of that WSGI application - the "myapp.wsgi" file we created in the previous step.
The <Directory> directive allows apache to read from the directory that holds the WSGI application.
Restart apache, and all should be fine! Be sure to check your error logs (/etc/httpd/logs/error_log) in case something is amiss.
|
> /etc/rc.d/init.d/httpd restart Stopping httpd: [ OK ] |
And of course, browse to the relevant location to see if your Pylons application appears! Good luck!
Posted Thursday, 18 March 2010 at 20:51 by Andrew Liu
Posted Monday, 08 March 2010
Updated Tuesday, 09 March 2010 at 02:09 by Andrew Liu
Posted Friday, 05 March 2010 at 23:13 by Andrew Liu
Posted Thursday, 04 March 2010 at 04:34 by Andrew Liu
Posted Wednesday, 03 March 2010 at 20:15 by Andrew Liu