How to Deploy Django 3 on Ubuntu 18.04

How to Deploy Django 3 on Ubuntu 18.04

Previously I had discussed about how to install Django 3 on Ubuntu 18.04 and run it using a development server. I am now discussing how to deploy Django 3 on Ubuntu 18.04 and run it using a Nginx HTTP server combined with a Gunicorn WSGI server.

0.System requirements

The software needed is:

  • Ubuntu Server 18.04 LTS (IP 216.158.228.175)
  • Django app
  • Nginx (HTTP server)
  • Gunicorn (WSGI server)
  • Domain (django.getbox.xyz)
Arsitektur Deployment Django
Django Deployment Architecture

HTTP requests from clients (web browsers) will be served by Nginx, if the requested request is a Django script (Python) then Nginx sends the request (reverse proxy) to Gunicorn (Green Unicorn – WSGI server). Then from Gunicorn accessing Django, then the results (response) are sent back from Django-> Gunicorn-> Nginx-> Web Browser.

1.Install Django app

Setting up the Django app for demo, if you already have the Django app, skip this step, proceed to the Gunicorn installation.

Install pip, virtualenv, and django.

Project configuration.

Ubah opsi ALLOWED_HOSTS dan tambahkan opsi STATIC_ROOT.

Run collectstatic to collect all static files (images, css, js).

The result

Try running it with a development server.

The result

Press CTRL + C to stop the development server.

2.Install Gunicorn

Install gunicorn via pip.

Run Django with Gunicorn.

The result

Press CTRL + C to stop the gunicorn.

Exit the virtual environment.

Create Gunicorn service

Create Gunicorn service so that Gunicorn doesn’t need to be run manually, keep running in the background (daemon), and by default it is active at boot (autostart).

Creating a service configuration file.

Fill in the file with the code below.

Create log directory.

Run the service and check the status.

The result.

3.Install Nginx

Install Nginx web server.

Create a server block for django.getbox.xyz.

Fill in the file with the code below.

Test Nginx configuration.

The message is displayed if there are no errors in the Nginx configuration.

Restart Nginx.

4.Test

Browse the Django application domain.

Django Default Page
Django Default Page

Leave a Reply

Your email address will not be published. Required fields are marked *