Python: How to setup Django in a Virtual Environment
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 |
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
Shankar Ram December 27, 2013 at 1:42 am
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
elorg March 30, 2014 at 12:48 pm
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:
James Davey March 31, 2014 at 8:52 am
Hi there! Thank you for pointing that out, as it has now been corrected.
Saurabh June 12, 2014 at 4:14 am
It is showing error in line 3 of Step 3.
The error is:–bash: syntax error near unexpected token `&’
Kindly check the same.
James Davey June 12, 2014 at 7:37 am
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.
oldski June 17, 2014 at 5:17 pm
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!
James Davey June 18, 2014 at 7:23 am
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.
Tim Chase September 26, 2014 at 9:24 pm
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
James Davey September 29, 2014 at 8:34 am
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.