<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>schmichael&#039;s blog &#187; mod_python</title>
	<atom:link href="http://blog.schmichael.com/tag/mod_python/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.schmichael.com</link>
	<description>good good study, day day up</description>
	<lastBuildDate>Sat, 05 Nov 2011 23:13:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Fun with Django and modwsgi</title>
		<link>http://blog.schmichael.com/2008/02/05/fun-with-django-and-modwsgi/</link>
		<comments>http://blog.schmichael.com/2008/02/05/fun-with-django-and-modwsgi/#comments</comments>
		<pubDate>Wed, 06 Feb 2008 05:12:56 +0000</pubDate>
		<dc:creator>Michael Schurter</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[cherrypy]]></category>
		<category><![CDATA[diy]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[dreamhost]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[modwsgi]]></category>
		<category><![CDATA[mod_python]]></category>
		<category><![CDATA[wsgi]]></category>

		<guid isPermaLink="false">http://michael.susens-schurter.com/blog/2008/02/05/fun-with-django-and-modwsgi/</guid>
		<description><![CDATA[Today I deployed my first Django application for a client. Its yet-another-blog, so I&#8217;ll refrain from posting the code and cluttering up the django-*blog* namespace on Google Code. Before you roll your eyes and complain about why I didn&#8217;t use &#8230; <a href="http://blog.schmichael.com/2008/02/05/fun-with-django-and-modwsgi/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today I deployed my first <a href="http://www.djangoproject.com/">Django</a> application for a client.  Its yet-another-blog, so I&#8217;ll refrain from posting the code and cluttering up the django-*blog* namespace on Google Code.  Before you roll your eyes and complain about why I didn&#8217;t use an existing solution, I think I have 2 somewhat valid reasons:</p>
<ol>
<li>The client actually needed a <em>sub</em>set of the features most blogs offer, so I wouldn&#8217;t really have anything to contribute back to an existing project.</li>
<li>Blogs are one of the simplest content driven web applications in existence.  Wikis are just a bit simpler perhaps.  At any rate, creating a blog app is an excellent way to learn a framework.</li>
</ol>
<p><strong>Python Deployment Decisions</strong></p>
<p>In the past I&#8217;ve used <a href="http://michael.susens-schurter.com/blog/2007/08/16/deploying-cherrypy-on-apache-2-using-mod_proxy/">CherryPy as my framework and a simple mod_proxy configuration to run the applications behind Apache</a>.  <a href="http://www.djangoproject.com/documentation/django-admin/#runserver-optional-port-number-or-ipaddr-port">Django considers its built-in web server a development tool only</a>, so I figured it was time to explore the myriad of Python web app deployment alternatives: <a href="http://www.modpython.org">mod_python</a>, <a href="http://www.fastcgi.com/">FastCGI</a>, <a href="http://www.modwsgi.org">modwsgi</a>.  I&#8217;m sure there are many more, but I&#8217;d say those are the big 3.</p>
<p>I had tried to deploy Python web applications on DreamHost using FastCGI before and entered <a href="http://www.al3x.net/2008/01/shared-hosting-is-ghetto.html">the hell that is deploying Python web apps on shared hosts</a>.  <em>So FastCGI wasn&#8217;t my first choice this time.</em></p>
<p>I had also tried mod_python for deploying <a href="http://www.cherrypy.org/">CherryPy</a> apps on my <a href="http://www.linode.com/">Linode</a> before and for whatever reason just found <a href="http://httpd.apache.org/docs/2.2/mod/mod_proxy.html">mod_proxy</a> to be <em>much</em> easier to setup and manage.</p>
<p>I was kind of eager to try out modwsgi because its been getting a lot of attention lately, so I downloaded the source and compiled it on my <a href="http://www.debian.org/">Debian</a> <a href="http://www.debian.org/releases/etch/">Etch</a> server.</p>
<p><strong>Deploying a Django App via modwsgi</strong></p>
<p>modwsgi was quite easy to setup as long as you follow the instructions in their wiki for Django integration.  I was hit by <a href="http://code.djangoproject.com/ticket/3762">bug #3762</a>, but the modwsgi documentation got me through it.  <small>(For what its worth the attached wsgi.patch also worked, but I don&#8217;t really want to run a patched version of Django.)</small></p>
<p>One big problem I ran into was <a href="http://docs.python.org/lib/module-sqlite3.html">sqlite3</a> gave me <code>OperationalError: unable to open database file</code> whenever I did anything that would write to the database.  My database file was owned by <code>www-data</code> (the Apache process owner) and had the permissions <code>664</code>.</p>
<p>I switched to <a href="http://www.postgresql.org/">PostgreSQL</a>, ran <a href="http://www.djangoproject.com/documentation/django-admin/#syncdb"><code>syncdb</code></a>, and everything worked beautifully.</p>
<p>My wsgi script file /srv/spam/eggs/eggs.wsgi:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>
<span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/srv/spam'</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/srv/spam/eggs'</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'DJANGO_SETTINGS_MODULE'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'eggs.wsgi_settings'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>
&nbsp;
_application = django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>.<span style="color: black;">WSGIHandler</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> application<span style="color: black;">&#40;</span>environ, start_response<span style="color: black;">&#41;</span>:
    environ<span style="color: black;">&#91;</span><span style="color: #483d8b;">'PATH_INFO'</span><span style="color: black;">&#93;</span> = environ<span style="color: black;">&#91;</span><span style="color: #483d8b;">'SCRIPT_NAME'</span><span style="color: black;">&#93;</span> + environ<span style="color: black;">&#91;</span><span style="color: #483d8b;">'PATH_INFO'</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> _application<span style="color: black;">&#40;</span>environ, start_response<span style="color: black;">&#41;</span></pre></div></div>

<p>Note I use <code>wsgi_settings</code> instead of my usual settings file.  wsgi_settings just imports my main settings file and changes some to their production values.</p>
<p>My Django application actually drops into the <code>/blog/</code> and <code>/accounts/</code> folders under a VirtualHost otherwise occupied by static files and some PHP scripts.  modwsgi made this easy by putting this in my existing VirtualHost:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">WSGIScriptAliasMatch /(blog|accounts)/.* /srv/spam/eggs/eggs.wsgi
&nbsp;
<span style="color: #adadad; font-style: italic;"># A simple Alias directive handles my static files</span>
<span style="color: #00007f;">Alias</span> /static/ /srv/spam/eggs/static/</pre></div></div>

<p><strong>Bottom Line</strong></p>
<p>I highly recommend using modwsgi for deploying Python web applications.  sqlite3 may work for you.  In my case its probably best I use PostgreSQL for a number of reasons.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schmichael.com/2008/02/05/fun-with-django-and-modwsgi/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

