<?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>Fighting Rabbits</title>
	<atom:link href="http://fightingrabbits.com/feed" rel="self" type="application/rss+xml" />
	<link>http://fightingrabbits.com</link>
	<description>and herding cats...</description>
	<lastBuildDate>Wed, 23 Jun 2010 12:28:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Please Support Them</title>
		<link>http://fightingrabbits.com/archives/322</link>
		<comments>http://fightingrabbits.com/archives/322#comments</comments>
		<pubDate>Wed, 23 Jun 2010 12:28:04 +0000</pubDate>
		<dc:creator>Richard Cooper</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://fightingrabbits.com/?p=322</guid>
		<description><![CDATA[I feel that I have I winged a bit today. I don’t want to sound like a moaner, I am just trying to get a cross that this really is a challenge in so many ways. In fact, the cycling is probably the easy bit. This has been far tougher than I had imagined.

I do realise that what I am going through is nothing compared to what someone fighting cancer goes through. And maybe that is just another reason why today has been emotional. Realising, why I am here and why I am doing this.]]></description>
			<content:encoded><![CDATA[<p>A couple of friends of mine are cycling across America &#8211; 3500 miles in  30 Days! &#8211; to raise money for 3 Cancer charities &#8211; Please help them&#8230;</p>
<blockquote><p>I feel that I have I winged a bit today. I don’t want to sound like a moaner, I am just trying to get a cross that this really is a challenge in so many ways. In fact, the cycling is probably the easy bit. This has been far tougher than I had imagined.</p>
<p>I do realise that what I am going through is nothing compared to what someone fighting cancer goes through. And maybe that is just another reason why today has been emotional. Realising, why I am here and why I am doing this.</p></blockquote>
<p><a href="http://www.cycleacrossamerica2010.com/blog">CycleAcrossAmerica2010.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fightingrabbits.com/archives/322/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lightweight Django Thumbnails</title>
		<link>http://fightingrabbits.com/archives/264</link>
		<comments>http://fightingrabbits.com/archives/264#comments</comments>
		<pubDate>Thu, 18 Feb 2010 20:07:53 +0000</pubDate>
		<dc:creator>Richard Cooper</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://fightingrabbits.com/?p=264</guid>
		<description><![CDATA[I needed a good thumbnail solution for a recent Django project but didn't want the overhead of using sorl - I just needed a simple template tag that would let me generate the correct size of thumbnail from the main picture.]]></description>
			<content:encoded><![CDATA[<p>I needed a good thumbnail solution for a recent Django project but didn&#8217;t want the overhead of using sorl &#8211; I just needed a simple template tag that would let me generate the correct size of thumbnail from the main picture.</p>
<p>I found a solution on <a href="http://www.djangosnippets.org/">djangosnippets.org</a> but it needed a little tweeking to suit my needs.</p>
<p>The filter is applied directly to an image field using a template tag such as</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">{{ object.image|make_thumbnail:'100x200' }}</pre></div></div>

<p>and supposing the image filename is &#8220;image.jpg&#8221;, it checks if there is a file called &#8220;image_100x200.jpg&#8221;, if the file isn&#8217;t there, it resizes and saves the original image, finally it returns the proper url to the resized image.</p>
<p>The updated filter is given below (you will need PIL installed):<br />
<span id="more-264"></span></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: #ff7700;font-weight:bold;">import</span> Image
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">template</span> <span style="color: #ff7700;font-weight:bold;">import</span> Library, Node
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">template</span> <span style="color: #ff7700;font-weight:bold;">import</span> Variable, resolve_variable
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">core</span>.<span style="color: black;">urlresolvers</span> <span style="color: #ff7700;font-weight:bold;">import</span> reverse
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">contrib</span>.<span style="color: black;">contenttypes</span>.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> ContentType
&nbsp;
register = Library<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> make_thumbnail<span style="color: black;">&#40;</span><span style="color: #008000;">file</span>, size=<span style="color: #483d8b;">'200x200'</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    Example:
    &lt;img src=&quot;object.get_image_url&quot; alt=&quot;original image&quot; /&gt;
    &lt;img src=&quot;object.image|make_thumbnail&quot; alt=&quot;image resized to default 200x200 format&quot; /&gt;
    &lt;img src=&quot;object.image|make_thumbnail:'200x300'&quot; alt=&quot;image resized to 200x300&quot; /&gt;
&nbsp;
    The filter is applied to a image field (not the image url get from
    get_image_url method of the model), supposing the image filename is
    &quot;image.jpg&quot;, it checks if there is a file called &quot;image_200x200.jpg&quot; or
    &quot;image_200x300.jpg&quot; on the second case, if the file isn't there, it resizes
    the original image, finally it returns the proper url to the resized image.
    &quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">file</span>:
        <span style="color: #808080; font-style: italic;"># defining the size</span>
        x, y = <span style="color: black;">&#91;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> size.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'x'</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
        <span style="color: #808080; font-style: italic;"># defining the filename and the miniature filename</span>
        basename, format = <span style="color: #008000;">file</span>.<span style="color: black;">path</span>.<span style="color: black;">rsplit</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        baseurl, _format = <span style="color: #008000;">file</span>.<span style="color: black;">url</span>.<span style="color: black;">rsplit</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        miniature_filename = basename + <span style="color: #483d8b;">'_'</span> + size + <span style="color: #483d8b;">'.'</span> +  format
        miniature_url = baseurl + <span style="color: #483d8b;">'_'</span> + size + <span style="color: #483d8b;">'.'</span> +  format
&nbsp;
        <span style="color: #808080; font-style: italic;"># if the image wasn't already resized, resize it</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>miniature_filename<span style="color: black;">&#41;</span>:
            image = Image.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #008000;">file</span>.<span style="color: black;">path</span><span style="color: black;">&#41;</span>
            pw = image.<span style="color: black;">size</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
            ph = image.<span style="color: black;">size</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
            nw = x
            nh = y
            pr = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>pw<span style="color: black;">&#41;</span> / <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>ph<span style="color: black;">&#41;</span>
            nr = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>nw<span style="color: black;">&#41;</span> / <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>nh<span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">if</span> image.<span style="color: black;">mode</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">'L'</span>, <span style="color: #483d8b;">'RGB'</span><span style="color: black;">&#41;</span>:
                image = image.<span style="color: black;">convert</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'RGB'</span><span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">if</span> pw == nw <span style="color: #ff7700;font-weight:bold;">and</span> ph == nh:
                <span style="color: #ff7700;font-weight:bold;">pass</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                <span style="color: #ff7700;font-weight:bold;">if</span> pr <span style="color: #66cc66;">&gt;</span> nr:
                    <span style="color: #808080; font-style: italic;"># image aspect is wider than destination ratio</span>
                    tw = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">round</span><span style="color: black;">&#40;</span>nh <span style="color: #66cc66;">*</span> pr<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                    image = image.<span style="color: black;">resize</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>tw, nh<span style="color: black;">&#41;</span>, Image.<span style="color: black;">ANTIALIAS</span><span style="color: black;">&#41;</span>
                    l = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">round</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span> tw - nw <span style="color: black;">&#41;</span> / <span style="color: #ff4500;">2.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                    image = image.<span style="color: black;">crop</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>l, <span style="color: #ff4500;">0</span>, l + nw, nh<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">elif</span> pr <span style="color: #66cc66;">&lt;</span> nr:
                    <span style="color: #808080; font-style: italic;"># image aspect is taller than destination ratio</span>
                    th = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">round</span><span style="color: black;">&#40;</span>nw / pr<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                    image = image.<span style="color: black;">resize</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>nw, th<span style="color: black;">&#41;</span>, Image.<span style="color: black;">ANTIALIAS</span><span style="color: black;">&#41;</span>
                    t = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">round</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span> th - nh <span style="color: black;">&#41;</span> / <span style="color: #ff4500;">2.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                    image = image.<span style="color: black;">crop</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, t, nw, t + nh<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #808080; font-style: italic;"># image aspect matches the destination ratio</span>
                    image = image.<span style="color: black;">resize</span><span style="color: black;">&#40;</span>size, Image.<span style="color: black;">ANTIALIAS</span><span style="color: black;">&#41;</span>
            image.<span style="color: black;">save</span><span style="color: black;">&#40;</span>miniature_filename, image.<span style="color: black;">format</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> miniature_url
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">''</span>
&nbsp;
register.<span style="color: #008000;">filter</span><span style="color: black;">&#40;</span>make_thumbnail<span style="color: black;">&#41;</span></pre></div></div>

<p>Save it as something like thumbs_filter.py in a templatetag folder in one of your django apps and then use it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">{% load thumbs_filter %}
{{ object.image|make_thumbnail:'100x200' }}</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fightingrabbits.com/archives/264/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nginx &amp; Django on Webfaction &#8211; Part 3</title>
		<link>http://fightingrabbits.com/archives/210</link>
		<comments>http://fightingrabbits.com/archives/210#comments</comments>
		<pubDate>Fri, 30 Oct 2009 16:52:11 +0000</pubDate>
		<dc:creator>Richard Cooper</dc:creator>
				<category><![CDATA[Random]]></category>

		<guid isPermaLink="false">http://fightingrabbits.com/?p=210</guid>
		<description><![CDATA[Now that we have a stable and resilient environment (See Part 1a, Part 1b, and Part 2) I&#8217;m going to show you how we can hand off some of the work of file uploads to nginx and even get a nice upload progress bar into the bargain :) Take a deep breath as this is [...]]]></description>
			<content:encoded><![CDATA[<p>Now that we have a stable and resilient environment (See Part 1a, Part 1b, and Part 2) I&#8217;m going to show you how we can hand off some of the work of file uploads to nginx and even get a nice upload progress bar into the bargain :)<br />
Take a deep breath as this is quite a long one&#8230;. :)<br />
<span id="more-210"></span><br />
First of all we will need to configure our django application, and add some models and views to handle the uploads (I&#8217;ve given some basic examples below but you will want to roll your own!).<br />
(I&#8217;m assuming you already have a suitable project and app outline setup!)</p>
<p>models.py:</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;">datetime</span>
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">db</span> <span style="color: #ff7700;font-weight:bold;">import</span> models
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Upload<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Uploaded files.&quot;&quot;&quot;</span>
    <span style="color: #008000;">file</span> = models.<span style="color: black;">FileField</span><span style="color: black;">&#40;</span>upload_to=<span style="color: #483d8b;">'uploads'</span>,<span style="color: black;">&#41;</span>
    timestamp = models.<span style="color: black;">DateTimeField</span><span style="color: black;">&#40;</span>default=<span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#41;</span>
    notes = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">255</span>, blank=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">class</span> Meta:
        ordering = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'-timestamp'</span>,<span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__unicode__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> u<span style="color: #483d8b;">&quot;%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: #008000;">file</span><span style="color: black;">&#41;</span>
&nbsp;
    @<span style="color: #008000;">property</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> size<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> filesizeformat<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: #008000;">file</span>.<span style="color: black;">size</span><span style="color: black;">&#41;</span></pre></div></div>

<p>forms.py:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django <span style="color: #ff7700;font-weight:bold;">import</span> forms
<span style="color: #ff7700;font-weight:bold;">from</span> models <span style="color: #ff7700;font-weight:bold;">import</span> Upload
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> UploadForm<span style="color: black;">&#40;</span>forms.<span style="color: black;">models</span>.<span style="color: black;">ModelForm</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">class</span> Meta:
        model = Upload
        exclude = <span style="color: black;">&#40;</span><span style="color: #483d8b;">'timestamp'</span>,<span style="color: black;">&#41;</span></pre></div></div>

<p>views.py:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> forms <span style="color: #ff7700;font-weight:bold;">import</span> UploadForm
<span style="color: #ff7700;font-weight:bold;">from</span> models <span style="color: #ff7700;font-weight:bold;">import</span> Upload
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">datetime</span>
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">forms</span> <span style="color: #ff7700;font-weight:bold;">import</span> save_instance
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">shortcuts</span> <span style="color: #ff7700;font-weight:bold;">import</span> render_to_response
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">template</span> <span style="color: #ff7700;font-weight:bold;">import</span> RequestContext
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">http</span> <span style="color: #ff7700;font-weight:bold;">import</span> HttpResponseRedirect
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">core</span>.<span style="color: black;">urlresolvers</span> <span style="color: #ff7700;font-weight:bold;">import</span> reverse
&nbsp;
<span style="color: #808080; font-style: italic;">#Our initial page</span>
<span style="color: #ff7700;font-weight:bold;">def</span> initial<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:
	data = <span style="color: black;">&#123;</span>
		<span style="color: #483d8b;">'form'</span>: UploadForm<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,
	<span style="color: black;">&#125;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> render_to_response<span style="color: black;">&#40;</span><span style="color: #483d8b;">'upload.html'</span>, data, RequestContext<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">#Our file upload handler that the form will post to</span>
<span style="color: #ff7700;font-weight:bold;">def</span> upload<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> request.<span style="color: black;">method</span> == <span style="color: #483d8b;">'POST'</span>:
        form = UploadForm<span style="color: black;">&#40;</span>request.<span style="color: black;">POST</span>, request.<span style="color: black;">FILES</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> form.<span style="color: black;">is_valid</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            upload = Upload<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            upload.<span style="color: black;">timestamp</span> = <span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            save_instance<span style="color: black;">&#40;</span>form, upload<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponseRedirect<span style="color: black;">&#40;</span>reverse<span style="color: black;">&#40;</span><span style="color: #483d8b;">'initial'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>urls.py:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span>.<span style="color: black;">urls</span>.<span style="color: black;">defaults</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span> <span style="color: #ff7700;font-weight:bold;">import</span> settings
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">contrib</span> <span style="color: #ff7700;font-weight:bold;">import</span> admin
admin.<span style="color: black;">autodiscover</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
urlpatterns = patterns<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span>,
    <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^admin/'</span>, include<span style="color: black;">&#40;</span>admin.<span style="color: #dc143c;">site</span>.<span style="color: black;">urls</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
    <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^media/(?P&lt;path&gt;.*)$'</span>, <span style="color: #483d8b;">'django.views.static.serve'</span>, <span style="color: black;">&#123;</span><span style="color: #483d8b;">'document_root'</span>: settings.<span style="color: black;">MEDIA_ROOT</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#41;</span>
&nbsp;
urlpatterns += patterns<span style="color: black;">&#40;</span><span style="color: #483d8b;">'app.views'</span>,
    url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^$'</span>,<span style="color: #483d8b;">'initial'</span>, name=<span style="color: #483d8b;">'initial'</span><span style="color: black;">&#41;</span>,
    url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^upload/$'</span>,<span style="color: #483d8b;">'upload'</span>, name=<span style="color: #483d8b;">&quot;upload&quot;</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#41;</span></pre></div></div>

<p>Next we&#8217;ll add a couple of templates to display our upload form.<br />
base.html:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Upload Test<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;{{MEDIA_URL|default:&quot;</span><span style="color: #66cc66;">/</span><span style="color: #000066;">media</span><span style="color: #66cc66;">/</span><span style="color: #ff0000;">&quot;}}css/screen.css&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span> <span style="color: #000066;">charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    {% block main %}{% endblock %}
    {% block js %}{% endblock %}
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>upload.html:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">{% extends &quot;base.html&quot; %}
{% block main %}
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upload_form&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;{% url upload %}&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">enctype</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multipart/form-data&quot;</span> <span style="color: #000066;">accept-charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;steps&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h3</span>&gt;</span>Upload a new file<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h3</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;step&quot;</span>&gt;</span>1:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>{{form.file}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    {% if form.file.errors %}<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>{{form.file.errors}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>{% endif %}
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;step&quot;</span>&gt;</span>2:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>{{form.notes}}<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;instruction&quot;</span>&gt;</span> Add a note...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    {% if form.file.errors %}<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>{{form.notes.errors}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>{% endif %}
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;step&quot;</span>&gt;</span>3:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Upload&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
{% endblock %}</pre></div></div>

<p>OK now we have a working uploader with the advantage that our nginx server will cache the file until it is fully uploaded and then pass the uploaded file to our django instance locally. What happens if we are sending large files that may take some time to upload? (e.g. mp3 or video) &#8211; we need some feedback to show that the upload is progressing.</p>
<p>Let&#8217;s add some code to our nginx.conf file to use the <a href="http://wiki.nginx.org/NginxHttpUploadProgressModule">upload progress module</a> we compiled in part 1a. This module will track the total uploaded bytes for the upload and allow us to query the status and progress.</p>
<p>First we need to set up an upload &#8220;zone&#8221; and reserve some server memory for tracking uploads by using the following directive in the http section of our nginx.conf:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">upload_progress <span style="color: #000000; font-weight: bold;">&lt;</span>zone_name<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">&lt;</span>memory<span style="color: #000000; font-weight: bold;">&gt;</span>;</pre></div></div>

<p>next we need to tell nginx to track uploads to Django under this zone by adding the following directive to our django location:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">track_uploads <span style="color: #000000; font-weight: bold;">&lt;</span>zone_name<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">&lt;</span>timeout<span style="color: #000000; font-weight: bold;">&gt;</span>;</pre></div></div>

<p>and finally we need to add a new location to poll for our upload status:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">location ^~ <span style="color: #000000; font-weight: bold;">/</span>upload<span style="color: #000000; font-weight: bold;">/</span>progress <span style="color: #7a0874; font-weight: bold;">&#123;</span>
      report_uploads <span style="color: #000000; font-weight: bold;">&lt;</span>zone_name<span style="color: #000000; font-weight: bold;">&gt;</span>;
    <span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>for a zone called uploadtracker our nginx.conf should now look something like this (Note the client_max_body_size is now set to allow 50Mb uploads!):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">daemon off;
worker_processes <span style="color: #000000;">1</span>;
&nbsp;
events <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    worker_connections <span style="color: #000000;">1024</span>;
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
http <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    include             mime.types;
    default_type        application<span style="color: #000000; font-weight: bold;">/</span>octet-stream;	
    upload_progress     uploadtracker 1m;	
&nbsp;
    server <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        listen  <span style="color: #000000;">55615</span>;
        server_name <span style="color: #000000; font-weight: bold;">*</span>.mydomain.com;
        client_max_body_size 50m;
&nbsp;
        location ^~ <span style="color: #000000; font-weight: bold;">/</span>upload<span style="color: #000000; font-weight: bold;">/</span>progress <span style="color: #7a0874; font-weight: bold;">&#123;</span>
            report_uploads uploadtracker;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
        location <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
           <span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #000000; font-weight: bold;">/</span>upload<span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
        location ~<span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>favicon.ico <span style="color: #7a0874; font-weight: bold;">&#123;</span>
           root <span style="color: #000000; font-weight: bold;">/</span>upload<span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>img<span style="color: #000000; font-weight: bold;">/</span>;
           access_log off;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
        location <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
            proxy_set_header  	        X-Real-IP  <span style="color: #007800;">$remote_addr</span>;
            proxy_set_header            X-Forwarded-For <span style="color: #007800;">$proxy_add_x_forwarded_for</span>;
            fastcgi_pass                unix:<span style="color: #000000; font-weight: bold;">/</span>upload<span style="color: #000000; font-weight: bold;">/</span>django.sock;
            fastcgi_pass_header         Authorization;          
            fastcgi_hide_header         X-Accel-Redirect;
            fastcgi_hide_header         X-Sendfile;
            fastcgi_intercept_errors    off;
            include                     fastcgi_params;
            track_uploads uploadtracker 30s;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>You&#8217;ll need to restart nginx with ./sbin/nginx -s reload for it to pick up the changes or if you are using supervisor you can use supervisorctl restart all to restart the django and nginx instances.</p>
<p>Now we need to add some java-script (and html) to poll the server and update a progress bar on the client side. I&#8217;ll use MooTools and the excellent dwProgressBar from <a href="http://davidwalsh.name/progress-bar-animated-mootools">david walsh</a> to do the heavy lifting for this although it should be easy to re-write in the framework of your choice.</p>
<p>first lets change the upload.html template and add in some html and some css for the progress bar:<br />
upload.html:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">{% extends &quot;base.html&quot; %}
{% block main %}
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upload_form&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;{% url upload %}&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">enctype</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multipart/form-data&quot;</span> <span style="color: #000066;">accept-charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;steps&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h3</span>&gt;</span>Upload a new file<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h3</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;step&quot;</span>&gt;</span>1:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>{{form.file}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    {% if form.file.errors %}<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>{{form.file.errors}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>{% endif %}
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;step&quot;</span>&gt;</span>2:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>{{form.notes}}<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;instruction&quot;</span>&gt;</span> Add a note...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    {% if form.file.errors %}<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>{{form.notes.errors}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>{% endif %}
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;step&quot;</span>&gt;</span>3:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Upload&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;progress_container&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;progress_filename&quot;</span>&gt;</span>Please Choose a File...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;put-bar-here&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
{% endblock %}
{% block js %}
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;{{MEDIA_URL|default:&quot;</span><span style="color: #66cc66;">/</span><span style="color: #000066;">media</span><span style="color: #66cc66;">/</span><span style="color: #ff0000;">&quot;}}js/mootools-1.2.4-core.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;{{MEDIA_URL|default:&quot;</span><span style="color: #66cc66;">/</span><span style="color: #000066;">media</span><span style="color: #66cc66;">/</span><span style="color: #ff0000;">&quot;}}js/mootools-1.2.4.1-more.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;{{MEDIA_URL|default:&quot;</span><span style="color: #66cc66;">/</span><span style="color: #000066;">media</span><span style="color: #66cc66;">/</span><span style="color: #ff0000;">&quot;}}js/dwProgressBar.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
{% endblock %}</pre></div></div>

<p>screen.css:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* Progress Bar */</span>
<span style="color: #cc00cc;">#progress_container</span> <span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">.9em</span><span style="color: #00AA00;">;</span>width<span style="color: #00AA00;">:</span> <span style="color: #933;">400px</span><span style="color: #00AA00;">;</span>height<span style="color: #00AA00;">:</span> <span style="color: #933;">80px</span><span style="color: #00AA00;">;</span>margin<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>background-<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>padding<span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#box2</span> <span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">../img/progress-back.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #000000; font-weight: bold;">right</span> <span style="color: #993333;">center</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">400px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">18px</span><span style="color: #00AA00;">;</span>float<span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#perc2</span> <span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">../img/progress.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #000000; font-weight: bold;">right</span> <span style="color: #993333;">center</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">18px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#text</span> <span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #3333ff;">:tahoma</span><span style="color: #00AA00;">,</span> arial<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>font-<span style="color: #000000; font-weight: bold;">size</span><span style="color: #00AA00;">:</span><span style="color: #933;">11px</span><span style="color: #00AA00;">;</span>color<span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>float<span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>padding<span style="color: #00AA00;">:</span><span style="color: #933;">3px</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#progress_filename</span> <span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">.9em</span><span style="color: #00AA00;">;</span>width<span style="color: #00AA00;">:</span><span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>line-<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">1.2em</span><span style="color: #00AA00;">;</span>padding<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">10px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>color<span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Now we can add our javascript &#8211; first we will create our progress bar &#8211; add this after the last &lt;script&gt; tag in the upload.html template</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> charset<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;utf-8&quot;</span><span style="color: #339933;">&gt;</span>
pb2 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> dwProgressBar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    container<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'put-bar-here'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    startPercentage<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
    speed<span style="color: #339933;">:</span><span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span>
    boxID<span style="color: #339933;">:</span> <span style="color: #3366CC;">'box2'</span><span style="color: #339933;">,</span>
    percentageID<span style="color: #339933;">:</span> <span style="color: #3366CC;">'perc2'</span><span style="color: #339933;">,</span>
    displayID<span style="color: #339933;">:</span> <span style="color: #3366CC;">'text'</span><span style="color: #339933;">,</span>
    displayText<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Next we need to create a unique id for our upload:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> uuid <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">32</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    uuid <span style="color: #339933;">+=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">16</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The next bit is where it gets a little tricky we are going to use MooTools to create a request to our nginx progress location, we will use Request.Periodical to send a request every second. The nginx server will return a JSON response containing the status of the upload, along with some other data such as total bytes and uploaded bytes depending on the status.<br />
Note: for this to work in WebKit based browsers we need to set async=false on the request due to this bug <a href="https://bugs.webkit.org/show_bug.cgi?id=23933">https://bugs.webkit.org/show_bug.cgi?id=23933</a></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> req <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Request<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'get'</span><span style="color: #339933;">,</span>
    headers<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'X-Progress-ID'</span><span style="color: #339933;">:</span> uuid<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/upload/progress/'</span><span style="color: #339933;">,</span>
    initialDelay<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span>
    delay<span style="color: #339933;">:</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span>
    limit<span style="color: #339933;">:</span> <span style="color: #CC0000;">10000</span><span style="color: #339933;">,</span>
    async<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
    onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>reply<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        test <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">decode</span><span style="color: #009900;">&#40;</span>reply<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>test.<span style="color: #660066;">state</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;uploading&quot;</span><span style="color: #339933;">:</span> 
                percent <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.00</span> <span style="color: #339933;">+</span> parseFloat<span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>test.<span style="color: #660066;">received</span> <span style="color: #339933;">/</span> test.<span style="color: #660066;">size</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'progress_filename'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'html'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'Uploading '</span> <span style="color: #339933;">+</span> filename <span style="color: #339933;">+</span> <span style="color: #3366CC;">' ...'</span> <span style="color: #339933;">+</span> percent <span style="color: #339933;">+</span> <span style="color: #3366CC;">'%'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                pb2.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span>percent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;starting&quot;</span><span style="color: #339933;">:</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'progress_filename'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'html'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'Starting Upload... '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;error&quot;</span><span style="color: #339933;">:</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'progress_filename'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'html'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'Upload Error... '</span> <span style="color: #339933;">+</span> test.<span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;done&quot;</span><span style="color: #339933;">:</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'progress_filename'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'html'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'Upload Finished...'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                req.<span style="color: #660066;">stopTimer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span>
                console.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Oooops!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>  
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Finally we hook it all together by adding an onClick handler to the submit button to change the action on our form, get the filename, and start our periodical requests.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'submit'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    filename <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id_file&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'value'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[\/\\]/</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;progress_filename&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'html'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'Uploading '</span> <span style="color: #339933;">+</span> filename <span style="color: #339933;">+</span> <span style="color: #3366CC;">' ...'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;upload_form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'action'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;/upload/?X-Progress-ID=&quot;</span> <span style="color: #339933;">+</span> uuid<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    req.<span style="color: #660066;">startTimer</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'X-Progress-ID='</span> <span style="color: #339933;">+</span> uuid<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The final upload.html template looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">{% extends &quot;base.html&quot; %}
&nbsp;
{% block main %}
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upload_form&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;{% url upload %}&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">enctype</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multipart/form-data&quot;</span> <span style="color: #000066;">accept-charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;steps&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h3</span>&gt;</span>Upload a new file<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h3</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;step&quot;</span>&gt;</span>1:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>{{form.file}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    {% if form.file.errors %}<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>{{form.file.errors}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>{% endif %}
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;step&quot;</span>&gt;</span>2:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>{{form.notes}}<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;instruction&quot;</span>&gt;</span> Add a note...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    {% if form.file.errors %}<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>{{form.notes.errors}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>{% endif %}
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;step&quot;</span>&gt;</span>3:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Upload&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;progress_container&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;progress_filename&quot;</span>&gt;</span>Please Choose a File...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;put-bar-here&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
{% endblock %}
&nbsp;
{% block js %}
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;{{MEDIA_URL|default:&quot;</span><span style="color: #66cc66;">/</span><span style="color: #000066;">media</span><span style="color: #66cc66;">/</span><span style="color: #ff0000;">&quot;}}js/mootools-1.2.4-core.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;{{MEDIA_URL|default:&quot;</span><span style="color: #66cc66;">/</span><span style="color: #000066;">media</span><span style="color: #66cc66;">/</span><span style="color: #ff0000;">&quot;}}js/mootools-1.2.4.1-more.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;{{MEDIA_URL|default:&quot;</span><span style="color: #66cc66;">/</span><span style="color: #000066;">media</span><span style="color: #66cc66;">/</span><span style="color: #ff0000;">&quot;}}js/dwProgressBar.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span>&gt;</span>
    var uuid = &quot;&quot;
    pb2 = new dwProgressBar({
        container: $('put-bar-here'),
        startPercentage: 0,
        speed:1000,
        boxID: 'box2',
        percentageID: 'perc2',
        displayID: 'text',
        displayText: false
    });
&nbsp;
    for (i = 0; i <span style="color: #009900;">&lt; <span style="color: #cc66cc;">32</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">        uuid +<span style="color: #66cc66;">=</span> Math.floor<span style="color: #66cc66;">&#40;</span>Math.random<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> * <span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span>.toString<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">    <span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #009900;">    var req <span style="color: #66cc66;">=</span> new Request<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">method</span>: <span style="color: #ff0000;">'get'</span>,</span>
<span style="color: #009900;">        <span style="color: #000066;">headers</span>: <span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">'X-Progress-ID'</span>: uuid<span style="color: #66cc66;">&#125;</span>,</span>
<span style="color: #009900;">        url: <span style="color: #ff0000;">'/upload/progress/'</span>,</span>
<span style="color: #009900;">        initialDelay: <span style="color: #cc66cc;">500</span>,</span>
<span style="color: #009900;">        delay: <span style="color: #cc66cc;">1000</span>,</span>
<span style="color: #009900;">        limit: <span style="color: #cc66cc;">10000</span>,</span>
<span style="color: #009900;">        onSuccess: function<span style="color: #66cc66;">&#40;</span>reply<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">            test <span style="color: #66cc66;">=</span> JSON.decode<span style="color: #66cc66;">&#40;</span>reply<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">            switch<span style="color: #66cc66;">&#40;</span>test.state<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">                case <span style="color: #ff0000;">&quot;uploading&quot;</span>: </span>
<span style="color: #009900;">                    percent <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0.00</span> + parseFloat<span style="color: #66cc66;">&#40;</span>Math.floor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>test.received <span style="color: #66cc66;">/</span> test.<span style="color: #000066;">size</span><span style="color: #66cc66;">&#41;</span>*<span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">                    $<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'progress_filename'</span><span style="color: #66cc66;">&#41;</span>.set<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'html'</span>,<span style="color: #ff0000;">'Uploading '</span> + filename + <span style="color: #ff0000;">' ...'</span> + percent + <span style="color: #ff0000;">'%'</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">                    pb2.set<span style="color: #66cc66;">&#40;</span>percent<span style="color: #66cc66;">&#41;</span>; </span>
<span style="color: #009900;">                    break;</span>
<span style="color: #009900;">                case <span style="color: #ff0000;">&quot;starting&quot;</span>:</span>
<span style="color: #009900;">                    $<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'progress_filename'</span><span style="color: #66cc66;">&#41;</span>.set<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'html'</span>,<span style="color: #ff0000;">'Starting Upload... '</span><span style="color: #66cc66;">&#41;</span>; </span>
<span style="color: #009900;">                    break;</span>
<span style="color: #009900;">                case <span style="color: #ff0000;">&quot;error&quot;</span>:</span>
<span style="color: #009900;">                    $<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'progress_filename'</span><span style="color: #66cc66;">&#41;</span>.set<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'html'</span>,<span style="color: #ff0000;">'Upload Error... '</span> + test.status<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">                    break;</span>
<span style="color: #009900;">                case <span style="color: #ff0000;">&quot;done&quot;</span>:</span>
<span style="color: #009900;">                    $<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'progress_filename'</span><span style="color: #66cc66;">&#41;</span>.set<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'html'</span>,<span style="color: #ff0000;">'Upload Finished...'</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">                    req.stopTimer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">                    break;</span>
<span style="color: #009900;">                default:</span>
<span style="color: #009900;">                    console.debug<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Oooops!&quot;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">                    break;  </span>
<span style="color: #009900;">            <span style="color: #66cc66;">&#125;</span></span>
<span style="color: #009900;">        <span style="color: #66cc66;">&#125;</span>,</span>
<span style="color: #009900;">    <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></span>
&nbsp;
<span style="color: #009900;">    $<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'submit'</span><span style="color: #66cc66;">&#41;</span>.addEvent<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'click'</span>, function<span style="color: #66cc66;">&#40;</span>evt<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">        filename <span style="color: #66cc66;">=</span> $<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;id_file&quot;</span><span style="color: #66cc66;">&#41;</span>.get<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'value'</span><span style="color: #66cc66;">&#41;</span>.split<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">/</span><span style="color: #66cc66;">&#91;</span>\<span style="color: #66cc66;">/</span>\\<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">/</span><span style="color: #66cc66;">&#41;</span>.pop<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">        $<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;progress_filename&quot;</span><span style="color: #66cc66;">&#41;</span>.set<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'html'</span>,<span style="color: #ff0000;">'Uploading '</span> + filename + <span style="color: #ff0000;">' ...'</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">        $<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;upload_form&quot;</span><span style="color: #66cc66;">&#41;</span>.set<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'action'</span>, <span style="color: #ff0000;">&quot;{% url upload %}?X-Progress-ID=&quot;</span> + uuid<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">        req.startTimer<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'X-Progress-ID='</span> + uuid<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">	<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
{% endblock %}</pre></div></div>

<p>You can download all the completed files from here &#8211; <a href='http://fightingrabbits.com/wp-content/uploads/2009/10/ZipFile.zip'>ZipFile</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://fightingrabbits.com/archives/210/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Django on Snow Leopard</title>
		<link>http://fightingrabbits.com/archives/266</link>
		<comments>http://fightingrabbits.com/archives/266#comments</comments>
		<pubDate>Sun, 04 Oct 2009 10:24:15 +0000</pubDate>
		<dc:creator>Richard Cooper</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Setup]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[easy_install]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[snow leopard]]></category>
		<category><![CDATA[Software.]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://fightingrabbits.com/?p=266</guid>
		<description><![CDATA[Having just been given an new MacBook Pro as an early birthday present, it didn&#8217;t take me long to try and install Django on it. Rather than use the MacPorts solution I decided to use the native python 2.6 and build the rest, apart from MySQL (use the Mac OS X 10.5 (x86_64) package from [...]]]></description>
			<content:encoded><![CDATA[<p>Having just been given an new MacBook Pro as an early birthday present, it didn&#8217;t take me long to try and install Django on it. Rather than use the MacPorts solution I decided to use the native python 2.6 and build the rest, apart from MySQL (use the Mac OS X 10.5 (x86_64) package from <a href="http://dev.mysql.com/downloads/mysql/5.1.html#downloads">here</a>)</p>
<p>Before proceeding make sure that you have installed XCode (It&#8217;s on the Snow Leopard install disc) as we need it to install libjpeg and PIL (all of my Django projects use this!)</p>
<p>Download <a href="http://freshmeat.net/urls/6228c609a20bb6f87b2fef4fd4b6a30c">libjpeg</a> then:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xzf</span> jpegsrc-1.v7.tar
<span style="color: #7a0874; font-weight: bold;">cd</span> jpegsrc-1.v7
<span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> glibtool<span style="color: #000000; font-weight: bold;">`</span> .<span style="color: #000000; font-weight: bold;">/</span>libtool
<span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">env</span> MACOSX_DEPLOYMENT_TARGET <span style="color: #000000;">10.6</span>
.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--enable-shared</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Now we can download the <a href="http://effbot.org/media/downloads/Imaging-1.1.6.tar.gz">source</a> and build PIL</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xzf</span> Imaging-1.1.6.tar.gz
<span style="color: #7a0874; font-weight: bold;">cd</span> Imaging-1.1.6
<span style="color: #c20cb9; font-weight: bold;">sudo</span> python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Last thing to do before we install Django is to install the MySqlDb connector &#8211; you will need to do add the following to your .bash_profile</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">PATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${PATH}</span>:/usr/local/mysql/bin&quot;</span></pre></div></div>

<p>Then:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> easy_install mysql-python</pre></div></div>

<p>Now we can easy_install Django:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> easy_install django</pre></div></div>

<p>and we are all set to go :)</p>
<p>Thanks to these guys for the help in figuring this out:<br />
<a href="http://dryan.com/articles/snow-leopard-64bit-mamp/">http://dryan.com/</a><br />
<a href="http://www.brambraakman.com/blog/comments/installing_mysql_python_mysqldb_on_snow_leopard_mac_os_x_106/">http://www.brambraakman.com/blog/</a><br />
<a href="http://colbypalmer.com/index.php?/colbyworld/blogEntry/install_libjpeg_and_pil_on_os_x_leopard/">http://colbypalmer.com/</a><br />
<a href="http://www.agapow.net/programming/python/installing-mysqldb">http://www.agapow.net/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fightingrabbits.com/archives/266/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Mounting Supervisor on a WebFaction subdomain</title>
		<link>http://fightingrabbits.com/archives/258</link>
		<comments>http://fightingrabbits.com/archives/258#comments</comments>
		<pubDate>Tue, 29 Sep 2009 19:03:18 +0000</pubDate>
		<dc:creator>Richard Cooper</dc:creator>
				<category><![CDATA[Setup]]></category>
		<category><![CDATA[webfaction]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[hosting]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://fightingrabbits.com/?p=258</guid>
		<description><![CDATA[In a previous post I mentioned that you could mount your supervisor process on a sub domain so that you could view and manage your Django and Nginx processes from a web browser. In this post I&#8217;m going to show you how to do this. First of all I&#8217;m going to assume that you have [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous post I mentioned that you could mount your supervisor process on a sub domain so that you could view and manage your Django and Nginx processes from a web browser. In this post I&#8217;m going to show you how to do this.<br />
<span id="more-258"></span><br />
First of all I&#8217;m going to assume that you have a domain called mydomain.com setup on webfaction, and that your supervisor process is listening on an app called supervisor.</p>
<p>To access your supervisor status via a web browser you need to take the following steps:<br />
1) Make sure you have a suitable sub-domain setup &#8211; you do this from the domain menu on the webfaction control panel (I usually use status.mydomain.com) :<br />
<div id="attachment_261" class="wp-caption aligncenter" style="width: 310px"><a href="http://fightingrabbits.com/wp-content/uploads/2009/09/sub-domain1.PNG"><img src="http://fightingrabbits.com/wp-content/uploads/2009/09/sub-domain1-300x198.PNG" alt="Check your Domain" title="Check your Domain" width="300" height="198" class="size-medium wp-image-261" /></a><p class="wp-caption-text">Check your Domain</p></div><br />
2) Create a new website which links the status subdomain and your supervisor app :<br />
<div id="attachment_262" class="wp-caption aligncenter" style="width: 310px"><a href="http://fightingrabbits.com/wp-content/uploads/2009/09/sub-domain2.PNG"><img src="http://fightingrabbits.com/wp-content/uploads/2009/09/sub-domain2-300x225.PNG" alt="Create a new Website" title="Create a new Website" width="300" height="225" class="size-medium wp-image-262" /></a><p class="wp-caption-text">Create a new Website</p></div><br />
3) browse to http://status.mydomain.com/ and enter the password and username we set-up in the supervisor configuration &#8211; you did remember to do that didn&#8217;t you?  ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://fightingrabbits.com/archives/258/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using Django over HTTPS on Webfaction</title>
		<link>http://fightingrabbits.com/archives/255</link>
		<comments>http://fightingrabbits.com/archives/255#comments</comments>
		<pubDate>Fri, 25 Sep 2009 11:58:48 +0000</pubDate>
		<dc:creator>Richard Cooper</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://fightingrabbits.com/?p=255</guid>
		<description><![CDATA[Webfaction have recently made a change to set the X-Forwarded-SSL header from the main Apache instance for HTTPS connections, this means the using Apache &#038; mod_wsgi HTTPS now Just Works&#8482; However if you are running using the Supervisor + Nginx + Django setup given on this blog you will need to add the following middleware [...]]]></description>
			<content:encoded><![CDATA[<p>Webfaction have recently made a change to set the X-Forwarded-SSL header from the main Apache instance for HTTPS connections, this means the using Apache &#038; mod_wsgi HTTPS now Just Works&#8482; </p>
<p>However if you are running <span id="more-255"></span>using the Supervisor + Nginx + Django setup given on this blog you will need to add the following middleware (available from <a href="http://www.djangosnippets.org/snippets/1706/">here</a>) to your Django setup.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> WebFactionFixes<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    Middleware that applies some fixes for people using
    the WebFaction hosting provider.  In particular:
&nbsp;
    * sets 'REMOTE_ADDR' based on 'HTTP_X_FORWARDED_FOR', if the
      latter is set.
&nbsp;
    * Monkey patches request.is_secure() to respect HTTP_X_FORWARDED_SSL.
      PLEASE NOTE that this is not reliable, since a user could set
      X-Forwarded-SSL manually and the main WebFaction Apache instance
      does not remove it, so it will appear to be a secure request
      when it is not.  Usually if they do that, they will be harming
      only themselves, but it depends how you use request.is_secure().
    &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> process_request<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request<span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;"># Fix REMOTE_ADDR</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            real_ip = request.<span style="color: black;">META</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'HTTP_X_FORWARDED_FOR'</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">KeyError</span>:
            <span style="color: #ff7700;font-weight:bold;">pass</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #808080; font-style: italic;"># HTTP_X_FORWARDED_FOR can be a comma-separated list of IPs. The</span>
            <span style="color: #808080; font-style: italic;"># client's IP will be the first one.</span>
            real_ip = real_ip.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;,&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            request.<span style="color: black;">META</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'REMOTE_ADDR'</span><span style="color: black;">&#93;</span> = real_ip
&nbsp;
        <span style="color: #808080; font-style: italic;"># Fix HTTPS</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'HTTP_X_FORWARDED_SSL'</span> <span style="color: #ff7700;font-weight:bold;">in</span> request.<span style="color: black;">META</span>:
            request.<span style="color: black;">is_secure</span> = <span style="color: #ff7700;font-weight:bold;">lambda</span>: request.<span style="color: black;">META</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'HTTP_X_FORWARDED_SSL'</span><span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'on'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fightingrabbits.com/archives/255/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nginx &amp; Django on Webfaction &#8211; Part 2</title>
		<link>http://fightingrabbits.com/archives/208</link>
		<comments>http://fightingrabbits.com/archives/208#comments</comments>
		<pubDate>Mon, 21 Sep 2009 19:18:00 +0000</pubDate>
		<dc:creator>Richard Cooper</dc:creator>
				<category><![CDATA[Random]]></category>

		<guid isPermaLink="false">http://fightingrabbits.com/?p=208</guid>
		<description><![CDATA[Hopefully after finishing Part 1a and Part 1b you have a working Nginx + Django install on Webfaction. In this installment I&#8217;m going to show you how to keep your site running through app failures and server reboots. One of the problems with having our own web stack  running is what happens if the Webfaction [...]]]></description>
			<content:encoded><![CDATA[<p>Hopefully after finishing <a href="http://fightingrabbits.com/archives/139">Part 1a</a> and <a href="http://fightingrabbits.com/archives/160">Part 1b</a> you have a working Nginx + Django install on Webfaction. In this installment I&#8217;m going to show you how to keep your site running through app failures and server reboots.<br />
<span id="more-208"></span><br />
One of the problems with having our own web stack  running is what happens if the Webfaction support team ever need to reboot the server or if our Django App crashes? We could solve this by adding a cron job to restart our stack every few minutes but because Nginx doesn&#8217;t spawn our FCGI Django process it gets quite complex, and we have no easy way to monitor the status.</p>
<p>The solution as suggested <a href="http://forum.webfaction.com/viewtopic.php?id=1981&#038;p=2">here</a>  is to use <a href="http://supervisord.org/">supervisor</a> to control our server processes and then make sure that we have a cron job to check and restart supervisor.</p>
<p><strong>Please don&#8217;t try this on a production site if you don&#8217;t know what you are doing unless you can afford some down time whilst you get it working!</strong></p>
<p>First we need to create a new app listening on port to reserve ourselves a port for supervisor to run on:<br />
<a href="http://fightingrabbits.com/wp-content/uploads/2009/08/create_app.png"><img src="http://fightingrabbits.com/wp-content/uploads/2009/08/create_app-300x176.png" alt="Create A new App" title="Create A new App" width="300" height="176" class="aligncenter size-medium wp-image-142" /></a><br />
And make a note of the port number for later:<br />
<a href="http://fightingrabbits.com/wp-content/uploads/2009/08/app_created.png"><img src="http://fightingrabbits.com/wp-content/uploads/2009/08/app_created-300x199.png" alt="Make a note of the Port Number" title="Make a note of the Port Number" width="300" height="199" class="aligncenter size-medium wp-image-141" /></a><br />
First we need to create a start script for Django so that supervisor can monitor it, use your favourite editor to create a file called runserver.py in your django project directory (/home/&lt;myuser&gt;/webapps/&lt;myapp&gt;/&lt;myproject&gt;/runserver.py) with the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/local/bin/python2.5</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</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;">'/home/&lt;myuser&gt;/webapps/&lt;myapp&gt;/&lt;myproject&gt;/'</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;">'/home/&lt;myuser&gt;/webapps/&lt;myapp&gt;/lib/python2.5/django/bin'</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;">'/home/&lt;myuser&gt;/webapps/&lt;myapp&gt;/lib/python2.5/django/'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> flup.<span style="color: black;">server</span>.<span style="color: black;">fcgi_fork</span> <span style="color: #ff7700;font-weight:bold;">import</span> WSGIServer
    <span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span> <span style="color: #ff7700;font-weight:bold;">import</span> WSGIHandler
    WSGIServer<span style="color: black;">&#40;</span>WSGIHandler<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,maxSpare=<span style="color: #ff4500;">1</span>, maxChildren=<span style="color: #ff4500;">1</span>, debug=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Remember to change the &lt;myuser&gt; etc to the correct values, save the file and make it executable using:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> +x runserver.py</pre></div></div>

<p>We also need to change our django settings.py file and change the ROOT_URLCONF to:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">ROOT_URLCONF = <span style="color: #483d8b;">'urls'</span></pre></div></div>

<p>Because supervisor can only interact with non daemonized processes we need to make a minor change to our nginx.conf file and add the following line right at the begining:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">daemon off;</pre></div></div>

<p>Next we need to install supervisor:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">easy_install-<span style="color: #000000;">2.5</span> supervisor</pre></div></div>

<p>Then create a file called supervisord.conf in your home directory with the following content (remember to replace &lt;portnumber&gt; with the number from earlier in this post):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">; Webfaction supervisor config file.
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>inet_http_server<span style="color: #7a0874; font-weight: bold;">&#93;</span>          				; inet <span style="color: #7a0874; font-weight: bold;">&#40;</span>TCP<span style="color: #7a0874; font-weight: bold;">&#41;</span> server setings
<span style="color: #007800;">port</span>=127.0.0.1:<span style="color: #000000; font-weight: bold;">&lt;</span>portnumber<span style="color: #000000; font-weight: bold;">&gt;</span>	    			; <span style="color: #7a0874; font-weight: bold;">&#40;</span>ip_address:port specifier, <span style="color: #000000; font-weight: bold;">*</span>:port <span style="color: #000000; font-weight: bold;">for</span> all iface<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">username</span>=<span style="color: #000000; font-weight: bold;">&lt;</span>username<span style="color: #000000; font-weight: bold;">&gt;</span>           				; <span style="color: #7a0874; font-weight: bold;">&#40;</span>default is no username <span style="color: #7a0874; font-weight: bold;">&#40;</span>open server<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">password</span>=<span style="color: #000000; font-weight: bold;">&lt;</span>password<span style="color: #000000; font-weight: bold;">&gt;</span>   	   			           ; <span style="color: #7a0874; font-weight: bold;">&#40;</span>default is no password <span style="color: #7a0874; font-weight: bold;">&#40;</span>open server<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>supervisord<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #007800;">logfile</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myuser<span style="color: #000000; font-weight: bold;">&gt;/</span>logs<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span>supervisor<span style="color: #000000; font-weight: bold;">/</span>supervisord.log 		; <span style="color: #7a0874; font-weight: bold;">&#40;</span>main log <span style="color: #c20cb9; font-weight: bold;">file</span>;default <span style="color: #007800;">$CWD</span><span style="color: #000000; font-weight: bold;">/</span>supervisord.log<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">logfile_maxbytes</span>=50MB       						; <span style="color: #7a0874; font-weight: bold;">&#40;</span>max main logfile bytes b4 rotation;default 50MB<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">logfile_backups</span>=<span style="color: #000000;">10</span>          						; <span style="color: #7a0874; font-weight: bold;">&#40;</span>num of main logfile rotation backups;default <span style="color: #000000;">10</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">loglevel</span>=debug               						; <span style="color: #7a0874; font-weight: bold;">&#40;</span>log level;default info; others: debug,warn,trace<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">pidfile</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myuser<span style="color: #000000; font-weight: bold;">&gt;/</span>supervisord.pid                          		; <span style="color: #7a0874; font-weight: bold;">&#40;</span>supervisord pidfile;default supervisord.pid<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">nodaemon</span>=<span style="color: #c20cb9; font-weight: bold;">false</span>              						; <span style="color: #7a0874; font-weight: bold;">&#40;</span>start <span style="color: #000000; font-weight: bold;">in</span> foreground <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #c20cb9; font-weight: bold;">true</span>;default <span style="color: #c20cb9; font-weight: bold;">false</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">minfds</span>=<span style="color: #000000;">1024</span>                 						; <span style="color: #7a0874; font-weight: bold;">&#40;</span>min. avail startup <span style="color: #c20cb9; font-weight: bold;">file</span> descriptors;default <span style="color: #000000;">1024</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">minprocs</span>=<span style="color: #000000;">200</span>                						; <span style="color: #7a0874; font-weight: bold;">&#40;</span>min. avail process descriptors;default <span style="color: #000000;">200</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>rpcinterface:supervisor<span style="color: #7a0874; font-weight: bold;">&#93;</span>
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>supervisorctl<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #007800;">serverurl</span>=http:<span style="color: #000000; font-weight: bold;">//</span>127.0.0.1:<span style="color: #000000; font-weight: bold;">&lt;</span>portnumber<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #007800;">username</span>=<span style="color: #000000; font-weight: bold;">&lt;</span>username<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #007800;">password</span>=<span style="color: #000000; font-weight: bold;">&lt;</span>password<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>program:nginx<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #007800;"><span style="color: #7a0874; font-weight: bold;">command</span></span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myuser<span style="color: #000000; font-weight: bold;">&gt;/</span>webapps<span style="color: #000000; font-weight: bold;">/&lt;</span>myapp<span style="color: #000000; font-weight: bold;">&gt;/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>nginx <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myuser<span style="color: #000000; font-weight: bold;">&gt;/</span>webapps<span style="color: #000000; font-weight: bold;">/&lt;</span>myapp<span style="color: #000000; font-weight: bold;">&gt;/</span>conf<span style="color: #000000; font-weight: bold;">/</span>nginx.conf
<span style="color: #007800;">autostart</span>=<span style="color: #c20cb9; font-weight: bold;">true</span>
<span style="color: #007800;">autorestart</span>=<span style="color: #c20cb9; font-weight: bold;">true</span>
<span style="color: #007800;">redirect_stderr</span>=<span style="color: #c20cb9; font-weight: bold;">true</span>
<span style="color: #007800;">exitcodes</span>=<span style="color: #000000;">0</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>fcgi-program:django<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #007800;">socket</span>=unix:<span style="color: #000000; font-weight: bold;">///</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myuser<span style="color: #000000; font-weight: bold;">&gt;/</span>webapps<span style="color: #000000; font-weight: bold;">/&lt;</span>myapp<span style="color: #000000; font-weight: bold;">&gt;/&lt;</span>myproject<span style="color: #000000; font-weight: bold;">&gt;/</span>django.sock
<span style="color: #007800;">process_name</span>=<span style="color: #000000; font-weight: bold;">%</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>program_name<span style="color: #7a0874; font-weight: bold;">&#41;</span>s_<span style="color: #000000; font-weight: bold;">%</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>process_num<span style="color: #7a0874; font-weight: bold;">&#41;</span>02d
<span style="color: #007800;">numprocs</span>=<span style="color: #000000;">1</span>
<span style="color: #7a0874; font-weight: bold;">command</span> = <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myuser<span style="color: #000000; font-weight: bold;">&gt;/</span>webapps<span style="color: #000000; font-weight: bold;">/&lt;</span>myapp<span style="color: #000000; font-weight: bold;">&gt;/&lt;</span>myproject<span style="color: #000000; font-weight: bold;">&gt;/</span>runserver.py  	
<span style="color: #007800;">environment</span>=<span style="color: #007800;">DJANGO_SETTINGS_MODULE</span>=settings
<span style="color: #007800;">autostart</span>=<span style="color: #c20cb9; font-weight: bold;">true</span>
<span style="color: #007800;">autorestart</span>=<span style="color: #c20cb9; font-weight: bold;">true</span>
<span style="color: #007800;">redirect_stderr</span>=<span style="color: #c20cb9; font-weight: bold;">true</span></pre></div></div>

<p>Replace the &lt;username&gt; and &lt;password&gt; in the file above with ones of your choosing (not your webfaction account ones!) Assuming that all the &lt;xxx&gt; bits have been replaced properly we could now start supervisor &#8211; however we would still suffer from the problem of supervisor not being started on a reboot. Let&#8217;s fix that &#8211; create a file called start_supervisor.sh in your home directory with the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash </span>
&nbsp;
<span style="color: #007800;">NAME</span>=supervisord
<span style="color: #007800;">SUPERVISORD</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myuser<span style="color: #000000; font-weight: bold;">&gt;/</span>bin<span style="color: #000000; font-weight: bold;">/</span>supervisord
<span style="color: #007800;">SUPERVISORCTL</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myuser<span style="color: #000000; font-weight: bold;">&gt;/</span>bin<span style="color: #000000; font-weight: bold;">/</span>supervisorctl
<span style="color: #007800;">PIDFILE</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myuser<span style="color: #000000; font-weight: bold;">&gt;/</span>supervisord.pid
<span style="color: #007800;">OPTS</span>=<span style="color: #ff0000;">&quot;-c /home/&lt;myuser&gt;/supervisord.conf&quot;</span>
<span style="color: #007800;">PS</span>=<span style="color: #007800;">$NAME</span>
<span style="color: #007800;">TRUE</span>=<span style="color: #000000;">1</span>
<span style="color: #007800;">FALSE</span>=<span style="color: #000000;">0</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">$SUPERVISORD</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${PATH:+$PATH:}</span>/usr/local/bin:/usr/sbin:/sbin:/home/&lt;myuser&gt;/bin:&quot;</span>
&nbsp;
isRunning<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
    pidof_daemon
    <span style="color: #007800;">PID</span>=<span style="color: #007800;">$?</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$PID</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">1</span>
    <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">0</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
pidof_daemon<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #007800;">PIDS</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">pidof</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">$PS</span><span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #c20cb9; font-weight: bold;">true</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #007800;">$PIDFILE</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #007800;">PIDS2</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$PIDFILE</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$PIDS</span>; <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$i</span>&quot;</span> = <span style="color: #ff0000;">&quot;<span style="color: #007800;">$PIDS2</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">1</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">done</span>
    <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">0</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
start <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Starting Supervisor daemon manager...&quot;</span>
    isRunning
    <span style="color: #007800;">isAlive</span>=<span style="color: #007800;">$?</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${isAlive}</span>&quot;</span> <span style="color: #660033;">-eq</span> <span style="color: #007800;">$TRUE</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Supervisor is already running.&quot;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #007800;">$SUPERVISORD</span> <span style="color: #007800;">$OPTS</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Failed...!&quot;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;OK&quot;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
stop <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Stopping Supervisor daemon manager...&quot;</span>
    <span style="color: #007800;">$SUPERVISORCTL</span> shutdown <span style="color: #000000; font-weight: bold;">||</span>  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Failed...!&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;OK&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000; font-weight: bold;">in</span>
  start<span style="color: #7a0874; font-weight: bold;">&#41;</span>
    start
	<span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
  stop<span style="color: #7a0874; font-weight: bold;">&#41;</span>
    stop
	<span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
  restart<span style="color: #000000; font-weight: bold;">|</span>reload<span style="color: #000000; font-weight: bold;">|</span>force-reload<span style="color: #7a0874; font-weight: bold;">&#41;</span>
    stop
    start
    <span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">esac</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span></pre></div></div>

<p>we&#8217;ll use this script to start and stop supervisor so we need to make it executable. Now we need to add it to a cron job to make sure that it runs every few minutes (the script checks that supervisor is running so won&#8217;t start multiple instances of it) this will allow us to be back up and running a few minutes after the server comes back up.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">EDITOR</span>=<span style="color: #c20cb9; font-weight: bold;">nano</span> crontab <span style="color: #660033;">-e</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">*/</span><span style="color: #000000;">10</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>; .<span style="color: #000000; font-weight: bold;">/</span>start_supervisor.sh</pre></div></div>

<p>This will run our script every 10 mins. Now if you stop your Django and Nginx processes, cd to your home directory, and run</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>start_supervisor.sh</pre></div></div>

<p>You should now be running on supervisor and your site should be up. If you&#8217;d like to be able to see that status of your processes then mount the app we created at the start on a subdomain such as status.mydomain.com and then browse to that url (you&#8217;ll need to enter the user and password that you put into the supervisor.conf file)<br />
<div id="attachment_252" class="wp-caption aligncenter" style="width: 310px"><a href="http://fightingrabbits.com/wp-content/uploads/2009/09/supervisor.PNG"><img src="http://fightingrabbits.com/wp-content/uploads/2009/09/supervisor-300x97.PNG" alt="Supervisor Website" title="Supervisor Website" width="300" height="97" class="size-medium wp-image-252" /></a><p class="wp-caption-text">Supervisor Website</p></div><br />
<strong>Enjoy!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://fightingrabbits.com/archives/208/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Better Fonts in Safari for Windows</title>
		<link>http://fightingrabbits.com/archives/221</link>
		<comments>http://fightingrabbits.com/archives/221#comments</comments>
		<pubDate>Tue, 15 Sep 2009 19:21:12 +0000</pubDate>
		<dc:creator>Richard Cooper</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Random]]></category>
		<category><![CDATA[safari]]></category>
		<category><![CDATA[Software.]]></category>
		<category><![CDATA[typekit]]></category>

		<guid isPermaLink="false">http://fightingrabbits.com/?p=221</guid>
		<description><![CDATA[I have been experimenting with the fonts available from typekit for web design, and was really disapointed with their rendering on Windows. After a bit of digging in the settings of my Typekit browser of choice (Currently Safari 4 as my day-to-day browser Chrome 4 is not supported by the Typekit admin yet!) I found the [...]]]></description>
			<content:encoded><![CDATA[<p>I have been experimenting with the fonts available from typekit for web design, and was really disapointed with their rendering on Windows.</p>
<div id="attachment_220" class="wp-caption aligncenter" style="width: 310px"><a href="http://fightingrabbits.com/wp-content/uploads/2009/09/Standard-Smoothing.PNG"><img class="size-medium wp-image-220" title="Safari Font Rendering" src="http://fightingrabbits.com/wp-content/uploads/2009/09/Standard-Smoothing-300x50.PNG" alt="Safari Font Rendering" width="300" height="50" /></a><p class="wp-caption-text">Safari Font Rendering</p></div>
<div id="attachment_216" class="wp-caption aligncenter" style="width: 310px"><a href="http://fightingrabbits.com/wp-content/uploads/2009/09/Firefox-Smoothing.PNG"><img class="size-medium wp-image-216" title="Firefox Font Rendering" src="http://fightingrabbits.com/wp-content/uploads/2009/09/Firefox-Smoothing-300x52.PNG" alt="Firefox Font Rendering" width="300" height="52" /></a><p class="wp-caption-text">Firefox Font Rendering</p></div>
<p><span id="more-221"></span></p>
<p>After a bit of digging in the settings of my Typekit browser of choice (Currently Safari 4 as my day-to-day browser Chrome 4 is not supported by the Typekit admin yet!) I found the following font settings within Safari.</p>
<div id="attachment_219" class="wp-caption aligncenter" style="width: 310px"><a href="http://fightingrabbits.com/wp-content/uploads/2009/09/Safari-Font-Settings.PNG"><img class="size-medium wp-image-219" title="Safari Font Settings" src="http://fightingrabbits.com/wp-content/uploads/2009/09/Safari-Font-Settings-300x195.PNG" alt="Safari Font Settings" width="300" height="195" /></a><p class="wp-caption-text">Safari Font Settings</p></div>
<p>There are 5 possible settings for font smoothing, and I recommend you try them all for yourself as the results depend on your screen (and eyes!)</p>
<div id="attachment_220" class="wp-caption aligncenter" style="width: 310px"><a href="http://fightingrabbits.com/wp-content/uploads/2009/09/Standard-Smoothing.PNG"><img class="size-medium wp-image-220" title="Standard Smoothing" src="http://fightingrabbits.com/wp-content/uploads/2009/09/Standard-Smoothing-300x50.PNG" alt="Standard Smoothing" width="300" height="50" /></a><p class="wp-caption-text">Standard Smoothing</p></div>
<div id="attachment_217" class="wp-caption aligncenter" style="width: 310px"><a href="http://fightingrabbits.com/wp-content/uploads/2009/09/Light-Smoothing.PNG"><img class="size-medium wp-image-217" title="Light Smoothing" src="http://fightingrabbits.com/wp-content/uploads/2009/09/Light-Smoothing-300x49.PNG" alt="Light Smoothing" width="300" height="49" /></a><p class="wp-caption-text">Light Smoothing</p></div>
<div id="attachment_218" class="wp-caption aligncenter" style="width: 310px"><a href="http://fightingrabbits.com/wp-content/uploads/2009/09/Medium-Smoothing.PNG"><img class="size-medium wp-image-218" title="Medium Smoothing" src="http://fightingrabbits.com/wp-content/uploads/2009/09/Medium-Smoothing-300x46.PNG" alt="Medium Smoothing" width="300" height="46" /></a><p class="wp-caption-text">Medium Smoothing</p></div>
<p>That&#8217;s Better!&#8230;</p>
<p>Now why can&#8217;t Chrome and Firefox implement something like this?</p>
]]></content:encoded>
			<wfw:commentRss>http://fightingrabbits.com/archives/221/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Webfaction Memory Usage Script.</title>
		<link>http://fightingrabbits.com/archives/198</link>
		<comments>http://fightingrabbits.com/archives/198#comments</comments>
		<pubDate>Mon, 07 Sep 2009 19:19:56 +0000</pubDate>
		<dc:creator>Richard Cooper</dc:creator>
				<category><![CDATA[Random]]></category>

		<guid isPermaLink="false">http://fightingrabbits.com/?p=198</guid>
		<description><![CDATA[This is a nice little script that I found on the Webfaction forums. It will let you know how much memory you are using so that you can avoid going over your limit. The advantage of this script is that it will discount the per-user Apache threads from the main Webfaction Apache instance, which are [...]]]></description>
			<content:encoded><![CDATA[<p>This is a nice little script that I found on the Webfaction forums. It will let you know how much memory you are using so that you can avoid going over your limit. The advantage of this script is that it will discount the per-user Apache threads from the main Webfaction Apache instance, which are not counted towards your memory usage.<br />
<span id="more-198"></span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/local/bin/python2.5</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;
mem.py - A script which calculates, formats, and displays a customer's
memory usage
&nbsp;
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">subprocess</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
&nbsp;
CMD = <span style="color: #483d8b;">&quot;ps -o rss,command -u %s | grep -v peruser | awk '{sum += $1} END {print sum / 1024}'&quot;</span>
MEM = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    proc = <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'groups'</span>, shell=<span style="color: #008000;">True</span>, stdout=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span><span style="color: black;">&#41;</span>
    proc.<span style="color: black;">wait</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    stdout = proc.<span style="color: black;">stdout</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: #dc143c;">user</span> <span style="color: #ff7700;font-weight:bold;">in</span> stdout.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        proc = <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span>CMD <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">user</span>, shell=<span style="color: #008000;">True</span>, stdout=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span><span style="color: black;">&#41;</span>
        proc.<span style="color: black;">wait</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        MEM<span style="color: black;">&#91;</span><span style="color: #dc143c;">user</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">float</span><span style="color: black;">&#40;</span>proc.<span style="color: black;">stdout</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Total Memory Usage: %i MB'</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>MEM.<span style="color: black;">values</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: #dc143c;">user</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">sorted</span><span style="color: black;">&#40;</span>MEM.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">user</span>.<span style="color: black;">ljust</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">15</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>MEM<span style="color: black;">&#91;</span><span style="color: #dc143c;">user</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>.<span style="color: black;">rjust</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'MB'</span>
    <span style="color: #ff7700;font-weight:bold;">print</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Note: &quot;Total Memory Usage&quot; is only valid when you execute mem using your<span style="color: #000099; font-weight: bold;">\</span>
 account<span style="color: #000099; font-weight: bold;">\'</span>s primary SSH user.'</span>
    <span style="color: #ff7700;font-weight:bold;">print</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Download it <a href='http://fightingrabbits.com/wp-content/uploads/2009/09/mem.txt'>here</a> copy it to your server, rename it to mem.py and make it executable.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mv</span> mem.txt mem.py
<span style="color: #c20cb9; font-weight: bold;">chmod</span> +x mem.py</pre></div></div>

<p>You can then call it using:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python2.5 .<span style="color: #000000; font-weight: bold;">/</span>mem.py</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fightingrabbits.com/archives/198/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nginx &amp; Django on Webfaction (Part&#160;1b) – Configuring</title>
		<link>http://fightingrabbits.com/archives/160</link>
		<comments>http://fightingrabbits.com/archives/160#comments</comments>
		<pubDate>Mon, 07 Sep 2009 12:18:46 +0000</pubDate>
		<dc:creator>Richard Cooper</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Random]]></category>
		<category><![CDATA[Setup]]></category>
		<category><![CDATA[webfaction]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[installation]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://fightingrabbits.com/?p=160</guid>
		<description><![CDATA[In Part 1a of this series we compiled Nginx and Installed Django in this next part we are going to configure nginx and Django and get our welcome page displaying. First lets configure nginx, the default conf file looks like this: #user nobody; worker_processes 1; &#160; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>In Part 1a of this series we compiled Nginx and Installed Django in this next part we are going to configure nginx and Django and get our welcome page displaying.</p>
<p>First lets configure nginx, the default conf file looks like this:</p>
<p><span id="more-160"></span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#user  nobody;</span>
worker_processes  <span style="color: #000000;">1</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">#error_log  logs/error.log;</span>
<span style="color: #666666; font-style: italic;">#error_log  logs/error.log  notice;</span>
<span style="color: #666666; font-style: italic;">#error_log  logs/error.log  info;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#pid        logs/nginx.pid;</span>
&nbsp;
&nbsp;
events <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    worker_connections  <span style="color: #000000;">1024</span>;
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
&nbsp;
http <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    include       mime.types;
    default_type  application<span style="color: #000000; font-weight: bold;">/</span>octet-stream;
&nbsp;
    <span style="color: #666666; font-style: italic;">#log_format  main  '$remote_addr - $remote_user [$time_local] &quot;$request&quot; '</span>
    <span style="color: #666666; font-style: italic;">#                  '$status $body_bytes_sent &quot;$http_referer&quot; '</span>
    <span style="color: #666666; font-style: italic;">#                  '&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;';</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">#access_log  logs/access.log  main;</span>
&nbsp;
    sendfile        on;
    <span style="color: #666666; font-style: italic;">#tcp_nopush     on;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">#keepalive_timeout  0;</span>
    keepalive_timeout  <span style="color: #000000;">65</span>;
&nbsp;
    <span style="color: #666666; font-style: italic;">#gzip  on;</span>
&nbsp;
    server <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        listen       <span style="color: #000000;">80</span>;
        server_name  localhost;
&nbsp;
        <span style="color: #666666; font-style: italic;">#charset koi8-r;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">#access_log  logs/host.access.log  main;</span>
&nbsp;
        location <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
            root   html;
            index  index.html index.htm;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">#error_page  404              /404.html;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># redirect server error pages to the static page /50x.html</span>
        <span style="color: #666666; font-style: italic;">#</span>
        error_page   <span style="color: #000000;">500</span> <span style="color: #000000;">502</span> <span style="color: #000000;">503</span> <span style="color: #000000;">504</span>  <span style="color: #000000; font-weight: bold;">/</span>50x.html;
        location = <span style="color: #000000; font-weight: bold;">/</span>50x.html <span style="color: #7a0874; font-weight: bold;">&#123;</span>
            root   html;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># proxy the PHP scripts to Apache listening on 127.0.0.1:80</span>
        <span style="color: #666666; font-style: italic;">#</span>
        <span style="color: #666666; font-style: italic;">#location ~ \.php$ {</span>
        <span style="color: #666666; font-style: italic;">#    proxy_pass   http://127.0.0.1;</span>
        <span style="color: #666666; font-style: italic;">#}</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span>
        <span style="color: #666666; font-style: italic;">#</span>
        <span style="color: #666666; font-style: italic;">#location ~ \.php$ {</span>
        <span style="color: #666666; font-style: italic;">#    root           html;</span>
        <span style="color: #666666; font-style: italic;">#    fastcgi_pass   127.0.0.1:9000;</span>
        <span style="color: #666666; font-style: italic;">#    fastcgi_index  index.php;</span>
        <span style="color: #666666; font-style: italic;">#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;</span>
        <span style="color: #666666; font-style: italic;">#    include        fastcgi_params;</span>
        <span style="color: #666666; font-style: italic;">#}</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># deny access to .htaccess files, if Apache's document root</span>
        <span style="color: #666666; font-style: italic;"># concurs with nginx's one</span>
        <span style="color: #666666; font-style: italic;">#</span>
        <span style="color: #666666; font-style: italic;">#location ~ /\.ht {</span>
        <span style="color: #666666; font-style: italic;">#    deny  all;</span>
        <span style="color: #666666; font-style: italic;">#}</span>
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #666666; font-style: italic;"># another virtual host using mix of IP-, name-, and port-based configuration</span>
    <span style="color: #666666; font-style: italic;">#</span>
    <span style="color: #666666; font-style: italic;">#server {</span>
    <span style="color: #666666; font-style: italic;">#    listen       8000;</span>
    <span style="color: #666666; font-style: italic;">#    listen       somename:8080;</span>
    <span style="color: #666666; font-style: italic;">#    server_name  somename  alias  another.alias;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">#    location / {</span>
    <span style="color: #666666; font-style: italic;">#        root   html;</span>
    <span style="color: #666666; font-style: italic;">#        index  index.html index.htm;</span>
    <span style="color: #666666; font-style: italic;">#    }</span>
    <span style="color: #666666; font-style: italic;">#}</span>
&nbsp;
&nbsp;
    <span style="color: #666666; font-style: italic;"># HTTPS server</span>
    <span style="color: #666666; font-style: italic;">#</span>
    <span style="color: #666666; font-style: italic;">#server {</span>
    <span style="color: #666666; font-style: italic;">#    listen       443;</span>
    <span style="color: #666666; font-style: italic;">#    server_name  localhost;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">#    ssl                  on;</span>
    <span style="color: #666666; font-style: italic;">#    ssl_certificate      cert.pem;</span>
    <span style="color: #666666; font-style: italic;">#    ssl_certificate_key  cert.key;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">#    ssl_session_timeout  5m;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">#    ssl_protocols  SSLv2 SSLv3 TLSv1;</span>
    <span style="color: #666666; font-style: italic;">#    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;</span>
    <span style="color: #666666; font-style: italic;">#    ssl_prefer_server_ciphers   on;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">#    location / {</span>
    <span style="color: #666666; font-style: italic;">#        root   html;</span>
    <span style="color: #666666; font-style: italic;">#        index  index.html index.htm;</span>
    <span style="color: #666666; font-style: italic;">#    }</span>
    <span style="color: #666666; font-style: italic;">#}</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Now that we have seen the default file let&#8217;s back it up and start from scratch :)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>webapps<span style="color: #000000; font-weight: bold;">/&lt;</span>myapp<span style="color: #000000; font-weight: bold;">&gt;/</span>conf
<span style="color: #c20cb9; font-weight: bold;">mv</span> nginx.conf nginx.conf.bak
<span style="color: #c20cb9; font-weight: bold;">nano</span> nginx.conf</pre></div></div>

<p>First we need to set our basic Nginx properties:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#2 workers x 1024 connections gives us some resiliance and should be enough for most sites.</span>
worker_processes <span style="color: #000000;">2</span>;
&nbsp;
events <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    worker_connections <span style="color: #000000;">1024</span>;
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
http <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    include             mime.types;
    default_type        application<span style="color: #000000; font-weight: bold;">/</span>octet-stream;	
&nbsp;
    server <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        listen  myport;
        server_name mydomain;
        <span style="color: #666666; font-style: italic;">#You will need to change the next line to something bigger if you want to accept uploads over 5Mb</span>
        client_max_body_size 5m;
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Remember to replace myport and mydomain with your actual values from Part 1a!</p>
<p>Next let&#8217;s setup our http server config to serve our static media (You could setup a static only app to serve your media from the main Apache Instance, but let&#8217;s do it this way as a learning exercise.) We are going to add one location for all our static media, and one location just to serve the favicon. Edit the http section to look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">http <span style="color: #7a0874; font-weight: bold;">&#123;</span>
&nbsp;
    include             mime.types;
    default_type        application<span style="color: #000000; font-weight: bold;">/</span>octet-stream;	
&nbsp;
    server <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        listen  myport;
        server_name mydomain;
        client_max_body_size 5m;
&nbsp;
        location <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
            <span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myusername<span style="color: #000000; font-weight: bold;">&gt;/</span>webapps<span style="color: #000000; font-weight: bold;">/&lt;</span>myapp<span style="color: #000000; font-weight: bold;">&gt;/&lt;</span>myproject<span style="color: #000000; font-weight: bold;">&gt;/</span>media<span style="color: #000000; font-weight: bold;">/</span>;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
        location ~<span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>favicon.ico <span style="color: #7a0874; font-weight: bold;">&#123;</span>
            root <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myusername<span style="color: #000000; font-weight: bold;">&gt;/</span>webapps<span style="color: #000000; font-weight: bold;">/&lt;</span>myapp<span style="color: #000000; font-weight: bold;">&gt;/&lt;</span>myproject<span style="color: #000000; font-weight: bold;">&gt;/</span>media<span style="color: #000000; font-weight: bold;">/</span>img<span style="color: #000000; font-weight: bold;">/</span>;
            <span style="color: #666666; font-style: italic;">#Let's not waste time logging this access...</span>
            access_log off;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Again remember to replace <myusername>, <myapp> and <myproject> with your actual values.</p>
<p>Right now let&#8217;s setup nginx to talk to our fastcgi django process add this section into the configuration file after the favicon.ico location:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">location <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    proxy_set_header  	         X-Real-IP  <span style="color: #007800;">$remote_addr</span>;
    proxy_set_header             X-Forwarded-For <span style="color: #007800;">$proxy_add_x_forwarded_for</span>;
    fastcgi_pass                unix:<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myusername<span style="color: #000000; font-weight: bold;">&gt;/</span>webapps<span style="color: #000000; font-weight: bold;">/&lt;</span>myapp<span style="color: #000000; font-weight: bold;">&gt;/&lt;</span>myproject<span style="color: #000000; font-weight: bold;">&gt;/</span>django.sock;
    fastcgi_pass_header          Authorization;          
    fastcgi_hide_header          X-Accel-Redirect;
    fastcgi_hide_header          X-Sendfile;
    fastcgi_intercept_errors     off;
    include                        fastcgi_params;
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Our nginx.conf should now look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#2 workers x 1024 connections gives us some resiliance and should be enough for most sites.</span>
worker_processes <span style="color: #000000;">2</span>;
&nbsp;
events <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    worker_connections <span style="color: #000000;">1024</span>;
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
http <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    include             mime.types;
    default_type        application<span style="color: #000000; font-weight: bold;">/</span>octet-stream;	
&nbsp;
    server <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        listen  myport;
        server_name mydomain;
        client_max_body_size 5m;
&nbsp;
        location <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
           <span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myusername<span style="color: #000000; font-weight: bold;">&gt;/</span>webapps<span style="color: #000000; font-weight: bold;">/&lt;</span>myapp<span style="color: #000000; font-weight: bold;">&gt;/&lt;</span>myproject<span style="color: #000000; font-weight: bold;">&gt;/</span>media<span style="color: #000000; font-weight: bold;">/</span>;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
        location ~<span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>favicon.ico <span style="color: #7a0874; font-weight: bold;">&#123;</span>
           root <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myusername<span style="color: #000000; font-weight: bold;">&gt;/</span>webapps<span style="color: #000000; font-weight: bold;">/&lt;</span>myapp<span style="color: #000000; font-weight: bold;">&gt;/&lt;</span>myproject<span style="color: #000000; font-weight: bold;">&gt;/</span>media<span style="color: #000000; font-weight: bold;">/</span>img<span style="color: #000000; font-weight: bold;">/</span>;
           <span style="color: #666666; font-style: italic;">#Let's not waste time logging this access...</span>
           access_log off;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
        location <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
            proxy_set_header  	        X-Real-IP  <span style="color: #007800;">$remote_addr</span>;
            proxy_set_header            X-Forwarded-For <span style="color: #007800;">$proxy_add_x_forwarded_for</span>;
            fastcgi_pass                unix:<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/&lt;</span>myusername<span style="color: #000000; font-weight: bold;">&gt;/</span>webapps<span style="color: #000000; font-weight: bold;">/&lt;</span>myapp<span style="color: #000000; font-weight: bold;">&gt;/&lt;</span>myproject<span style="color: #000000; font-weight: bold;">&gt;/</span>django.sock;
            fastcgi_pass_header         Authorization;          
            fastcgi_hide_header         X-Accel-Redirect;
            fastcgi_hide_header         X-Sendfile;
            fastcgi_intercept_errors    off;
            include                     fastcgi_params;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>We need to make a slight change to the fastcgi_params file that is being included so that it works correctly with Django. Comment out the following line by placing a # in front of it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">fastcgi_param  SCRIPT_NAME		 <span style="color: #007800;">$fastcgi_script_name</span>;</pre></div></div>

<p>and add this just above it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">fastcgi_param  PATH_INFO          <span style="color: #007800;">$fastcgi_script_name</span>;</pre></div></div>

<p>You can download the complete sample files here <a href='http://fightingrabbits.com/wp-content/uploads/2009/09/configfiles.zip'>zip</a> / <a href='http://fightingrabbits.com/wp-content/uploads/2009/09/configfiles.tar.gz'>gz</a></p>
<p>Django configuration is pretty simple as we can use the default settings file for now:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Django settings for myproject project.</span>
&nbsp;
DEBUG = True
TEMPLATE_DEBUG = DEBUG
&nbsp;
ADMINS = <span style="color: #7a0874; font-weight: bold;">&#40;</span>
    <span style="color: #666666; font-style: italic;"># ('Your Name', 'your_email@domain.com'),</span>
<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
MANAGERS = ADMINS
&nbsp;
DATABASE_ENGINE = <span style="color: #ff0000;">''</span>           <span style="color: #666666; font-style: italic;"># 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.</span>
DATABASE_NAME = <span style="color: #ff0000;">''</span>             <span style="color: #666666; font-style: italic;"># Or path to database file if using sqlite3.</span>
DATABASE_USER = <span style="color: #ff0000;">''</span>             <span style="color: #666666; font-style: italic;"># Not used with sqlite3.</span>
DATABASE_PASSWORD = <span style="color: #ff0000;">''</span>         <span style="color: #666666; font-style: italic;"># Not used with sqlite3.</span>
DATABASE_HOST = <span style="color: #ff0000;">''</span>             <span style="color: #666666; font-style: italic;"># Set to empty string for localhost. Not used with sqlite3.</span>
DATABASE_PORT = <span style="color: #ff0000;">''</span>             <span style="color: #666666; font-style: italic;"># Set to empty string for default. Not used with sqlite3.</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Local time zone for this installation. Choices can be found here:</span>
<span style="color: #666666; font-style: italic;"># http://en.wikipedia.org/wiki/List_of_tz_zones_by_name</span>
<span style="color: #666666; font-style: italic;"># although not all choices may be available on all operating systems.</span>
<span style="color: #666666; font-style: italic;"># If running in a Windows environment this must be set to the same as your</span>
<span style="color: #666666; font-style: italic;"># system time zone.</span>
TIME_ZONE = <span style="color: #ff0000;">'America/Chicago'</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Language code for this installation. All choices can be found here:</span>
<span style="color: #666666; font-style: italic;"># http://www.i18nguy.com/unicode/language-identifiers.html</span>
LANGUAGE_CODE = <span style="color: #ff0000;">'en-us'</span>
&nbsp;
SITE_ID = <span style="color: #000000;">1</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># If you set this to False, Django will make some optimizations so as not</span>
<span style="color: #666666; font-style: italic;"># to load the internationalization machinery.</span>
USE_I18N = True
&nbsp;
<span style="color: #666666; font-style: italic;"># Absolute path to the directory that holds media.</span>
<span style="color: #666666; font-style: italic;"># Example: &quot;/home/media/media.lawrence.com/&quot;</span>
MEDIA_ROOT = <span style="color: #ff0000;">''</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># URL that handles the media served from MEDIA_ROOT. Make sure to use a</span>
<span style="color: #666666; font-style: italic;"># trailing slash if there is a path component (optional in other cases).</span>
<span style="color: #666666; font-style: italic;"># Examples: &quot;http://media.lawrence.com&quot;, &quot;http://example.com/media/&quot;</span>
MEDIA_URL = <span style="color: #ff0000;">''</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a</span>
<span style="color: #666666; font-style: italic;"># trailing slash.</span>
<span style="color: #666666; font-style: italic;"># Examples: &quot;http://foo.com/media/&quot;, &quot;/media/&quot;.</span>
ADMIN_MEDIA_PREFIX = <span style="color: #ff0000;">'/media/'</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Make this unique, and don't share it with anybody.</span>
SECRET_KEY = <span style="color: #ff0000;">'4#(%&amp;p)^37k2ihr-q-k73qau44ec&amp;8k==gyxb)1+$pck@4+qp='</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># List of callables that know how to import templates from various sources.</span>
TEMPLATE_LOADERS = <span style="color: #7a0874; font-weight: bold;">&#40;</span>
    <span style="color: #ff0000;">'django.template.loaders.filesystem.load_template_source'</span>,
    <span style="color: #ff0000;">'django.template.loaders.app_directories.load_template_source'</span>,
<span style="color: #666666; font-style: italic;">#     'django.template.loaders.eggs.load_template_source',</span>
<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
MIDDLEWARE_CLASSES = <span style="color: #7a0874; font-weight: bold;">&#40;</span>
    <span style="color: #ff0000;">'django.middleware.common.CommonMiddleware'</span>,
    <span style="color: #ff0000;">'django.contrib.sessions.middleware.SessionMiddleware'</span>,
    <span style="color: #ff0000;">'django.contrib.auth.middleware.AuthenticationMiddleware'</span>,
<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
ROOT_URLCONF = <span style="color: #ff0000;">'myproject.urls'</span>
&nbsp;
TEMPLATE_DIRS = <span style="color: #7a0874; font-weight: bold;">&#40;</span>
    <span style="color: #666666; font-style: italic;"># Put strings here, like &quot;/home/html/django_templates&quot; or &quot;C:/www/django/templates&quot;.</span>
    <span style="color: #666666; font-style: italic;"># Always use forward slashes, even on Windows.</span>
    <span style="color: #666666; font-style: italic;"># Don't forget to use absolute paths, not relative paths.</span>
<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
INSTALLED_APPS = <span style="color: #7a0874; font-weight: bold;">&#40;</span>
    <span style="color: #ff0000;">'django.contrib.auth'</span>,
    <span style="color: #ff0000;">'django.contrib.contenttypes'</span>,
    <span style="color: #ff0000;">'django.contrib.sessions'</span>,
    <span style="color: #ff0000;">'django.contrib.sites'</span>,
<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Finally we need to start our servers:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>webapps<span style="color: #000000; font-weight: bold;">/&lt;</span>myapp<span style="color: #000000; font-weight: bold;">&gt;/</span>
.<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>nginx
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">&lt;</span>myproject<span style="color: #000000; font-weight: bold;">&gt;</span>
python2.5 manage.py runfcgi <span style="color: #007800;">maxchildren</span>=<span style="color: #000000;">1</span> <span style="color: #007800;">maxspare</span>=<span style="color: #000000;">1</span> <span style="color: #007800;">method</span>=prefork <span style="color: #007800;">socket</span>=<span style="color: #007800;">$PWD</span><span style="color: #000000; font-weight: bold;">/</span>django.sock <span style="color: #007800;">pidfile</span>=<span style="color: #007800;">$PWD</span><span style="color: #000000; font-weight: bold;">/</span>django.pid <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span> $</pre></div></div>

<p>If you haven&#8217;t done it already mount your app in a domain from the webfaction control panel and then browse to the correct url and you should see the following:</p>
<div id="attachment_162" class="wp-caption aligncenter" style="width: 310px"><a href="http://fightingrabbits.com/wp-content/uploads/2009/08/django-app.png"><img src="http://fightingrabbits.com/wp-content/uploads/2009/08/django-app-300x107.png" alt="Django Welcome Screen" title="Django Welcome Screen" width="300" height="107" class="size-medium wp-image-162" /></a><p class="wp-caption-text">Django Welcome Screen</p></div>
<p>So there you have it &#8211; we have installed and configured Nginx + Django however we still have a few thing&#8217;s to do&#8230; In Part 2 I&#8217;ll show you a couple of ways to make sure that our site isn&#8217;t down for long if the web-server is rebooted or Django crashes.</p>
<p>In Part 3 I&#8217;ll show you how to hand off file uploads to Nginx and get a fancy upload progress bar into the bargin :D</p>
<p>This post was brought to you by the letters L and A and the following websites.</p>
<p><a href="http://forum.webfaction.com/viewtopic.php?id=1981">http://forum.webfaction.com/viewtopic.php?id=1981</a></p>
<p><a href="https://calomel.org/nginx.html">https://calomel.org/nginx.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fightingrabbits.com/archives/160/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
