Deploy django web application using NGinx, Gunicorn and Supervisor in Ubuntu

In this tutorial, you will learn about deployment on Ubuntu desktop or Ubuntu server using following technologies

Setup new or existing project

To start new project, create virtual environment using following commands. You can create virtual environment wherever you want but I would suggest to create parallel to you project folder. (For existing project)

Install django to start new project in Django

Start new project with name “demo”

Change directory to your project folder and install all the python dependencies (For existing project)

Migrate database

Collect all static/media files of all apps into one directory to server using Nginx and to do that following parameters must be set in

STATIC_ROOT = "<Project directory>/static"

MEDIA_ROOT= "<Project directory>/media"

Run following command to put all static files together in static root folder.

Now your project is ready to run, so let’s try once by hitting following command,

Check on browser with localhost:8000 URL. If it works fine then let’s go for next step.

Install Gunicorn and make ready to serve the Django application

Activate your python environment if you haven’t.

Install Gunicorn python package

Let’s check Gunicorn by serving Django application using command

Test in browser with the same URL and If it works fine then let’s move further and make script with more parameters, so that can be used as service.

Create folder deployment in your project folder and inside that create one file named gunicorn_service, so that file can be tracked along with your project code. Copy following content to this file and save it.

In above file, you need to change user, group and path of project. If you want to change those paths according to your structure then just make sure that you have the permission to create and read the files wherever required.

Further let’s test this script by using following two commands

If there is no error printed in console that means script is running successfully. Let’s move to next step setting up this script as service.

Setup script as startup service using Supervisor.

Let’s install supervisor first

Now create one file /etc/supervisor/conf.d/demo_gunicorn.conf and paste following code to that file.

Enable the service under supervisor and then start the service which we have created.

Check out the log file in logs folder under project folder and log must be similar as follows

Let’s setup Nginx for service static files and passing backend requests to Django application through Gunicorn.

Gunicorn is already running one upstream server and now we need to use that upstream in to nginx to create link between two. So let’s move to the first step of installation

Install nginx

Create new configuration file /etc/nginx/sites-avaiable/demo for demo project and paste following configuration in to the file.

Now let’s disable default nginx configuration and enable this demo app.

Restart Nginx and test the application in browser using http://localhost or

Bravo, You have successfully deployed Django application by yourself. Good Job !!

Originally published at