title: How does one deploy a django application on OS X Server?
body: <p>How would one deploy a Django application on OS X Server 2.0 without using homebrew or a different flavor of python than the one shipped with OS X 10.8.1? I'm using the cocoa bindings in a Django application and had trouble getting it to work with homebrew on my desktop machine (running OS X 10.8.1); hence the request to deploy the application on the system installed version of Python.</p>
  
  <p>I have the following OS X Server environment, with the following already installed:</p>
  
  <ul>
  <li>OS X 10.8.1</li>
  <li>OS X Server 2.0</li>
  <li>Python 2.7.2</li>
  <li>Apache 2.2.22</li>
  </ul>
  
  <p>Django 1.4.1 was installed using the following command:</p>
  
  <pre><code>sudo easy_install django
  </code></pre>
  
  <p>My first attempt is to deploy an empty website, and once that succeeds, deploy a real application to be used in production. The project was created at <code>/Library/Server/Web/Data/WebApps/mysite/</code> using the following command</p>
  
  <pre><code>django-admin.py startproject mysite
  </code></pre>
  
  <p>I ran the application using the following command. It simply confirmed that the application was up and running. It is the standard "It worked!" page when you first created a project.</p>
  
  <pre><code>python manage.py runserver 8080
  </code></pre>
  
  <p>I then created a file <code>/Library/Server/Web/Config/apache2/httpd_mysite.conf</code> with the following content: </p>
  
  <pre><code>WSGIScriptAlias /mysite /Library/Server/Web/Data/WebApps/mysite/mysite/wsgi.py
  </code></pre>
  
  <p>I further created a file <code>/Library/Server/Web/Config/apache2/webapps/com.example.mysite.wsgi.plist</code> with the following content:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-7"?&gt;
  &lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
  &lt;plist version="1.0"&gt;
  &lt;dict&gt;
          &lt;key&gt;name&lt;/key&gt;
          &lt;string&gt;com.example.mysite.wsgi&lt;/string&gt;
          &lt;key&gt;displayName&lt;/key&gt;
          &lt;string&gt;Python "My Site" app&lt;/string&gt;
          &lt;key&gt;launchKeys&lt;/key&gt;
          &lt;array/&gt;
          &lt;key&gt;proxies&lt;/key&gt;
          &lt;dict/&gt;
          &lt;key&gt;installationIndicatorFilePath&lt;/key&gt;
          &lt;string&gt;/Library/Server/Web/Data/WebApps/mysite/mysite/wsgi.py&lt;/string&gt;
          &lt;key&gt;includeFiles&lt;/key&gt;
          &lt;array&gt;
                  &lt;string&gt;/Library/Server/Web/Config/apache2/httpd_mysite.conf&lt;/string&gt;
          &lt;/array&gt;
          &lt;key&gt;requiredModuleNames&lt;/key&gt;
          &lt;array&gt;
                  &lt;string&gt;wsgi_module&lt;/string&gt;
          &lt;/array&gt;
  &lt;/dict&gt;
  &lt;/plist&gt;
  </code></pre>
  
  <p>The file <code>com.example.mysite.wsgi.plist</code> was adapted from <code>com.apple.webapp.wsgi.plist</code> and <code>httpd_mysite.conf</code> adapted from <code>httpd_wsgi.conf</code>. Both these files are used to run the "standalone" python application successfully when configured through the server manager.</p>
  
  <p>I then created a site with the server manager, confirmed my application was in the list of web applications. However, when I visit <a href="http://example.com/mysite" rel="noreferrer">http://example.com/mysite</a> I get a 500 error. The logs has the following entries (IP addresses changed to 1.2.3.4 for privacy reasons):</p>
  
  <pre><code>[Sat Sep 01 21:49:17 2012] [warn] Init: Name-based SSL virtual hosts only work for clients with TLS server name indication support (RFC 4366)
  [Sat Sep 01 21:49:17 2012] [notice] Apache/2.2.22 (Unix) PHP/5.3.13 with Suhosin-Patch mod_wsgi/3.3 Python/2.7.2 mod_fastcgi/2.4.6 mod_ssl/2.2.22 OpenSSL/0.9.8r DAV/2 configured -- resuming normal operations
  [Sat Sep 01 21:50:13 2012] [error] [client 1.2.3.4] (8)Exec format error: exec of '/Library/Server/Web/Data/WebApps/mysite/mysite/wsgi.py' failed
  [Sat Sep 01 21:50:13 2012] [error] [client 1.2.3.4] Premature end of script headers: wsgi.py
  </code></pre>
  
  <p>It doesn't seem that the WSGI module is handling the request, but instead, the request may be processed using FCGI. However, the log indicates that <code>mod_wsgi/3.3</code> was loaded. </p>
  
  <p>When I created a standard Python application that looks as follows:</p>
  
  <pre><code>def application(environ, start_response):
      status = '200 OK'
      output = 'Hello World!'
  
      response_headers = [('Content-type', 'text/plain'),
                          ('Content-Length', str(len(output)))]
      start_response(status, response_headers)
  
      return [output]
  </code></pre>
  
  <p>And update the files to point to <code>/Library/Server/Web/Data/WebApps/helloworld.wsgi</code> rather than <code>/Library/Server/Web/Data/WebApps/mysite/mysite/wsgi.py</code> then "Hello World" is displayed. I therefore assume that wsgi is correctly configured and able to execute applications and that something else is wrong with my setup.</p>