Not able to run django with lighttpd

Viewed 627

I am learning Django and configuring Lighttpd as web server.

My Lighttpd Server Configuration for running Django is like below

server.modules = (
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_redirect",
        "mod_scgi",
        "mod_proxy",
)

server.document-root        = "/var/www/html"
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log"
server.pid-file             = "/var/run/lighttpd.pid"
server.username             = "www-data"
server.groupname            = "www-data"
server.port                 = 8899


index-file.names            = ( "index.php", "index.html", "index.lighttpd.html" )
url.access-deny             = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/javascript", "text/css", "text/html", "text/plain" )

# default listening port for IPv6 falls back to the IPv4 port
## Use ipv6 if available
#include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

scgi.protocol = "uwsgi"
scgi.server = (
  "/" => (( "host" => "127.0.0.1", "port" => 9000, "check-local" => "disable" )),
)

I created Django app polls (as part of Django tutorials mentioned in the Django documentation) where in the first part it is to create a simple view under polls/view.py

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

I did as is mentioned in the documentation and able to get above page by executing following command for uwsgi

uwsgi --http 127.0.0.1:9000 --chdir ~/new_project/mysite --wsgi-file mysite/wsgi.py 

And I got the webpage as expected enter image description here

But here is the problem when I want to open from Lighttpd web server it is not happening I changed uwsgi command as required from uwsgi --http ... to uwsgi --uwsgi-socket ...

Below is what I received from browser enter image description here

Not sure how to proceed to make it work? Any help will be highly appreciated

3 Answers

I guess, that I've run into a very similar issue. The fix is trivial. But since the combination of Django + uWSGI + Lighttpd seems to be somewhat unusual (in particular lighttpd is the uncommon piece here), it took way too lo to figure out. However, there is a nice repo showcasing this setup. In particular, the lighttpd.conf there was helpful. In the relevant section (scgi.server), I found this:

"fix-root-scriptname" => "enable"

The docs says (scgi.server shares most parameters with fastcgi.server):

"fix-root-scriptname": use this for backends with extension "/" (and check-local is disabled).

Since django urls end with / this fixed my issues. I don't get why this is not turned on by default and I'm not sure if there is any downside to enabling it. So far, I didn't find any. If somebody can add some insight on why this fixes the issue, I'd appreciate it.

The error page you show above is from Django, so lighttpd is successfully connecting to the backend running uwsgi --uwsgi-socket.

"Hello World" examples for configuring lighttpd with uwsgi can be found at: https://redmine.lighttpd.net/projects/lighttpd/wiki/HowToPythonWSGI

The Python code you posted imports HttpResponse and returns an HttpResponse.

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

The HTTP protocol is different from the UWSGI protocol. A simple "Hello World" application:

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain;charset=utf-8')])
    return ['Hello World!\n']

Your Django report makes it clear that polls/ is getting lost and it's possible (and worth checking) if it's landing in SCRIPT_NAME when Django expects it in PATH_INFO.

I run a Django site under lighttpd and I ran into trouble with SCRIPT_NAME and PATH_INFO and wrote some middleware to solve that.

You're running uwsgi a little differently to me, I run it on unix socket not an http port and I point lighttp at that socket. And that may well make a difference, I don't know.

But you can check out what I did here:

https://github.com/bernd-wechner/CoGs/blob/master/django_lighttpd_middleware/__init__.py

It might and might not be helpful.

Related