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:
- Install Django and flup
- Create or upload yourdjangoapp folder to somewhere in your home directory on the 1and1 server
- Set appropriate file paths in yourdjangoapp/settings.py
- Move static files to a location within your webroot (e.g. webroot/static)
- Create an fcgi script to handle requests, and place this in webroot/cgi-bin
- 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 setup.py 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 django-admin.py without entering the full path, add the following to ~/.bashrc
PATH=${PATH}:~/.local/bin
export PATH
Creating your app
If you need any help with this, there's a tutorial at the Django site.
Editing yourdjangoapp/settings.py
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'
TEMPLATE_DIRS = (
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 manage.py 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:
#!/usr/bin/python
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
HTH