title: Deploy WSGI application with FastCGI
body: <p>I was trying to deploy Plone 5 with WSGI and FastCGI. Based on a <a href="http://blog.toms-projekte.de/run-plone-with-wsgi.html" rel="nofollow">link</a> on official Plone 5 document, Plone 5 supports deployment with WSGI. I can successfully run <code>bin/paste serve zope.wsgi</code> based on the link above, and see the Plone 5 home page.</p>
  
  <p>The next step is wrapping the WSGI application with <code>flup</code>, which make it compatible with FastCGI. I created two files:</p>
  
  <pre><code>dispatch_fcgi.py:
  
  #!/usr/bin/env python
  ve = '/home/xxx/Plone/zinstance'
  
  import site
  site.addsitedir(ve+'/eggs')
  
  from flup.server.fcgi import WSGIServer
  from paste.deploy import loadapp
  
  wsgi_app = loadapp('config:'+ve+'/zope.wsgi')
  
  if __name__ == '__main__':
      WSGIServer(wsgi_app).run()
  </code></pre>
  
  <p>and</p>
  
  <pre><code>dispatch.fcgi:
  
  #!/bin/bash
  this_dir=`dirname $0`
  
  export HOME=/home/xxx
  source "$HOME/Plone/zinstance/bin/activate"
  
  err_log_file="${this_dir}/../logs/dispatch_err.log"
  exec python "${this_dir}/dispatch_fcgi.py" "$@" 2&gt;&gt;"${err_log_file}"
  </code></pre>
  
  <p><code>.htaccess</code> redirects all requests to <code>dispatch.fcgi</code>, which set up all environments and pass arguments to <code>dispatch_fcgi.py</code>. The latter one then load the WSGI configuration file with <code>Paste</code>, and run it.</p>
  
  <p>Ideally, it should run exactly the same as running with <code>./bin/paste serve zope.wsgi</code>. However, it shows me the default Zope page, and I cannot access ZMI because it keeps asking me for password (I can access it with <code>paste serve</code>). Also, the URL is weird, like '<a href="http://xxxxxx/dispatch.fcgi/manage" rel="nofollow">http://xxxxxx/dispatch.fcgi/manage</a>', where <code>dispatch.fcgi</code> should not be a part of it.</p>
  
  <p>Any idea why the behaviour is different when executing <code>./bin/paste serve zope.wsgi</code> and running <code>paste</code> inside a script?</p>
  
  <p>UPDATE: <code>.htaccess</code></p>
  
  <pre><code>Options +ExecCGI
  AddHandler fastcgi-script .fcgi
  RewriteEngine On
  RewriteRule   ^(dispatch\.fcgi/.*)$  - [QSA,L]
  RewriteRule   ^(.*)$  dispatch.fcgi/$1 [QSA,L]
  </code></pre>