Code


Version 3 (modified by Chris Moffitt <chris.moffitt@…>, 7 years ago) (diff)

--

Setting up Django on Thinkhost

http://www.thinkhost.com has been my provider for a while. While they don't specifically promote their Python support, they run FreeBSD and have always been very willing to install additional Python packages upon request. For example, I recently asked them to install PIL and it was done in short order.

I noticed that they supported fcgi and figured I'd give it a shot to see if I could get Django working. Here's how I managed to get it running for my setup. This is heavily based on the other fcgi samples found on the web.

Control Panel Setup

  • Use the Thinkhost control panel to make sure that fcgi is enabled on your domain.
Manage Domains -> my-domain.com -> Edit -> Enable Fast-CGI: Enable
  • Use the control panel to create your MySQL username and password as well as any databases you may want to use. You'll need to put this in your settings file later on.

Command Line Configuration

  • ssh into your account
  • Create a directory to store Django
cd my-domain.com
mkdir tools
cd tools
  • Checkout the latest version of django into the tools directory
svn co http://code.djangoproject.com/svn/django/trunk/ django_src
  • Create a directory for flup and checkout the latest source into the tools directory
svn co http://svn.saddi.com/flup/trunk flup

  • Create ~/my-domain.com/cgi-bin/mysite.fcgi as shown below. Make sure to put in the correct paths.
#!/usr/local/bin/python
import sys, os

# Add a custom Python path.
sys.path.insert(0, "/usr/home/www/yourusername/my-domain.com/tools/django_src")
sys.path.insert(0, "/usr/home/www/yourusername/my-domain.com/tools/flup")
# Switch to the directory of your project. (Optional.)
os.chdir("/usr/home/www/yourusername/my-domain.com/tools/yourproject")

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

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
  • Create ~/my-domain.com/docs/.htaccess as shown below.
AddHandler fastcgi-script .fcgi
RewriteEngine On
RewriteRule ^/(media.*)$ /$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(admin.*)$ /cgi-bin/mysite.fcgi/$1 [QSA,L]
  • Create links to the admin media directory so the data will be served up when we go to the admin site.
ln -s /usr/home/www/yourusername/my-domain.com/tools/django_src/django/contrib/admin/media

This seems to work well for me. The only caveat is that it takes time after you make changes for them to show up on the system. As such, I can't recommend this approach for a development environment but it works fine for production where changes to your Django code will be minimal.