Knowledgebase: Web Hosting Related
How to setup Django with our shared hosting servers?
Posted by Support, Last modified by Support on 08 April 2014 01:55 PM

How to setup Django with our shared hosting servers?

Using virtualenv for Django and other Python frameworks is very easy. With that ease comes portability, just in case you need to move the site to a VPS or different shared server for any reason at any time. 

Before you get started, we'll need to be sure that your account has SSH (shell) access, pip, and virutalenv installed. This can all be accomplished by contacting our support department. You can just copy and paste the following into the ticket you create with us:

Hello Support,

I am following your tutorial for installing Django with virtualenv. Can you make sure that pip and virtualenv are installed on my server, and enable shell access for my account?

Domain name:
FTP Username: myusername

Once you’ve received confirmation from our team that those items are installed, then you can move on with the actual installation of Django. Throughout this tutorial you'll see the term ‘username’, which should be substituted with your unique FTP username. Let's get started!

  1. Login with SSH
  2. Once logged in, type the following commands

    mkdir .env
    cd .env
    virtualenv env
    source env/bin/activate
    pip install flup
    pip install django
    echo 'source /var/www/vhtdocs/username/.env/env/bin/activate' >> ~/.bashrc
    echo 'export PATH=$PATH:$HOME/.env/env/lib/python2.6/site-packages/django/bin' >> ~/.bash_profile
    echo 'export PYTHONPATH=$PYTHONPATH:$HOME/.env/env/lib/python2.6/site-packages' >> ~/.bash_profile

    Tip! Copy/paste the above text into a text editor, replace ‘username’ with your actual FTP username, and then copy the text out again and paste it straight into the shell session.

    Now we need to test it out.

  3. Log out of shell and log right back in
  4. To the left of the shell prompt you should see '(env)'... it's working!

    Does it work? Great! If not, check the steps you already completed. Also, check your .bashrc and .bash_profile to make sure that the entries in them are correct.

  5. Assuming it works, enter the Python interactive console by just typing 'python' and pressing Enter:

    (env)username@server [~]# python
    Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import flup
    >>> import django

Assuming there are still no errors, you're now ready to install Django and set it up to run via FastCGI, just like you'd do if you were not using virtualenv.

  1. Type the following:

    cd ~/
    mkdir website
    cd website
    # replace 'myproj' with whatever your project name startproject myproj

    # this is up to you, but it is nice to keep everything contained
    mkdir media
    mkdir scripts
    mkdir templates

  2. Create a symbolic link to your project so Python knows where it is:

    cd ~/.env/env/lib/python2.6
    ln -s ~/website/myproj/myproj

  3. Make sure we can import the project:

    (env)username@server [~]# python
    Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import myproj

If it doesn't import, then check your symbolic link that you crated in the previous step. Now you need to to configure Django to work via FastCGI:

  1. Navigate to your html directory

    cd ~/html

  2. Create a file called 'dispatch.fcgi' and paste in the following contents, changing 'username' to your actual FTP username:


    import sys, os

    sys.path.insert(0, '/var/www/vhtdocs/username/.env/env/lib/python2.6/site-packages')

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

    from django.core.servers.fastcgi import runfastcgi

    runfastcgi(method="threaded", daemonize="false")

  3. Once you've saved it make it executable:

    chmod 755 dispatch.fcgi

    Lastly, we need to create a .htaccess file to direct the browser to dispatch.fcgi.

  4. Create a file in html folder called '.htaccess' and paste in the following:

    AddHandler fcgid-script .fcgi
    RewriteEngine On
    Options +FollowSymLinks +ExecCGI

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

If you've followed the steps correctly, changed your username when needed, and everything else is in order, then you should be done installing Django! If you need any help with these steps, don't hesitate to contact our support department.

(3 vote(s))
Not helpful