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.
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.
This example assumes you, jdoe, will set up a django-projects directory to contain multiple django projects, and your first project is a blog.
$ mkdir /home/jdoe/django-projects $ cd /home/jdoe/django-projects $ django-admin.py startproject blog
Now we need to setup your fastcgi file, we’ll place this in /home/jdoe/www/blog/mysite.fcgi, this is assuming your document-root is /home/jdoe/www/blog.
#!/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")
Okay, now that you’ve set up your fcgi file, let’s test it out
$ 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
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>Unhandled Exception</title>
</head><body>
<h1>Unhandled Exception</h1>
<p>An unhandled exception was thrown by the application.</p>
</body></html>
This is the output you can expect to see, if you happen to get the following output instead
$ 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'
You should replace
runfastcgi(method="threaded", daemonize="false")
in mysite.fcgi with
runfastcgi(["method=threaded", "daemonize=false"])
Now the last step is setting up lighttpd, the following sets up django in your domain blog.domain.tld, 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.
$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",
)
}
Pingback: Running a FastCGI Application Server with a Lighttpd Front End | iamtgc
Pingback: Tagz | "iamtgc » Blog Archive » Django on Lighttpd with FastCGI" | Comments
Thanks, this was really helpful.
Try checking the paths ,using which models are getting imported.
Great Post.
However i am getting an import error when i run the fcgi script.
I am running it this way
cs@cs:~/workspace/djasngo-proj$PYHTONPATH=
test.fcgi
It gives the standard django error page. Upon opening it I got a cannot import error.
What could the problem possibly be ?
Thanks a lot for the guide! I finally managed to run my django app in lighty. I’m puzzled why django docs don’t say anything about the .fcgi file contents.
Thanks again!