The Definitive Guide to Installing Django on Ubuntu
I am writing this guide in order to explain the exact steps needed to install Django on Ubuntu 10.04 starting from scratch. The assumption is that you already have Ubuntu installed either on VirtualBox or as a standalone OS and just need the instructions to install PostgreSQL, Apache, mod_wsgi, and so forth. The guide is divided in the following sections:
- Installing Apache + mod_wsgi
- Installing PostgreSQL + python_pyscopg2
- Installing Django
- Linking everything together
A few other things to keep in mind
- Replace [username] with your Ubuntu username
- Replace Django-1.2.4 with the appropriate version at the time you download the Django package
1. Installing Apache + mod_wsgi
- Installing Apache:
sudo apt-get install apache2
In order for Apache to interface with Django we will need to install mod_wsgi which implements the Python WSGI interface as an Apache module.
- Install mod_wsgi:
sudo apt-get install libapache2-mod-wsgi
There is more to be done to link Django and Apache/mod_wsgi but we will cover that in the last section.
2. Installing PostgreSQL + python_psycopg2
- Install PostgreSQL:
sudo apt-get install postgresql
Now we need to install the Python adapter for PostgreSQL so Django can run queries on the database.
- Install python-psycopg2:
sudo apt-get install python-psycopg2
3. Installing Django
We are going to download the latest Django release (Django-1.2.4) and install it in a /var/src/ folder. I like to keep my Django files + apps and other Python scripts in a /var/src/ folder since it makes it cleaner and more maintainable rather than sticking every module in the global site/dist-packages folder.
First make a new directory to hold the Django files:
sudo mkdir /var/src cd /var/src
Download the latest Django release (Django-1.2.4) in the /var/src/ folder:
sudo wget http://www.djangoproject.com/download/1.2.4/tarball/ sudo tar xzvf Django-1.2.4.tar.gz cd Django-1.2.4
We now have to tell Python where to look for the Django files. We do this by creating a .pth file and placing it in the /usr/lib/python2.6/dist-packages/ directory. The actual name of the file is not important, just make sure it ends in .pth.
Create the file:
cd /usr/lib/python2.6/dist-packages sudo nano varsrc.pth
Enter the following line and then save your changes (Ctrl + X):
By creating the .pth file we let Python know where to look for the Django files by adding it to the module search path. To make sure this works, go to your command line and run the following command:
emilian@ubuntu:~$ python Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information.
Then import django:
>>> import django >>> django.VERSION (1, 2, 3, 'final', 0) >>>
Symlink the django-admin.py script to the your /usr/local/bin/ directory:
sudo ln -s /var/src/Django-1.2.4/django/bin/django-admin.py /usr/local/bin/django-admin.py
Now we have to start a new Django project called "website". We do this by running the following command from the /home/[username]/ directory:
django-admin.py startproject website
4. Linking Everything Together
We now have to create a WSGI file that links Django and mod_wsgi, and we also have to create the website in Apache.
- Create the file below in the /home/[username] directory and call it django.wsgi:
- Paste the following code inside of the file and save your changes (Ctrl + X). Don't forget to replace [username] with your Ubuntu username!
import os import sys
sys.path = ['/home/[username]'] + sys.path os.environ['DJANGO_SETTINGS_MODULE'] = 'website.settings'
import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
This file is used as the "glue" between your Django application and mod_wsgi. It sets the DJANGO_SETTINGS_MODULE variable and inserts your project path in the PYTHONPATH.
Create the following file in your /etc/apache2/sites-available directory and name it django.conf -- remember to replace [username] with your Ubuntu username:
WSGIScriptAlias / /home/[username]/django.wsgi
Alias /media/ /home/[username]/website/media/
<Directory /home/[username]/website/> Order allow,deny Allow from all </Directory>
Then enable the django site using this Debian specific command:Alternatively you can just symlink the /etc/apache2/sites-available/django.conf file to the /etc/apache2/sites-enabled/ directory.
sudo a2ensite django.conf
sudo /etc/init.d/apache2 restart
Load the Django site by typing http://localhost in your browser.
Voila you are done!
Please comment if you see an error or would like to see something added to the guide.