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.

Installation

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;
    server_name 0.0.0.0;

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

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

    location / {
            include proxy_params;
            proxy_pass 0.0.0.0:8030;
    }
}

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

Source: http://rahmonov.me/posts/run-a-django-app-with-nginx-and-gunicorn/

Deploy/Run a Django app with Gunicorn

Here is my directory structure of Django app:

myproject
├── myproject
│   ├── wsgi.py
|   ├── settings.py 
├── app_folder
├── manage.py
├── virtualenv

Install Guinicorn using pip:

pip install gunicorn

Now you might be wondering what is that wsgi thing is. Well, it stands for Web Server Gateway Interface and basically is a way how apps/frameworks and servers talk to each other. If the server(like Gunicorn) has wsgi implemented and so has your framework(Django), it means that you can run your app with that server. And the entry point of communication for these two is the variable application, which is located in myproject/wsgi.py in our case.

Let’s play with it a little bit to see what it got.

We can bind it to a specific port:

gunicorn --bind 0.0.0.0:8030 myproject.wsgi

You can increase the number of workers to serve requests, which you probable will in real life as your users increase:

gunicorn --workers 3 myproject.wsgi

Run it in a daemon mode:

gunicorn --daemon myproject.wsgi

Or all of them altogether(a shorter version):

gunicorn --daemon -b 0.0.0.0:8030 -w 3 myproject.wsgi

After running your app with gunicorn, go to the django admin panel at localhost:8000/admin. You will see that all styles are gone. The reason is that gunicorn is an application server and it does not serve static files. In order to solve this problem, we will take a look at Nginx next and use it as a reverse proxy for gunicorn. We will talk about what reverse proxy is as well so don’t think about it for now.

Source: http://rahmonov.me/posts/run-a-django-app-with-gunicorn-in-ubuntu-16-04/