How to Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 16.04

By Hitesh Jethva, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

Django is a free, open source and high-level Python Web framework that encourages rapid development and clean, pragmatic design. Django the MVC (Model-View-Controller) architecture and is maintained by the Django Software Foundation. Django is a strong web framework that can assist you to get your application online as quickly as possible. The primary goal of Django is to ease the creation of complex, database-driven websites. Django supports four major database backends including, PostgreSQL, MySQL, SQLite, and Oracle.

You can run Django in conjunction with Apache, Nginx using WSGI, Gunicorn, or Cherokee using a Python module.

In this tutorial, we will learn how to install and configure Django with PostgreSQL database on and Elastic Compute Service (ECS) Ubuntu 16.04 server. We will also learn to configure the Gunicorn application server to interface with our applications and set up Nginx to reverse proxy to Gunicorn.

Prerequisites

  1. A fresh Alibaba Cloud Ubuntu 16.04 instance.
  2. A static IP address is configured.
  3. A root password is set up to your instance.

Launch ECS Instance

First, log in to your Alibaba Cloud ECS Console. Create a new ECS instance, choosing Ubuntu 16.04 as the operating system with at least 2GB RAM. Connect to your ECS instance and log in as the root user.

Once you are logged into your Ubuntu 16.04 instance, run the following command to update your base system with the latest available packages.

Install Required Packages

Before starting, you will need to install Nginx, PostgreSQL and some Python package to your system. You can install all of them with the following command:

Once all the packages are installed. You can proceed to configure PostgreSQL database.

Configure PostgreSQL

Next, you will need to create a PostgreSQL database and user for Django application.

First, log into PostgreSQL interactive session by running the following command:

Next, create a PostgreSQL database and user for your Django project using the following command:

Next, setting the default encoding to UTF-8, setting the default transaction isolation scheme to “read committed” and setting the timezone to UTC with the following command:

Next, grant all the privileges to your database and exit from the PostgreSQL session with the following command:

Create a Python Virtual Environment

Next, you will need to create a Python virtual environment for your project. You can do that by installing package required by Python within a virtual environment.

First, upgrade pip and install virtual environment with the following command:

Next, create a directory for your project:

Next, create a Python virtual environment by running the following command:

The above command will create a testprojectenv directory under testproject directory and install a local version of Python and a local version of pip.

Next, you will need to install all the required packages for your project.

First, activate the virtual environment with the following command:

After activation your virtual environment, install Django, Gunicorn, and the psycopg2 PostgreSQL adaptor with the following command:

Output:

Configure a New Django Project

Next, you will need to tell Django to install the records inside your project directory. You can do this with the following command:

Next, you will need define your server’s IP address, database username and password in settings.py file:

Make the following changes:

Save and close the file when you are completed.

Next, you will need to migrate the initial database schema to our PostgreSQL database. You can do this by running the following script:

Next, create an administrative user for the project by running the following command:

Output:

Next, collect all of the static content into the directory location you have configured by running the following command:

Now, you can start your project by starting up the Django development server with the following command:

Once the server started successfully. You should see the following output:

Performing system checks…

Now, open your web browser and type the URL http://your-server-ip:8000. You will be redirected to the Django index page as shown below:

You can also access Django’s admin interface by typing the URL http://your-server-ip:8000/admin in your web browser. You will be prompted for the admin username and password which you have created earlier as shown below:

After entering username and password, click on Login button. You will be redirected to the Django admin interface as shown below:

Now, press CTRL+C in the terminal window to shut down the development server.

If you want to test Gunicorn to test whether Gunicorn can serve the application. To do so, load your project’s WSGI module with the following command:

The above command will start Gunicorn on the same interface that the Django development server was running on. You can test it by visiting the URL http://your-server-ip:8000.

Once you have completed your testing, press CTRL+C in the terminal to stop the Gunicorn.

Now, deactivate your virtual environment by running the following command:

Create a Systemd Service file for Gunicorn

Next, you will need to create a systemd service file for Gunicorn. So, you can easily start and stop the application server. You can do this by creating the following file:

Add the following lines:

Save and close the file. Then, start Gunicorn service and enable it to start on boot time with the following command:

You can check the status of Gunicorn with the following command:

Output:

Configure Nginx to Proxy Pass to Gunicorn

Gunicorn is now configured and working. it’s time to configure Nginx to pass traffic to the process. You can do this by creating a new Nginx configuration file inside /etc/nginx/sites-available/ directory.

Add the following lines:

Save and close the file. Then, enable the Nginx virtual host by creating symlink:

Next, test the Nginx for any configuration error with the following command:

Output:

Finally, restart Nginx by running the following command:

Now, Nginx is configured to pass the traffic to the process. To test it, open your web browser and type the URL http://your-server-ip. You will be redirected to your application as shown in the following page:

Reference:https://www.alibabacloud.com/blog/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04_594319?spm=a2c41.12451631.0.0

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.