<?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>iamtgc &#187; Lighttpd</title>
	<atom:link href="http://iamtgc.com/category/lighttpd/feed/" rel="self" type="application/rss+xml" />
	<link>http://iamtgc.com</link>
	<description></description>
	<lastBuildDate>Thu, 02 Feb 2012 00:15:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Running a FastCGI Application Server with a Lighttpd Front End</title>
		<link>http://iamtgc.com/2008/12/18/running-a-fastcgi-application-server-with-a-lighttpd-front-end/</link>
		<comments>http://iamtgc.com/2008/12/18/running-a-fastcgi-application-server-with-a-lighttpd-front-end/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 04:17:11 +0000</pubDate>
		<dc:creator>tgc</dc:creator>
				<category><![CDATA[Lighttpd]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://iamtgc.com/?p=63</guid>
		<description><![CDATA[Running a FastCGI Application Server separately from the Web Server has been an increasingly popular topic. The FastCGI Wikipedia Article states some of the reasons for it - &#8220;This separation allows server and application processes to be restarted independently — &#8230; <a href="http://iamtgc.com/2008/12/18/running-a-fastcgi-application-server-with-a-lighttpd-front-end/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Running a FastCGI Application Server separately from the Web Server has been an increasingly popular topic.  The <a href="http://en.wikipedia.org/wiki/FastCGI">FastCGI Wikipedia Article</a> states some of the reasons for it -</p>
<blockquote><p>&#8220;This separation allows server and application processes to be restarted independently — an important consideration for busy web sites. It also facilitates per-application security policies — important for ISPs and web hosting companies.&#8221;</p></blockquote>
<p>The <a href="http://www.lighttpd.net">lighttpd</a> web server includes the binary spawn-fcgi, which as the name suggests, allows you to spawn FastCGI processes.  This can be done independently of the web server processes.<br />
<span id="more-63"></span><br />
Lighttpd&#8217;s FastCGI module, mod_fastcgi is well documented <a href="http://redmine.lighttpd.net/wiki/1/Docs:ModFastCGI">here</a>, but there are a lot of arguments to sift through and it can be a process of trial and error to get a working configuration.</p>
<p>In this example, we will review how to configure lighttpd and spawn-fcgi on separate servers.  The web server which will run lighttpd and host all html, images, css, etc. files.  The FastCGI server will host all PHP files.</p>
<p>Here is the relevant excerpt from lighttpd.conf on <strong>Server A:</strong> <em>(the server hosting the html, images, css, etc)</em></p>
<pre class="brush:plain;">
server.modules = (
                          [ ... ]
                          "mod_fastcgi",
                          [ ... ]
                        )
[ ... ]
$HTTP["host"] =~ "(^|\.)iamtgc\.com$" {
   server.name = "iamtgc.com"
   server.document-root = "/lighttpd/" + server.name
   fastcgi.server         =  ( ".php" =>
                               ((
                                   "host" => "192.168.25.120",
                                   "port" => 9999,
                                   "docroot" => "/php/iamtgc.com",
                                   "check-local" => "disable"
                               ))
                             )
   accesslog.filename = "/var/log/lighttpd/" + server.name + "-access.log"
}
[ ... ]
</pre>
<p>On <strong>Server B</strong> <em>(where the FastCGI processes are running)</em> you start the FastCGI server like this:</p>
<pre class="brush:shell;"># spawn-fcgi -f /usr/local/bin/php-cgi -a 192.168.25.120 -p 9999</pre>
<p>Note, the php needs to exist in the <strong>docroot</strong> reference above, in this case /php/iamtgc.com.  The bind address (<strong>-a</strong>) and port (<strong>-p</strong>) also correspond to the <strong>host</strong> and <strong>port</strong> variables referenced in lighttpd.conf on Server A.</p>
<p>Also note, php-cgi can be replaced with something like dispatch.fcgi in the case of <a href="http://rubyonrails.org/">Ruby on Rails</a> or a similar fcgi file for <a href="http://www.djangoproject.com/">Django</a> as seen <a href="http://iamtgc.com/2007/07/04/django-on-lighttpd-with-fastcgi/">here</a>.  In both of these cases, other changes would be also required to Server A&#8217;s lighttpd.conf.</p>
]]></content:encoded>
			<wfw:commentRss>http://iamtgc.com/2008/12/18/running-a-fastcgi-application-server-with-a-lighttpd-front-end/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django on Lighttpd with FastCGI</title>
		<link>http://iamtgc.com/2007/07/04/django-on-lighttpd-with-fastcgi/</link>
		<comments>http://iamtgc.com/2007/07/04/django-on-lighttpd-with-fastcgi/#comments</comments>
		<pubDate>Thu, 05 Jul 2007 01:53:51 +0000</pubDate>
		<dc:creator>tgc</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Lighttpd]]></category>

		<guid isPermaLink="false">http://iamtgc.com/2007/07/04/django-on-lighttpd-with-fastcgi/</guid>
		<description><![CDATA[After having followed the Django documentation on how to use Django with lighttpd and fastcgi numerous times, I had very little success. Being new to Django and very anxious to dive in head first, this was a very frustrating hurdle &#8230; <a href="http://iamtgc.com/2007/07/04/django-on-lighttpd-with-fastcgi/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After having followed the Django documentation on how to use Django with lighttpd and fastcgi numerous times, I had very little success.  Being new to Django and very anxious to dive in head first, this was a very frustrating hurdle to encounter.</p>
<p>In this article I outline the steps which I took that lead to a successful configuration.  This article is not explanation on how to install Django, lighttpd or any of the associated components.  It is, however, my hope that this article will help you get Django running with lighttpd and fastcgi without the frustration.<br /> <br />
<span id="more-7"></span><br />
This example assumes you, jdoe, will set up a django-projects directory to contain multiple django projects, and your first project is a blog. </p>
<pre class="brush:shell;">
$ mkdir /home/jdoe/django-projects

$ cd /home/jdoe/django-projects

$ django-admin.py startproject blog
</pre>
<p>Now we need to setup your fastcgi file, we&#8217;ll place this in <b>/home/jdoe/www/blog/mysite.fcgi</b>, this is assuming your document-root is <b>/home/jdoe/www/blog</b>.</p>
<pre class="brush:py;">
#!/usr/pkg/bin/python2.4
import sys, os

# Add a custom Python path.
sys.path.insert(0, "/home/jdoe/django-projects")

# Switch to the directory of your project. (Optional.)
# os.chdir("/home/jdoe/django-projects/blog")

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "blog.settings"

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
</pre>
<p>Okay, now that you&#8217;ve set up your fcgi file, let&#8217;s test it out</p>
<pre class="brush:shell;">
$ python2.4 ./mysite.fcgi
WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Traceback (most recent call last):
  File "/usr/pkg/lib/python2.4/site-packages/flup-0.5-py2.4.egg/flup/server/fcgi_base.py", line 558, in run
    protocolStatus, appStatus = self.server.handler(self)
  File "/usr/pkg/lib/python2.4/site-packages/flup-0.5-py2.4.egg/flup/server/fcgi_base.py", line 1112, in handler
    result = self.application(environ, start_response)
  File "/usr/pkg/lib/python2.4/site-packages/Django-0.95.1-py2.4.egg/django/core/handlers/wsgi.py", line 148, in __call__
    response = self.get_response(request.path, request)
  File "/usr/pkg/lib/python2.4/site-packages/Django-0.95.1-py2.4.egg/django/core/handlers/base.py", line 59, in get_response
    response = middleware_method(request)
  File "/usr/pkg/lib/python2.4/site-packages/Django-0.95.1-py2.4.egg/django/middleware/common.py", line 40, in process_request
    if settings.APPEND_SLASH and (old_url[1][-1] != '/') and ('.' not in old_url[1].split('/')[-1]):
IndexError: string index out of range
Content-Type: text/html

&lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&gt;
&lt;html&gt;&lt;head&gt;
&lt;title&gt;Unhandled Exception&lt;/title&gt;
&lt;/head&gt;&lt;body&gt;
&lt;h1&gt;Unhandled Exception&lt;/h1&gt;
&lt;p&gt;An unhandled exception was thrown by the application.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
</pre>
<p>This is the output you can expect to see, if you happen to get the following output instead</p>
<pre class="brush:shell;">
$ python2.4 ./mysite.fcgi
Traceback (most recent call last):
  File "./mysite.fcgi", line 15, in ?
    runfastcgi(method="threaded", daemonize="false")
TypeError: runfastcgi() got an unexpected keyword argument 'method'
</pre>
<p>You should replace </p>
<pre class="brush:py;">
runfastcgi(method="threaded", daemonize="false")
</pre>
<p>in mysite.fcgi with</p>
<pre class="brush:py;">
runfastcgi(["method=threaded", "daemonize=false"])
</pre>
<p>Now the last step is setting up lighttpd, the following sets up django in your domain <b>blog.domain.tld</b>, but the important part is the fastcgi.server block.  The primary change that I had to make from the Django documentation is the addition of the bin-path variable.</p>
<pre class="brush:plain;">
$HTTP["host"] =~ "(^|\.)blog\.domain\.tld$" {
        server.document-root = "/home/jdoe/www/blog"
        accesslog.filename = "/var/log/lighttpd/blog.domain.tld-access.log"

        fastcgi.server = (
                ".fcgi" => (
                        "localhost" => (
                                "bin-path" => "/home/jdoe/www/blog/mysite.fcgi",
                                "socket" => "/home/jdoe/tmp/mysite.sock",
                                "check-local" => "disable",
                                "min-procs" => 2,
                                "max-procs" => 4,
                        )
                ),
        )

        alias.url = (
                "/media/" => "/home/jdoe/www/blog/media/",
        )
        url.rewrite-once = (
                "^(/media.*)$" => "$1",
                "^/favicon\.ico$" => "/media/favicon.ico",
                "^(/.*)$" => "/mysite.fcgi$1",
        )
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://iamtgc.com/2007/07/04/django-on-lighttpd-with-fastcgi/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

