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 different 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 Team 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!
- Login with SSH
- 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 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. - Log out of shell and log right back in
- 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. - 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.
- Type the following:
cd ~/
mkdir website
cd website
# replace 'myproj' with whatever your project name
django-admin.py startproject myproj
# this is up to you, but it is nice to keep everything contained
mkdir media
mkdir scripts
mkdir templates - 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 - 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:
- Navigate to your public_html directory
cd ~/public_html - 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") - 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. - 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 Team.