Here's something I was fighting with for about 3 days. At first I wanted to use virtualenv too, but it all resulted in a headache. I'm a novice in these matters, so forgive me.
Recently I bought a router TP-Link WR-1043ND. Not the best choice now, I'm aware: TP-Link MR3420 has higher quality to price ratio. Anyway, my router got upgraded to OpenWRT, version Backfire 10.3-RC4, thanks to great work of Cezary Jackiewicz aka Obsy.
First packages that we can install with OpenWRT package manager:
- lighttpd, lighttpd-mod-alias, lighttpd-mod-rewrite, lighttpd-mod-fcgi - packages needed for http server called lighty
- pysqlite, libsqlite3 - the database
- python wasn't installed at first, so we'll need this too
opkg install lighttpd lighttpd-mod-alias lighttpd-mod-rewrite lighttpd-mod-fcgi opkg python libsqlite3 pysqlite
These will be installed from Python Package Index (PyPI). We will need:
- setuptools - Go to pypi.python.org, search for setuptools, follow the instructions.
- pip - We should be able to call: easy_install pip to obtain it.
- django - With pip in place getting django is as easy as pip install django. Pip, contrary to easy_install, has a nice uninstall option.
- flup - For FastCGI related stuff
3 ways of doing thisFirst of all, let me briefly explain how FastCGI works. Actually this might not be a good idea to believe me, because, as I said, I'm a layman in this matters. There are 3 entities in this process: the browser, the server (Lighttpd), the FastCGI process (Django).
First step is forwarding the http request for a webpage to FastCGI process:
Browser => Lighttpd => Django FCGIRequest is processed and Django returns html source code of a webpage. Shove it back to your browser and render it.
Browser <= Lighttpd <= Django FCGI
- I came to conclusion that there are 3 ways of setting this up.
- By bin-path
- By host and port
- By socket
My app's directory: /www/apps/homepage. It doesn't have to be anything fancy. It's ok to use brand new project created with django-admin.py startproject homepage.
This method is my favourite so far. We tell lighttpd where to find executable file .fcgi and it starts FCGI processes by itself. All we need to take care of now is that the server is started after reboot. Here are the details:
- Contents of .fcgi file. It sits at the django project's directory.
#!/usr/bin/env python import sys, os # Add a custom Python path. sys.path.insert(0, "/www/apps") # Set the DJANGO_SETTINGS_MODULE environment variable. os.environ['DJANGO_SETTINGS_MODULE'] = "homepage.settings" from django.core.servers.fastcgi import runfastcgi # maxspare=2 is minimum from what i saw runfastcgi(method="threaded", daemonize="false", maxspare=2)
- Lighttpd config file from /etc/lighttpd/lighttpd.conf. It is edited version of default config file. I just omitted most of commented out lines.
You don't need .fcgi file in this method. Remove line with "bin-path" in fastcgi.server section of lighttpd config. Now you need to make sure that you run this at every boot (maybe you can use start-stop-daemon tool for this):
/www/apps/homepage/manage.py runfcgi method=threaded host="127.0.0.1" port=3033This will start FastCGI threads. Lighttpd will expect them to be there when he needs a page. Otherwise you'll get 503 or 500. This method is said to be easier because there is no need to set permissions on TCP socket.
In this method FCGI process and HTTP Server communicate through a socket (aka named pipe). Proper permissions must be in place. FCGI file is also not needed here. You must make sure that you run similar command as in the second method:
/www/apps/homepage/manage.py runfcgi method=threaded socket=/www/apps/homepage/homepage.socketLines in fastcgi.server with port and host get removed. Instead add
"socket" => "/www/apps/homepage/homepage.socket"
I use a lot of bad practices here:
- do everything as root
- not using virtualenv
- keeping my app in /www (but I changed my document-root, so maybe I'm ok?)
There's quite a lot of this kind of tutorials, but to someone without proper knowledge all these terms and blindly followed instructions are no good if something doesn't work as expected. I hope someone will find the wording I used useful. I hope will when I'll need to do everything all over again.