Ansible playbooks to deploy a Django project - Part 1

25 Jun 2016

Two weeks back, I started learning Django. I built a polls app by following the tutorial from Django’s website1. Later I also built a blog using it. My code is available at https://github.com/pattu777/LearningDjango.

But I also wanted to know how a Django project is typically deployed on a remote server. I read a couple of blogs about the deployment process2. And I thought of automating it using Ansible.

So I ended up writing a bunch of Ansible playbooks to accomplish this. I bought a domain name learningdjango.in from GoDaddy and a cheap server from Digital Ocean to host my project. Please use my referral link if you want to sign up with Digital Ocean. So in this post I will describe

Prerequisites

A remote server.

Ansible installed on your local system.

Passwordless setup for a user with sudo privilleges.

A Django project copied onto the remote server.

For simplicity I am using SQLite as my back-end database.

A custom domain pointing to your cloud server(Optional). This domain name is used while configuring Nginx.

Architecture

First and foremost let’s see how our deployment architecture looks like. So we have our Django project on a remote server. We will use Gunicorn as our app server. In development we often use Django’s builtin server. But in production, it’s not advisable to use it. We will run Gunicorn on port 8000.

Next we will use Nginx as a reverse proxy server. Let me explain what it’ll do. From browser when we visit http://learningdjango.in, the browser will send a HTTP request to our server. Now Nginx will accept this request and simply forward it to our Gunicorn server running at port 8000. Apart from this, Nginx will also serve all the static(CSS, JS files) and media assets of our Django project. We will provide path to our static files in Nginx config file. We will run Nginx on port 80.

The role django will execute Django specific tasks such as migration, collection of static files etc.

The role services will configure and run gunicorn and Nginx.

Now create individual directories for each of the roles as shown below. Don’t worry about the YAML files in tasks and vars folder inside each roles. We will talk about them later. I have also added a README and a LICENSE file.

In core/tasks/main.yml, we will write a task that will loop through these packages and install them one by one. I am assuming the remote server is Debian based as I am using apt module in our playbook. Otherwise you can use yum to install the packages.