Last Updated: December 21, 2018
· madhugb

Python + Django + Apache + Ubuntu

Configuring Django framework to work with Apache is little tricky, Here is a quick How-To guide.

Lets Jump-in...

Installing Python

By default python 2.7 is present in Ubuntu 11.04, If not then

sudo apt-get install python2.7 

Installing Django

You need pip installer for easy-installation

sudo apt-get install python-pip 

then install Django framework for python

sudo pip install Django

Creating a Django project

Its very easy to create a new project in Django
I am creating it inside /var/www/
(make sure django-admin.py is in your PATH)

cd /var/www/
django-admin.py startproject <my-project-name>

The above command should create a directory by the project name contains following files.

  |--- manage.py
  |--- <my-project-name>/
            |---- __init__.py
            |---- settings.py
            |---- urls.py
            |---- wsgi.py

Lets test if we have created project successfully, using default server given by Django, Remember this is for the development process,

cd /var/www/<my-project-name>
python manage.py runserver

If you open browser and open localhost:8000, it should show default Success page.
( remember to press Ctrl+D in terminal to close the server )

Note: You may want to create two directories *static** and templates inside your project directory for rendering static files like images, scripts and css files. templates for html templates used to render pages. see the apache configuration below for static directories*

So far we have installed python, Django and created a project in Django. Now lets move on to deploying the project in production environment i.e configuring it with Apache.


Django needs a wsgi configuration file to run in Apache.
Create a django.wsgi in side project directory,

vim /var/www/<my-project-name>/django.wsgi

and add the following content

import os
import sys


if path not in sys.path:

os.environ['DJANGO_SETTINGS_MODULE'] = '<my-project-name>.settings'

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

Configuring Apache

You need mod-wsgi adapter to configure Django in Apache
install wsgi lib like below.

sudo apt-get install libapache2-mod-wsgi
sudo a2enmod wsgi

Creating a Virtual Host for your site

After installing mod-wsgi you need to add a Virtual host in Apache
I am calling it django so that I can put all my Django sites configurations in one place

vim /etc/apache2/sites-available/django

and add the following content inside it,

<VirtualHost *:80>
    #My site Name
    ServerName <my-site-name>.com

    #Demon process for multiple virtual hosts
    WSGIDaemonProcess <my-site-name> threads=5

    #Pointing wsgi script to config file
    WSGIScriptAlias / /<my-site-dir>/django.wsgi
    WSGIProcessGroup <my-site-name>

    #Your static files location
    Alias /static/ "/<my-site-dir>/static/"
    <Location "/media">
        SetHandler None
    <LocationMatch "\.(jpg|gif|png|js|css)$">
        SetHandler None
    <Directory /<my-site-dir> >
        WSGIProcessGroup <my-site-name>
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all

You need to enable newly created site configuration in apache

sudo a2ensite django

Add your site to hosts

vim /etc/hosts

and add reference like below        <my-site-name>.com

Restart Apache

sudo /etc/init.d/apache2 restart

6 Responses
Add your response


thanks for your sharing.
it helps me a lot!

over 1 year ago ·

You shouldn't put your Django application code into /var/www/ or whatever the root directory for Apache is. Create another directory for this purpose.

over 1 year ago ·

i have a vps i tried all steps above but still nothing happened.

12 months ago ·

if this is a guide for a newbies, why assume someone will know how to edit the unfinished sections of this file: '/etc/apache2/sites-available/django' ??????
guide is effectively useless without that information

8 months ago ·

Hi Madhu. You said create the virtual host file django so that you may put all your django site configurations in one place. And then you asked to give the command : sudo a2ensite django but I get the error "ERROR: Site django does not exist!" . My site name is mycoolsite.com which is what I have specified inside my django virtual host file.

Also, what do you mean by add your site to /etc/hosts ? That file already had " local host " written in it. Should I edit it to mycoolsite.com ?

4 months ago ·

this is not working my ubuntu 16.04
like python3

3 months ago ·