Django is a free open-source web framework based on Python. It's a high-level python web framework with an MVT (Model-View-Template) architecture pattern. Django is maintained by the Django Software Foundation (DSF). The Django web framework is fast, it helps developers to built applications rapidly (RAD).

In this tutorial, I will show you how to install the Django web framework and configure it to use a PostgreSQL database for development. We will run Nginx as a reverse proxy for Django web framework for speed and security reasons. Django will run under the python WSGI HTTP server 'Gunicorn', we will manage and control 'Gunicorn' with 'Supervisor' as the process control system.

Prerequisite

Ubuntu 16.04

Root Privileges

Step 1 - Install Django, Virtualenv, and Gunicorn

I will use python3 as the default Python version for the system. We will install python3 and pip3 on Ubuntu 16.04. Pip3 is a package management system to install and manage software packages written in python.

Connect to your server as root user on the terminal or by SSH (like I do it here) and update the Ubuntu repository:

Step 2 - Install and Configure PostgreSQL

We will use PostgreSQL as the database for our Django project. Install PostgreSQL with this apt command.

sudo apt-get install postgresql postgresql-contrib libpq-dev

When the installation is finished, install the new python package named 'psycopg2' with pip. It's the python-postgreSQL database adapter.

pip install psycopg2

Now access the postgres user and open the PostgreSQL shell with psql command:

su - postgrespsql

Set a new password for the postgres user with the command below:

\password postgresEnter new password:

Next, create a new database and a new user/role for the django project in PostgreSQL. I will create new databases named 'django_db' with user/role 'python_dev'. Type command below on the PostgreSQL shell.

Step 3 - Start New Django Project

Start the first Django project with the name 'hello_django'. We are not going to use root privileges for the project, I will use a normal Linux user 'natsume' for the project. Feel free to use a different username that describes your project.

Create a New User

Create new Linux user with the command below:

useradd -m -s /bin/bash natsumepasswd natsume

-m = Automatically create the home directory.-s = Define the default shell for the user.

Create New Virtualenv and Install Django

Login to the natsume user:

su - natsume

Create a new virtual environment in the directory 'myproject' for our Django project with python3 as the default python version.

mkdir myprojectvirtualenv --python=python3 myproject/

Go to the 'myproject' directory and activate the virtualenv.

cd myproject/source bin/activate

Next, install Django, gunicorn, and psycopg2 with pip command in that environment.

pip install django gunicorn psycopg2

Start New Project with PostgreSQL

Start the Django project 'hello_django' with the django-admin command below:

django-admin startproject hello_django

It will create a new directory 'hello_django', go to that directory and edit the configuration file 'setting.py' with vim.

Step 5 - Install and Configure Supervisor

Supervisor is a process control system for Linux operating systems. It allows you to configure, monitor and control a number of processes on Linux. Supervisor is based on python, we can install it from the python repository pypi with the pip command or install it from the ubuntu repository with apt.

I will install supervisor with this apt command:

sudo apt-get install supervisor

Supervisor has a default configuration file in the '/ect/supervisor/' directory. Go to that directory and create a new configuration for our Django project so we can control it with supervisor.

Links

Muhammad Arul is a freelance system administrator and technical writer. He is working with Linux Environments for more than 5 years, an Open Source enthusiast and highly motivated on Linux installation and troubleshooting. Mostly working with RedHat/CentOS Linux and Ubuntu/Debian, Nginx and Apache web server, Proxmox, Zimbra Administration, and Website Optimization. Currently learning about OpenStack and Container Technology.