ooerda
Last Updated: November 01, 2018
·
33.4K
· 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.

<my-project-name>/
  |
  |--- 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*

Ok...
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.wsgi

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

path='/var/www/<my-project-name>'

if path not in sys.path:
  sys.path.append(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
    </Location>
    <LocationMatch "\.(jpg|gif|png|js|css)$">
        SetHandler None
    </LocationMatch>
    <Directory /<my-site-dir> >
        WSGIProcessGroup <my-site-name>
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

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

127.0.0.1        <my-site-name>.com

Restart Apache

sudo /etc/init.d/apache2 restart

6 Responses
Add your response

15355

thanks for your sharing.
it helps me a lot!

over 1 year ago ·
29161

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 ·
29650

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

10 months ago ·
29917

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

7 months ago ·
31010

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 " 127.0.0.1 local host " written in it. Should I edit it to 127.0.0.1 mycoolsite.com ?

about 2 months ago ·
31112

this is not working my ubuntu 16.04
like python3

about 1 month ago ·
Filed Under