Deploying Django Application with Nginx and uwsgi
Introduction
In this post we will learn about deploying Django applications. There are many ways to deploy a Django application. we can use either nginx or apache web server. Nginx is just a web server. It doesn’t know how to run Python programs. For this, we need a server which understands how to run python programs. This server is called application server. To run a application server, we can use uwsgi, gunicorn and etc. For hosting the application we can use standalone servers or cloud computing like Heroku.
This tutorial assumes you have Ubuntu server or any other Linux based system.
In this tutorial we are going to use
- Ubuntu hosting,
- nginx server,
- uwsgi app server
- postgresql for database
Django Project
For deploying our application, I have a very minimal twitter like project which is available in github. https://github.com/thavan/minitwit. Let’s checkout this repository later on when deploying.
Setting up Nginx
To start our deployment, let’s first install nginx.
| |
Let’s create a nginx configuration for our minitwit site. Create a new file /etc/nginx/conf.d/minitwit.conf and update it with following content.
| |
There are three location directive in the config. /static/ and /media/ are for serving static files. / is to be handled by django, so we pass it to upstream url called minitwit, which our django app server running with uwsgi.
Setting up uwsgi
Django runs on a application server. wsgi can be installed via pip
| |
Setting up Postgres
Install postgres using,
| |
and then start it using
| |
As this is our first deployment, we will create a new user and a new database. postgresql comes with new default account called postgres. We will login to that account to use it.
| |
In the psql prompt do the following to create database and user.
| |
Now try to connect as testuser
| |
If the above line works, we are good to go.
Checking out code and Starting application server
First, let’s checkout the code from git in our server
| |
If you take a look at /var/www/minitwit directory, you will see a file called muwsgi.ini. This is the configuration file for uwsgi server.
| |
We need a python virtual environment to deploy our code. Run the following commands to install virtualenv package.
| |
Add below three lines to your ~/.bashrc files.
| |
Now create a virtual environment.
| |
Let’s install all third party packages.
| |
Before starting the django app server, if you take a look at django’s settings.py, you will notice following lines.
| |
This is Django’s database configuration. Instead of giving db username and password, we are taking it from environment variables. So we have to set these environment variables in order to Django pickup db user name and password.
Take a look at prodenv.sh. Let’s modify it to update username and password details.
| |
That’s it. Now let’s set these environment variables.
| |
Start uwsgi server
| |
collect static files and migrate database
| |
Restart nginx
| |
Make sure in your DNS provider’s console, you created CNAME record for your sub-domain (like minitwit.thavanathan.com) to point to your server’s IP.
Congratulations! You have deployed your first Django application.