Skip to content

Instantly share code, notes, and snippets.

@tkaemming
Created April 25, 2010 21:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tkaemming/378732 to your computer and use it in GitHub Desktop.
Save tkaemming/378732 to your computer and use it in GitHub Desktop.

Deploying Django on HostMonster Shared Hosting

I haven't fully tested this yet, so I may have forgotten a step (or two) but this should be fairly accurate.

  1. Set up SSH access on the host.

  2. Add your SSH to ~/.ssh/authorized_keys on the host.

  3. SSH in to the server.

  4. Set up Git.

     wget http://kernel.org/pub/software/scm/git/git-1.7.0.6.tar.gz
     tar xvf git-1.7.0.6.tar.gz
     cd git-1.7.0.6
     ./configure --prefix=$HOME
     make install
    
  5. Test the Git installation.

     cd ~
     git
    
  6. Download Setuptools for Python 2.6.

     wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefe74e
     tar xvf setuptools-0.6c11.tar.gz
     cd setuptools-0.6c11
    
  7. Create a local site-packages directory.

     mkdir ~/lib/python2.6 ~/lib/python2.6/site-packages
    
  8. Update your PYTHONPATH. Use a text editor to add this line to your ~/.bash_profile:

     export PYTHONPATH="${HOME}/lib/python2.6/site-packages/:${PYTHONPATH}"
    
  9. Install Setuptools.

     python2.6 setup.py install --prefix=$HOME
    
  10. Install pip.

    easy_install-2.6 --prefix=$HOME pip
    
  11. TODO: Virtualenv support? (HostMonster does not have python-devel, so you get this error when trying to create a new virtual environment because the setuptools bootstrap fails: error: invalid Python installation: unable to open /usr/lib/python2.6/config/Makefile (No such file or directory))

  12. Set up a SSH keypair for the hosting account.

    cd ~/.ssh
    ssh-keygen (name it id_rsa)
    
  13. Copy and paste the generated public key as a deploy key to your GitHub repository administration page.

    cat id_rsa.pub
    
  14. Create a directory for your django sites and clone your repository.

    mkdir ~/django-sites
    cd ~/django-sites
    git clone git://<repository url>
    cd <repository directory>
    
  15. Install dependencies from your pip requirements file. (You are using requirements files, right?) You'll want to include Django, PIL, database drivers, etc. in here.

    pip install -r requirements.txt --install-option="--prefix=${HOME}"
    
  16. Sync your database, and load any test data that you have. This may vary, depending on your setup, but I'm assuming your manage.py (or similar) is located in the project root.

    ./manage.py syncdb
    
  17. Change directories to the web root, and set up your fcgi file for the site. Name it something like <project name>.fcgi.

    #!/usr/bin/python2.6
    
    import sys, os
    
    HOME = '/home/<hostmonster username>/'
    SITE_PACKAGES_DIR = HOME + 'lib/python2.6/site-packages/'
    
    sys.path.insert(0, SITE_PACKAGES_DIR[:-1])
    # Add any other references to eggs that are not autoloaded for some reason(?) here:
    # For example, sys.path.insert(0, SITE_PACKAGES_DIR + 'flup-1.0.2-py2.6.egg')
    sys.path.insert(0, HOME + 'django-sites')
    
    os.chdir(HOME + 'django-sites/<project directory name>')
    
    # Set the DJANGO_SETTINGS_MODULE environment variable.
    os.environ['DJANGO_SETTINGS_MODULE'] = "<settings file here>" # This will probably be something like "myproject.settings" if you're running a simple project.
    
    from django.core.servers.fastcgi import runfastcgi
    runfastcgi(method="threaded", daemonize="false")
    
  18. Make sure you have flup installed! You can install it with pip install flup==1.0.2. Make sure to add it to your requirements.txt!)

  19. Set up your .htacccess file, with the following contents:

    AddHandler fastcgi-script .fcgi
    AddHandler fcgid-script .fcgi
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ <project name>.fcgi/$1 [QSA,L]
    
  20. If you are serving static media out of the same subdomain, symbolically link your static or media directory into the web root. You'll also have to do this for any admin media (the path to admin media will be $HOME/lib/python2.6/site-packages/django/contrib/admin/media.)

    ln -s $HOME/django-sites/<repository directory>/rest/of/path/to/static/media static
    
  21. Make sure your .fcgi file is running properly. It's just a normal Python script, so you should be able to execute it as such. (Although you might need to "chmod +x" it first.)

    ./<project name>.fcgi
    

    You should get some errors at the top of STDERR, but that's OK — they'll probably look like this…

    WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
    WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
    WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
    WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
    
  22. Profit!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment