Django + Apache + Windows WSGIDaemonProcess Alternative
After setting up a django site and running on the dev server, I have finally gotten开发者_开发百科 around to figuring out deploying it in a production environment using the recommended mod_wsgi/apache22. I am currently limited to deploying this on a Windows XP machine.
My problem is that several django views I have written use the python subprocess
module to run programs on the filesystem. I keep getting errors when running the subprocess.Popen
I have seen several SO questions that have asked about this, and the accepted answer is to use WSGIDaemonProcess to handle the problem (due to permissions of the apache user, I believe).
The only problem with this is that WSGIDaemonProcess is not available for mod_wsgi on Windows. Is there any way that I can use mod_wsgi/apache/windows/subprocess together?
It's not a good idea to open subprocesses from within mod_wsgi, anyway.
An alternative (and a common one) is to use mod_proxy on the apache side and forward requests from apache to a WSGI server running Django. This has the advantage of moving the python thread(s) out of apache's memory space There are dozens of options for wsgi servers; tornado and gunicorn are two popular choices, and gunicorn integrates* with Django.
*by integrate I just mean it provides a manage.py command if you add it to INSTALLED_APPS.
I ran into a couple of issues trying to use subprocess under this configuration. Since I am not sure what specifically you had trouble with I can share a couple of things that were not easy for me to solve but in hindsight seem pretty trivial.
- I was receiving permissions related errors when trying to execute an application. I searched quite a bit but was having a hard time finding Windows specific answers. This one was obvious: I changed the user under which Apache runs to a user with higher permissions. (Note, there are security implications with that so you want to be sure you understand what you are getting in to).
- Django (depending on your configuration) may store strings as Unicode. I had a command line application I was trying to run with some parameters from my view which was crashing despite having the correct arguments passed in. After a couple hours of frustration I did a type(args) which returned
<type 'unicode'>
rather than my expected string. A quick conversion resolved that issue.
精彩评论