Site5 - Built For Designers & Developers MENU
Home  ›  Python  ›  Python: How to setup Django in a Virtual Environment

Python: How to setup Django in a Virtual Environment

9 Comments

In this article, we will show you how to setup Django in a virtual environment.

1.) Log into your account via SSH.

2.) Run the following to setup our export project name. Make sure you change “myproject” to the project name you wish to use.

export PROJECTNAME="myproject"

3.) Run the following to setup your local path

cd
mkdir -p ~/.local/bin
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
. .bashrc

4.) Then, you will need to set up a symlink for the python 2.6 installation on the server in our local installation folder:

cd ~/.local/bin; ln -s /usr/local/bin/python2.6 python

5.) Now you can download and unpack virtualenv into the ~/src folder:

mkdir ~/src; cd ~/src; curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.9.tar.gz; tar xzf virtualenv-1.9.tar.gz; cd virtualenv-1.9

6.) Now you can create a folder called site-packages and start the installation for virtualenv:

mkdir -p $HOME/.local/lib/python2.6/site-packages/
python setup.py install --prefix=~/.local
cd ~; virtualenv venv --distribute

7.) Enter the virtualenv shell (you will see a “(venv)” at the beginning of your prompt):

. venv/bin/activate

8.) From this shell you can now proceed on installing Django, flup and MySQL-python, then exit the shell:

pip install Django==1.4.5
pip install flup
pip install MySQL-python
deactivate
NOTE: after running the deactivate command your prompt should return to the previous shell without (venv)

9.) Now you can start you django project:

django-admin.py startproject $PROJECTNAME

10.) Then you will need to set up your .htaccess and index files for Django:

cd ~/public_html
vi .htaccess
  • Press INSERT on your keyboard and copy/paste the following content:
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.fcgi/\$1 [QSA,L]
  • Press ESCAPE and then type :wq! to write the file and exit.
vi index.fcgi
  • Press INSERT on your keyboard and copy/paste the following content (be sure to replace user and projectname with you username and project name):
#!/home/USER/venv/bin/python
USER='USER'
PROJECTNAME='PROJECTNAME'

import sys, os
from django.core.servers.fastcgi import runfastcgi

packages_path = os.path.join('/home', USER, 'venv/lib/python2.6/site-packages')
site_path = os.path.join('/home', USER)

sys.path.insert(0, packages_path)
sys.path.insert(0, site_path)

os.chdir(site_path)

os.environ['DJANGO_SETTINGS_MODULE'] = PROJECTNAME + '.settings'
runfastcgi(method='threaded', daemonize='false')
  • Press ESCAPE and then type :wq! to write the file and exit.

11.) All that’s left is to set executable permissions on index.fcgi:

chmod +x index.fcgi

And that’s it! Now you can visit your site in your browser and start customizing it

Please Note: In some situations, you may not notice changes made to your code/website. This can be resolved by killing/stopping the index.fcgi process over SSH. For more information on how to do this, please click here.

Still have a question? Or need help?
If you need technical support with your account, please email us or chat live with a representative.

9 Comments

  • Incase you are running into dependency issues on step 5 with the error : Cannot find sdist setuptools-*.tar.gz
    Cannot find sdist pip-*.tar.gz

    Do the following:

    $ cd ~/.local/bin
    $ curl -O https://raw.github.com/pypa/virtualenv/master/virtualenv.py
    $ curl -O https://pypi.python.org/packages/source/s/setuptools/setuptools-1.1.6.tar.gz
    $ curl -O https://pypi.python.org/packages/source/p/pip/pip-1.4.1.tar.gz

    Then cd ~ and continue with the venv commands

  • Step# 11 has a typo, in case it causes confusion for anyone –

    11.) All that’s left is to set executable permissions on index.fcgid:

    should be

    11.) All that’s left is to set executable permissions on index.fcgi:

    • Hi there! Thank you for pointing that out, as it has now been corrected.

  • It is showing error in line 3 of Step 3.
    The error is:–bash: syntax error near unexpected token `&’
    Kindly check the same.

    • Hello Sauabh,

      I am very sorry, but it appears that the characters that should appear in that command are being encoded, for some reason. The >> shown in that command should be >>

      That is, two right angle brackets.

      We will take a look at what is causing that to parse incorrectly in the article, and get that fixed up as soon as possible. Thank you for bringing this to our attention.

  • Any reason why I’d be getting these errors when executing index.fcgi?

    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!

    • There could be a few reasons. Can I get you to open a ticket on this with our support team, through BackStage? We’d be happy to take a look for you.

  • Step #8 is failing on boston:

    $ pip install Django==1.4.5
    Can not perform a ‘–user’ install. User site-packages are not visible in this virtualenv.
    Storing complete log in /home/redacted/.pip/pip.log

    • Hi Tim,

      I am sorry to hear that. Have you contacted our support team to make them aware? If not, I would recommend doing so, via BackStage.

Money Back Guarantees