Knowledgebase: How-To
Install Django Using Virtualenv
Posted by on 24 February 2014 04:55 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, open a ticket with our Tech Support Ninjas, and we'll triple-check for you.

Once you’ve received confirmation from our team that those items have been 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 username. Let's get started!

  1. Login 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. If you are using Python 3, you should use the "flup6" module instead of "flup"

    mkdir .env
    cd .env
    virtualenv --python=pythonx.x ~/.env/env
    source env/bin/activate
    pip install flup
    pip install django
    echo 'source $HOME/.env/env/bin/activate' >> ~/.bashrc
    echo 'export PATH=$PATH:$HOME/.env/env/lib/pythonx.x/site-packages/django/bin' >> ~/.bash_profile
    echo 'export PYTHONPATH=$PYTHONPATH:$HOME/.env/env/lib/pythonx.x/site-packages' >> ~/.bash_profile



    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
    django-admin 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:

    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.

    cd ~/.env/env/lib/pythonx.x
    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:

    #!/home/username/.env/env/bin/python
    
    import sys
    import os
    
    sys.path.insert(0, '/home/username/.env/env/lib/pythonx.x/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.

See also Use Python with FastCGI, Cheat Sheet for .htaccess

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

Help Desk Software by Kayako fusion
ERROR: This domain name (kb.asmallorange.com), does not match the domain name in the license key file help.asmallorange.com.

For assistance with your license, please contact the Kayako support team: https://support.kayako.com