ooerda
Last Updated: December 01, 2017
·
15.61K
· madhugb
C344b028c960dad018e616657cb2a671

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
Say Thanks
Respond

2 Responses
Add your response

15355
664941f421568b9f71c197bc060049cd

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.

4 months ago ·
Filed Under