Installing a Django app on 1and1 Linux shared hosting

Using Django on shared hosting platforms (without root access, or ability to install Apache modules) is quite possible so long as you have SSH access. Finding all the steps you need to go through, in one place, is not so easy:

  1. Install Django and flup
  2. Create or upload yourdjangoapp folder to somewhere in your home directory on the 1and1 server
  3. Set appropriate file paths in yourdjangoapp/
  4. Move static files to a location within your webroot (e.g. webroot/static)
  5. Create an fcgi script to handle requests, and place this in webroot/cgi-bin
  6. Create an .htaccess file in your web root to direct requests to your fcgi script

Installing Django and flup

SFTP the Django tarball and flup .egg package to your server, unpack and install:

> tar xzf Django*tar.gz 
> cd Django*/
> python install --user # Django will be installed to .local in your home folder
> cd ..
> unzip flup*
> mv flup ~/.local/lib/python*/site-packages/
> mv EGG-INFO ~/.local/lib/python*/site-packages/
> python -c "import django" # to test - should not return any errors

In order to be able to use without entering the full path, add the following to ~/.bashrc

export PATH

Creating your app

If you need any help with this, there's a tutorial at the Django site.

Editing yourdjangoapp/

I'd advise setting a basepath variable with the path to your home directory on the server, then set STATIC_ROOT, and add/modify the TEMPLATE_DIRS values:

basepath = '/your/full/home/path'
STATIC_ROOT = basepath + 'webroot/static'
   basepath + 'pathto/yourdjangoapp/templates',

Move static files

Create an appropriate directory in webroot (this and the above STATIC_ROOT settings assume the directory is webroot/static)

python collectstatic --noinput # N.B. don't use --link, you'll get 403 errors

Creating the .fcgi script

Add the following script, yourdjangoapp.fcgi, and place in webroot/cgi-bin:

import sys, os

basepath = '/your/full/home/path'

sys.path.insert(0, basepath + '/.local/lib')
sys.path.insert(0, basepath + '/pathto/yourdjangoapp')

os.environ['DJANGO_SETTINGS_MODULE'] = 'main.settings'

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method='threaded', daemonize='false')

N.B., I tried creating this script in webroot, but found it would not run from there
N.N.B, When you change any code in your app, you may need to restart fastcgi with touch yourdjangoapp.fcgi

Create an .htaccess file

AddHandler fcgid-script .fcgi
RewriteEngine on
RewriteBase /

# Exclude any paths that should be served literally:
RewriteRule 	^static/		-			[L,NC]
# Add a default page
RewriteRule 	^$			home			[NC]

RewriteCond %{REQUEST_FILENAME} !(cgi-bin/main.fcgi) 
RewriteRule 	^(.*)$ 			cgi-bin/main.fcgi/$1	[QSA,L]

Your Django app should now run sans problème


"Be realistic - demand the impossible!" (Soyez réalistes - demandez l'impossible!)

Situationist graffiti, Paris 1968