This “How To” describe how have Django ready to work on RailsPlayground through two ways: Lighttpd+FastCGI (static) or Apache+FastCGI (dynamic).
RailsPlayground have actually a SVN version of Django (0.96-pre), it’s means that you don’t need to be worried about update. :)
This howto currently suggests to start a django application inside your public_html directory. I think this is a really unsafe setup, because unless you set file permissions with great care you risk making all of your project files visible on the web (thus giving away your database passwords stored in settings.py, among other things). You’d better use another arrangement, for example keeping all of your projects in a directory named django under your home dir and symlinking the media subdirectory.
To get the LightTPD web server running you have to request a port for it. Send an email to support@railsplayground.com with the domain (or subdomain) that will have the LightTPD over Apache. Note: this domain will only work under LightTPD.
Django project
Create a Django project in the public html of the domain:
ssh myuname@mydomain.com cd ~/public_html/ django-admin.py startproject myprojectname
See that django-admin created a folder with the name of your project in your public html directory, now you have to set the permissions of manage.py :
cd myprojectname/ chmod 755 manage.py
FastCGI process script
Now that you have the domain configured for LightTPD (actually just the port of domain, it still don’t work), go to your home directory and create the script fcgi.sh :
#!/bin/bash
# Replace these three settings.
PROJDIR="/home/myuname/public_html/myprojectname"
PIDFILE="$PROJDIR/myprojectname.pid"
SOCKET="$PROJDIR/myprojectname.sock"
cd $PROJDIR
if [ -f $PIDFILE ]; then
kill `cat -- $PIDFILE`
rm -f -- $PIDFILE
fi
exec $PROJDIR/manage.py runfcgi method="prefork" socket=$SOCKET pidfile=$PIDFILE minspare=1 maxspare=1 maxchildren=1
This script will turn on 2 FastCGI processes permanently (prefork method), it’s generally fine for a medium site, but if you want more processes (verify the amount of static processes that your plan support) you can change the parameters minspare (minimum number of spare processes to keep running), maxspare (maximum number of spare processes to prefork) and maxchildren (hard limit number of processes in prefork mode). Make fcgi.sh executable, execute it and check if the processes are running:
chmod 755 fcgi.sh ./fcgi.sh ps ux | grep runfcgi
Configure LightTPD
Create a file named lighttpd.conf (can be in your home dir):
server.dir-listing = "disable"
server.modules = ( "mod_rewrite",
"mod_access",
"mod_fastcgi",
"mod_accesslog")
# files to check for if .../ is requested
server.indexfiles = ( "index.html", "index.py")
## deny access the file-extensions
#
# ~ is for backupfiles from vi, emacs, joe, ...
# .inc is often used for code includes which should in general not be part
# of the document-root
url.access-deny = ( "~", ".inc" )
## bind to port, the port of your domain in railsplayground
server.port = 4012
## to help the rc.scripts
server.pid-file = "/home/myuname/lighttpd.pid"
server.document-root = "/home/myuname/public_html/myprojectname/"
server.errorlog = "/home/myuname/myprojectname_lighttpd.error.log"
accesslog.filename = "/home/myuname/myprojectname_lighttpd.access.log"
fastcgi.server = (
"/main.fcgi" => (
"main" => (
"socket" => "/home/myuname/public_html/myprojectname/myprojectname.sock"
)
),
)
url.rewrite-once = (
"^(/media.*)$" => "$1",
"^/favicon\.ico$" => "/media/favicon.ico",
"^(/.*)$" => "/main.fcgi$1",
)
Go to the /public_html/myprojectname directory and create the last file named main.fcgi :
#!/bin/sh export DJANGO_SETTINGS_MODULE=myprojectname.settings
Set the permissions to 755:
chmod 755 main.fcgi
Now just start the Lighttpd server and have fun. :)
lighttpd -f lighttpd.conf
Useful commands:
Restart FastCGI: /fcgi.sh
Kill FastCGI: kill -9 `cat ~/public_html/myprojectname/myprojectname.pid`
Kill LightTPD: kill -9 `cat lighttpd.pid`
In this setup method I will use a subdomain for example. First create your subdomain in the cPanel, something like “http://myprojectname.mydomain.com” and create the Django project with the same name replacing the subdmain directory (or create it with other name inside the subdomain dir, but make the correct changes in the dir path inside the scripts).
Django Project
ssh myuname@mydomain.com cd ~/public_html/ mv myprojectname/ myprojectname.bak django-admin.py startproject myprojectname
django-admin created a folder with the name of your project in your public html directory, now you have to set the permissions of manage.py :
cd myprojectname/ chmod 755 manage.py
File .htaccess
Save a file named .htaccess in your project directory with this content:
Note: this is a hidden file, to show it type “ls -a”
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} -d
RewriteCond %{SCRIPT_FILENAME} ^.*[^\/]$
RewriteRule ^(.*)$ $1/ [N]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]
ErrorDocument 500 "<h2>Application error</h2>Django application failed to start properly"
FastCGI script
Create a file named dispatch.fcgi in your project directory: Note: the path to python may be /usr/local/bin/python depending on the server which hosts your account.
#!/usr/bin/python import sys, os project_sys="/home/myuname/public_html" # Add a custom Python path. sys.path.insert(0, project_sys) # Set the DJANGO_SETTINGS_MODULE environment variable. os.environ['DJANGO_SETTINGS_MODULE'] = "myprojectname.settings" from django.core.servers.fastcgi import runfastcgi runfastcgi(method="threaded")
This script will turn on 1 FastCGI process dynamically (threaded method). It means that Apache will determine when start or kill it on-the-fly. Make dispatch.fcgi executable and it’s done, just access your subdomain. :)
chmod 755 dispatch.fcgi
Use FastCGI processes static or dynamic?
If you want a stable app running all the time and have only one domain or your host plan support more processes, then static FastCGI is recommended. But if you have more than one domain, a few processes and do not have a critical app, dynamic FastCGI sounds like a good idea.
Django official FastCGI tutorial: http://www.djangoproject.com/documentation/fastcgi/
Start your first app: http://www.djangoproject.com/documentation/tutorial1/