Knowledgebase: Third-Party Software
Install Django Using Virtualenv
Posted by CTX Admin on 30 September 2014 12:17 PM

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. 

Note: to complete this tutorial, you'll need SSH access and pip/virtualenv needs to be installed on your server. Both of these should be done by default! So continue on with the tutorial. If you find you are having issues, opening a ticket with our Tech Support Ninjas and we'll triple-check for you.

 Throughout this tutorial you'll see the term ‘username’, which should be substituted with your unique username. Let's get started!

  1. Log in with SSH

  2. Once logged in, type the following commands

    Note: where x.x is the desired Python version. This article utilizes Python 2.6. To set up a different environment, like Python 2.7 or Python 3.4, change the code above to reflect that, where referenced.

    mkdir .env
    cd .env
    virtualenv --python=pythonx.x ~/.env/env
    source env/bin/activate
    pip install flup
    pip install django
    echo 'source /home/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 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 public_html directory

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

    import sys
    import os
    sys.path.insert(0, '/home/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 public_html called '.htaccess' and paste in the following:

    AddHandler fcgid-script .fcgi
    RewriteEngine On
    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 venerable Tech Support Ninjas.

(0 vote(s))
This article was helpful
This article was not helpful

Comments (0)
Post a new comment
Full Name:
Help Desk Software by Kayako fusion