Ayeshmantha Perera

Serving Django-Rest Application in Apache and Mod-wsgi

Django is a production grade, free and open-source web framework, written in Python, for the rapid development and clean pragmatic design of web applications.


Django (pronounced ‘Jango’) is a production grade, free and open-source web framework, written in Python, for the rapid development and clean pragmatic design of web applications. In the latest in our series of Mitra Innovation tech guides, we have written a tutorial about deploying the Django Rest Framework using the Apache Mod WSGI environment.

Thanks to the quick start guide provided by Django-rest framework, engineers can easily spin up a fresh Django-rest project with SQlite database support. This is the easiest part of the deployment.

As part of this tutorial, we’ve used a production grade Apache server to function as the server of the Django application. This is because the Django simplified server is not good enough when it comes to production grade deployments. It needs a more secure and powerful web server like Apache or Nginx to deploy in a production environment.

The text that follows provides a step-by-step guide for deploying the Django-rest project. We’ve used a virtual environment to configure the project and its dependencies. This is a best practice if you’re a python developer because maintaining python packages in a single environment is not that simple and can result in huge conflicts.

Step 1:
Create a directory

mkdir tutorial
cd tutorial

(Snippet 1: Creating a virtual environment and activating it)

virtualenv env
source env/bin/activate

(Snippet 2: This environment will contain all the Python packages that work as dependencies to our project)

It is important to always activate the virtual environment before ‘up and running’ a Django project. Otherwise, error messages will occur which state: “dependencies are not found”.

Step 2:
This involves the pip installation of django and it’s rest framework Django-rest, where django rest is written to decouple the client side of the application from the server to make it easy to integrate it with any client with Rest calls.

pip install django
pip install djangorestframework

(Snippet 3: Scaffolding a Django rest project)

django-admin.py startproject tutorial .
cd tutorial
django-admin.py startapp quickstart
cd ..

(Snippet 4: ‘Up and run’ the Django rest project)

python manage.py migrate
python manage.py createsuperuser –email admin@example.com –username admin

(Snippet 5 : Apply the migrations and create a super user – this will be the first sync of application with the database)

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

(Snippet 6: Ensure the settings file is edited and add the STATIC_FILE path is given – this is where the static files of the project will be located)

python manage.py collectstatic

(Snippet 7 : Run the above command to collect all static content in to the static directory as we specified in the path above [see Snippet 6])

Now we have a brand new Django-rest app.


(Fig 1: Preview of up and running Django app)

Step 3:
Now let’s take a look at the configurations to deploy the application on Apache using mod_wsgi.

As I mentioned before, the python interpreter and the simple server is not enough for a production grade server. mod_wsgi is a package that implements a simple to use Apache module that can host any Python web application which supports the Python WSGI specification.

sudo apt-get update
sudo apt-get install python-pip apache2 libapache2-mod-wsgi

(Snippet 8 : Installing mod_wsgi and plugins needed)

sudo nano /etc/apache2/sites-available/000-default.conf

(Snippet 9 : Edit the virtual default host file with our application configuration)

WSGIPythonPath /var/www/html/tutorial/tutorial
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request’s Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerAdmin webmaster@localhost
#DocumentRoot /var/www/html/tutorial
Alias /static “/var/www/html/tutorial/tutorial/static”
<Location “/media”>
SetHandler None
<LocationMatch “\.(jpg|gif|png|js|css)$”>
SetHandler None
<Directory /var/www/html/tutorial/tutorial >
WSGIProcessGroup tutorial
WSGIApplicationGroup %{GLOBAL}
Require all granted

WSGIDaemonProcess tutorial python-path=/var/www/html/tutorial/tutorial
WSGIProcessGroup tutorial

WSGIScriptAlias / /var/www/html/tutorial/tutorial/wsgi.py
# Available loglevels: trace8, …, trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with “a2disconf”.
#Include conf-available/serve-cgi-bin.conf

(Snippet 10 : Here’s a sample configuration of .conf file)

chmod 664 ~/tutorial/db.sqlite3
chmod 775 ~/tutorial
sudo chown :www-data ~/tutorial/db.sqlite3
sudo chown :www-data ~/tutorial

(Snippet 11 : Grant permissions in order to give Apache write permissions for the projects database)

sudo systemctl restart apache2

(Snippet 12 : Restart the server)

(Fig 2: Status of apache)


It works!

(Fig 3: Preview of up-and-running site on Apache)

Thank you for reading our latest Mitra Innovation Tech Guide. We hope you will also read our next tutorial so that we can help you solve some more interesting problems.

Click here to see other tutorials in our Mitra Innovation Tech Guide section.


Ayeshmantha Perera

Software Engineer | Mitra Innovation