#2 Install and Configure Nginx

Next we focus on webserver. For this course I chose nginx as webserver. In this part of the course I will explain where nginx fits in whole production picture and how it relates to django application in general.

In previous lesson we prepared our VPS. In this lesson we take care of web server part. The application who first meets, so to speak, your web requests is the web server and not Django application. There are couple of reasons for that. First of all, Django application by design is really great at working with complex business logic, interacting with database, managing users and authentication and so on. But when it comes for example to handle TLS (secure HTTP) layer - Django Web Framework just says "it is not my problem". Same with static files - css, javascript files, images, fonts; dealing with static files is just out of scope for Django Framework.

Yes, Django Web Framework can serve static files. But that is only out of courtesy to ease your web application development process. Long story short - you need a real web server in production. Top 2 choices are Apache and Nginx. In this lesson we use Nginx.

Hello, Http Rquest!

Before jumping on how to install and configure Nginx, let's understand it first. Web servers, Nginx in particular, usually hosts more than one web application (or website). So, one of the very first thing webserver needs to do, is to decide to whom that http request is addressed. In picture below NginX hosts 3 applications demo.dglte.net, papermerge.com and django-lessons.com. Each of those application has it's own requirements and specifics, for example papermerge.com is just a bunch of html and css files, while both django-lessons.com and demo.dglte.net are full fledged Django Applications. From the point of Nginx each of those 3 things are called - virtual server.


In practice, each virtual server is usually a separate file stored in directory /etc/nginx/sites-available/. So a name like 'site available' instead of virtual server is less scary.

To configure Nginx to serve demo.dglte.com, you need to create file /etc/nginx/sites-available/demo.dglte.com - with following content:

server {
    listen 80 ;
    server_name demo.dglte.net;

    location /static/ {
        alias /opt/demo/static/;

    location / {
       proxy_pass http://localhost:8000;


How you name the file, is not important, I chose to name it demo.dglte.net because it describes files purpose perfectly. It is not even important where you place the configuration file for that site. What is important though is to create a symbolic link to it from /etc/nginx/sites-enabled/:

cd /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/site-available/demo.dglte.net    # with ln -s command if you specify only the target

In nutshell, /etc/nginx/sites-available/demo.dglte.net defines a server directive, which instructs Nginx how to serve all incoming http requests from domain specified by server_name, on port specified by directive listen. Each server directive will have only one server_name. Another very important directive is alias directive.

Alias directive works by replacing the matched argument with its argument. See picture below for examples:


Result in the figure above is a file on file system which will be served by Nginx.

During screencast I use dig command to troubleshoot DNS related issue. Dig command has following format:

dig <domain> -t <DNS Record Type>

DNS record for mapping to IP address is A Record, I think A stands for alias.

Another popular command line tool for troubleshooting DNS is drill

#1 Setup the Remote Host

Let's prepare the remote host first. When you start with a new server, you need to create a non-root user account, setup hostname and most importantly take care of ssh access.

#2 Install and Configure Nginx

Next we focus on webserver. For this course I chose nginx as webserver. In this part of the course I will explain where nginx fits in whole production picture and how it relates to django application in general.

#3 Automation with Ansible

Ansible is a fantastic tool to have in your arsenal, it is practical, simple to use and a huge time saver. In this part of the course I introduce you to main concepts of this awesome tool. Don't skip it.

#4 Database

PostgreSQL - is the database of choice for this course. PostgreSQL is an extremely powerful database. The best part is that it is easy to setup and in this episode I will show you how.

#5 The Heart of it - Gunicorn + Django!

Gunicorn + Django + Nginx is the killer combo for production environment. It is extremely important to understand how they interact and how they relate to each other. In just 12 minutes you will learn the core of any production environment.

#6 Ansible Roles for Gunicorn and Application

In this unit we complete our ansible playbook. We will add two more ansible roles: one for gunicorn and one for django application. Here I assume that you completed previous lesson so that I will skip gunicorn details and I will focus only on ansible related material.


Without HTTPS (secured http) your application is not production ready. In this lesson you will learn how to secure your web application with certbot (Let's Encrypt Client)