How to install Django
Django is a Web framework written in Python that is designed to help build complex Web applications easily by reusing common code.
These instructions are designed to cover the specifics of setting up Django on our servers. Since we run a shared hosting environment that doesn't provide mod_python or root access, this guide will explain how to install and configure Django to run via FastCGI in a Virtual Python environment. Please be sure to consult the Django documentation for any further information.
Finally, please note that Django is an advanced software package that is targeted at Web developers who are familiar with the Python programming language. If you are looking for a content management system for your Web site that is extendable and easy to manage, you may want to consider WordPress instead.
1.Set up Python environment
First you will need to setup a "Virtual Python" installation. This will allow you to install additional Python modules without requiring system privileges. In order to continue, log into your shell account and run the following commands from your home directory:
/usr/bin/wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py python virtualenv.py private-python
You will then need to update your $PATH variable to use the new local copy of Python by default:
echo 'export PATH=$HOME/private-python/bin:$PATH' >> ~/.bash_profile
To reload your bash_profile and verify that the change has taken effect, run the following commands:
source ~/.bash_profile which python
... and it should display "/home/ex/example.com/private-python/bin/python".
2. Installing extra modules
Before creating your first Django site, you will need to install at least one additional Python module called "flup". Flup is a Python utility for FastCGI enabled sites. As mentioned above, our servers don't use mod_python, so FastCGI is used to run python scripts. You can install this package by running the following command:
easy_install flup
Now you are ready to install Django.
3. Get Django
To get the latest stable version of Django, please visit their download page and copy the link in the top right corner next to "latest release". For this example, Django-1.3.1 is the latest stable release. From your home directory, run the following command to download Django, using the link from their download page:
/usr/bin/wget -O Django-1.3.1.tar.gz http://www.djangoproject.com/download/1.3.1/tarball/
(It is important to use the -O command to specify the file name, or else it will download as "index.html").
Next, extract these files and navigate to the newly created Django-1.3.1 folder:
tar xzvf Django-1.3.1.tar.gz cd Django-1.3.1
Next, run the installer:
python setup.py install
This will copy the Django files to your local Python library. (Once the installer has finished, you can delete the Django-1.3.1 folder and the Django-1.3.1.tar.gz file if you wish.)
You can verify Django is installed by running the following command:
python -c "import django; print django.get_version()"
If everything is working properly, this will print "1.3.1". Django is now successfully installed on your account and you can start setting up projects. Below are instructions for creating a very simple test site below, to give an idea of how to manage Django projects on our servers.
4. Creating a test site
Django developers recommend keeping the code for your sites outside of your main Web directory, so users aren't able to view any sensitive information that may be contained within your project. You will now create a projects folder in your home directory to store your projects:
mkdir ~/projects cd ~/projects django-admin.py startproject testsite
This creates a directory at "~/projects/testsite". Now you need to add a link to this folder in the local Python "site-packages" directory, so Python recognizes your new module:
ln -s ~/projects/testsite/ ~/private-python/lib/python2.6/site-packages/
Now you will create a subdomain in your Web folder which will let you access the project via a Web browser:
mkdir ~/html/testsite
Then switch to that folder:
cd ~/html/testsite
Now you need to create a FastCGI script to run this project. Using a text editor, create a file called "dispatch.fcgi" that contains the following code:
#!/home/ex/example.com/private-python/bin/python
import sys
import os
sys.path.append('/home/ex/example.com/projects/testsite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'testsite.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
Once you have saved this file, make sure it has the correct permissions by running:
chmod 0755 dispatch.fcgi
Now create a .htaccess file with the following code inside:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !(dispatch.fcgi)
RewriteRule ^(.*)$ dispatch.fcgi/$1 [L]
This lets the Web server know to run your dispatch.fcgi script when somebody visits the Web site. You should now be able to view your Django project by visiting http://testsite.example.com/.
5. Using a database
If you plan to use Django’s database API functionality, you’ll need to create a MySQL databse. We recommend creating a new database specifically for each Django project, rather than using an existing one. To create the database on our servers, follow our instructions explaining how to create a MySQL database.
Django uses the MySQLdb package to interact with your MySQL database via Python, which you will need to install if using a database:
easy_install mysql-python
Your database details can be entered in the settings.py file for your application.
6. Final notes
Managing sites with Django can be a complicated task which goes well beyond the scope of our support. For any questions regarding the use of this software, please see the official Django documentation for more detailed information and support.
