Complete guide on django installation

3 min read

Setting up Django with all required softwares can be a pain in the ass if you read offical documents. You could easily get lost by seemly endless documents related to software like apache, wsgi, etc.

Based on experiences by Ayman Farhat and PDX pixel , this article will pave a clear road for you at the marching journey to django.

I'm using ubuntu 12.04 with python 2.7.

1. Updating the repository

 sudo apt-get update 

2. Personally, I prefer to use vim to write codes, so:

sudo apt-get install vim

3. Then, you need to install pip, a tool for installing and managing python packages, (please read the quickstart if you'd like to,

sudo apt-get install python-pip python-dev build-essential

update pip for better performance:

pip install --upgrade pip 

4. Install database

pip apt-get install mysql-server

Install virtualenv to bypass conflicts among different version

pip install virtualenv

5. Create a virtual environment to work with:

mkvirtualenv /home/mysite  --no-site-packages

6. Install Django:

pip install Django==version number

7. Create a django project in virtual enviroment

mkdir /home/andy
cd /home/andy
source  ./bin/activate startproject andy

8. The next big step is set up database

mysql -u root -p ***** (database name)


Edit file so that database are linked

vim andy/andy/
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Using 'mysql' for this tutorial.
        'NAME': 'mysite',                     # Database name.
        'USER': 'root',
        'PASSWORD': '******',
        'HOST': '',                           # Empty for localhost.
        'PORT': '',                           # Empty string for default.

9. Run the belowing command (order is highly important)

sudo apt-get install python-mysqldb
sudo apt-get install libmysqlclient-dev
pip install mysql-python

10. synchronise what you created earlier:

python syncdb

Now, let's install and configure Apache and mod_wsgi:

sudo apt-get install apache2 apache2.2-common apache2-mpm-prefork apache2-utils libexpat1
sudo apt-get install libapache2-mod-wsgi

This should automatically restart Apache but just to be sure, issue this command:

sudo service apache2 restart

We need to now create a virtual host and set up WSGI. First, let's create the site in the Apache configuration:

vim /etc/apache2/sites-available/mysite

There are many different ways to set up the WSGI part, but personally I highly recommend Django docs on this part.

vim /home/django_projects/mysite/mysite/

In it, add these lines (Django sets some deefault values, so consider this carefully):

import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with

# Add the app's directory to the PYTHONPATH

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Let's then set where static files go for Django:

mkdir /var/www/mysite/static
vim /home/django_projects/mysite/mysite/

Make sure these lines are set:

STATIC_ROOT = '/var/www/mysite/static/'
STATIC_URL = '/static/'

Next, we need to tell Django to collect the static files for use on the site:

python collectstatic --noinput

Restart Apache again and you should be good to go:

sudo service apache2 restart
comments powered by Disqus
comments powered by Disqus