Deploy django project to production


Django is the most popular framework for web application. It is python framework and based on MVT (Models, Views & Templates) similarly to MVC frameworks. Deployment is an important part of the website development process. Deploying to the server is last essential step of every project. Here's an overview of the steps involved.

1. Create python virtual environment
2. Create Django project
3. Configure database
4. Deploy Django app to the server

If you haven't configured apache2 yet then follow Install and configure Apache2, MySQL, PHP and phpMyAdmin on ubuntu.

1. Create python virtual environment-

Here some assumption was made that you have already installed python, pip, and virtualenv on your Ubuntu system. If not run following command-

sudo apt-get update

sudo apt-get upgrade

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

sudo apt-get install apache2 libapache2-mod-wsgi

sudo pip install virtualenv

To create new python virtual environment type the following in your terminal and hit Enter-

virtualenv myenv


Now activate your virtual enviroment-

Source myenv/bin/activate

activate virtualenv

Now it’s time to install django and other required python modules. Run the following command to install django-

pip install django

It will install the latest version of Django supported to python version you are using.

Install django

2. Create Django project-

django-admin startproject myproject

you will get a folder created by Django named myproject. The folder tree looks like-

project tree

3. Configure database-

# myproject/myproject/

'default': {

'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),


replace above lines with-


"default": {

"ENGINE": "django.db.backends.mysql",
"NAME": "Test", #database name
"USER": "root", #database username
"PASSWORD": "password", #database password
"HOST": "",
"PORT": "",

Also at last of add-

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

Now install MySQL for python-

pip install mysqlclient

Install MySQL

Now Create your django app using the following command-

python startapp myapp

This will create a folder named myapp inside your myproject folder. Myproject folder tree now looks like-

tree after app create

Now navigate to your project and run-

cd myproject
python migrate

migrate database

Now run following command to check project is working or not. If you get following then congratulations!! You have done your first step.

congrats django

Now connect your App to your project we have to change some files. Add folloeing to your project’s

# myproject/myproject/

from django.conf.urls import url,include

url(r'^myapp/', include(myapp.urls)),

# file looks like-

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [

url(r'^myapp/', include('myapp.urls')),

Now create inside your app’s directory and paste following code-


from django.conf.urls import url
from views import TestView

urlpatterns = [

url(r'^test/', TestView),

Write TestView to your

# -*- coding:utf-8 _*_
from __future__ import unicode_literals

from django.shortcuts import render, HttpResponse

def TestView(request):

return HttpResponse('This is my test view')

Again run-

python runserver

and open into your browser. You will see the message you have written and return HttpResponse('This is my test view').

Now add domain you want to use in ALLOWED_HOSTS-


Create /etc/apache2/sites-available/django.conf using-

sudo vi /etc/apache2/sites-available/django.conf

and add following code inside django.conf

<VirtualHost *:80>

ServerAdmin [email protected]
DocumentRoot /var/www/myproject

Alias /static /var/www/myproject/static
<Directory /var/www/myproject/static>

Require all granted

<Directory /var/www/myproject/myproject>


Require all granted



WSGIDaemonProcess blogs python-home=/var/www/myenv python-path=/var/www/myproject
WSGIProcessGroup blogs
WSGIScriptAlias / /var/www/myproject/myproject/
ErrorLog /var/www/logs/error.log
CustomLog /var/www/logs/custom.log combined


Add domain name to your hosts-

sudo vi /etc/hosts

and the following line and save the file-

Now run the following to enable django.conf and other configurations-

sudo a2enmod rewrite

sudo a2enmod wsgi

sudo a2ensite django

sudo service apache2 restart

Now open and you will see the same page as

Note: If you are deploying to some domain globally then don’t forget to change DEBUG setting to False and you need to point out your public IP at domain’s DNS setting.


Join us for latest updates and blog posts

Recent Posts







RSS / Atom