Deploy/Run a Django app with Nginx, Gunicorn and Supervisor in Ubuntu

The reason we need Supervisord

Right now, we have our app running with Nginx and Gunicorn. However, every time our machine boots we have to start gunicorn and overall, controlling (stopping, restarting and etc) gunicorn is very difficult. What we want is an easy way of doing so.

Welcome Supervisord which allows us to monitor and control a number of processes on UNIX-like operating systems.

Let’s remember how we used to start our app:

gunicorn --daemon --workers 3 --bind unix:/home/ubuntu/myproject/myproject.sock myproject.wsgi

With supervisord at our disposal, it will be very easy and convenient to execute those commands:

supervisorctl start myproject
supervisorctl stop myproject
supervisorctl restart myproject

Installation and Setup

To install, type the following:

sudo apt-get install supervisor

Now, restart it:

sudo service supervisor restart

The main configuration file of supervisord is here /etc/supervisor/supervisord.conf. If we take a look, we will see that it contains these lines:

files = /etc/supervisor/conf.d/*.conf

It means that config files of specific projects can be stored here /etc/supervisor/conf.d/ and they will be included in that main file.

So, let’s create myproject.conf in /etc/supervisor/conf.d/ folder:

sudo vim /etc/supervisor/conf.d/myproject.conf

and configure our project:

command=/home/ubuntu/myprojenv/bin/gunicorn --workers 3 --bind myproject.wsgi

Let’s save the file and execute the following commands to bring these changes into effect:

sudo supervisorctl reread
sudo supervisorctl update

Well, that’s pretty much it. Simple, right? To verify that everything is working, type this:

ps ax | grep gunicorn

You should see several gunicorn processes running. Or, you can go to localhost:8000 and you will see your django app up and running.

Or, you can now use supervisor to check whether your app is running:

sudo supervisorctl status myproject


Deploy/Run a Django app with Nginx and Gunicorn in Ubuntu

The reason we need Nginx

If you followed the previous tutorial, we ran our django app with Gunicorn. However, at the end, we saw that the styles of the admin panel were gone. The reason is that Gunicorn is an application server and just runs the app (django app in our case) and django, as we know, does not serve static files except in development. Nginx to the rescue! It will be a reverse proxy for Gunicorn. What the hell is a reverse proxy? Good question! We all know what VPNs are, right? We use them to access some website that is blocked for some reason. In this case, we access that website through a VPN: We -> VPN -> some website. This kind of proxies are called Forward Proxies. As for reverse proxies, think of them as forced proxies. For example, a user is trying to access our django app running in gunicorn. He thinks that he is accessing the app directly. However, what is happening is that he is first accessing the Nginx server which decides what to do next. If the user is accessing a static file, the Nginx server will serve it itself. Otherwise, it will redirect it to Gunicorn. In plain terms, http requests will be handled by Gunicorn and static ones by Nginx. That’s why we need Nginx.

Apart from that, Nginx also improves performance, reliability, security and scale.


By now we already have Django and Gunicorn ready. So, let’s install Nginx now:

sudo apt-get install nginx

Now, we will configure Nginx to pass traffic to the process.

Create a file /etc/nginx/sites-available/domain and type in the following:

server {
    listen 8000;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
            root /home/ubuntu/myproject;

    location / {
            include proxy_params;

Now, let’s enable this file by linking it to the sites-enabled folder:

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

Restart nginx:

sudo service nginx restart


Learning too many new things today

Today, I installed Gitlab server on the experimental server (link) and solved the Monisha’s problem in BuildD project

Few new things that I have learned today:

  • Run Nginx on different ports instead of 80
  • Virtual site configuration with Nginx
  • Port forwarding by using Iptables command
  • Little C++ stuff
  • Working on dictionary list ( keys and values ) in Python
  • jQuery: Conditional show an element based on drop down box selection