Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Django IntroductionChapter 6. Deploy Django App

Application Server Setup – Gunicorn

Application Server Setup – Gunicorn

Setting Up Gunicorn as Your Django Application Server

The Gunicorn (Green Unicorn) is a Python Web Server Gateway Interface (WSGI) HTTP server. As we have already installed Gunicorn, we'll explain how to set it up in this section. To manage the Gunicorn application server on a Linux server, we can use systemd to run the server.

As web services should always be available on the Internet, the process must run when the server runs. systemd is a daemon (or a service) that manages other daemons for Linux OS. When Linux OS starts or reboots, systemd also starts and continues running to centrally control other daemons until the OS shuts down.

If you want to learn more about systemd, please check our 'Linux OS Introduction' course.

Linux Introduction – 'Systemd'

There are three steps for Gunicorn setup.

  1. To run Gunicorn using systemd, you need to create two unit files. Unit files are used to set rules to manage units (processes) by systemd.
  2. Enable the unit files and start the units
  3. Check if the units are running

Create unit files

To run Gunicorn, you need to create a service unit file and a socket unit file.

Create a service unit file

A service unit file is used to create a service to run the application itself. The file should be stored under /etc/systemd/system. You can set any name but usually use a project name like project_d.service. To create and edit the file, use the vim command.

Command Line - INPUT
sudo vim /etc/systemd/system/project_d.service

Edit the file like in the example below.

/etc/systemd/system/project_d.service
[Unit]
Description=gunicorn daemon
Requires=project_d.socket
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/project_d
ExecStart=/home/ubuntu/project_d/d_env/bin/gunicorn \
        --access-logfile - \
        --workers 3 \
        --bind unix:home/ubuntu/project_d/project_d.sock \
config.wsgi:application

[Install]
WantedBy=multi-user.target

You need to carefully type file paths, as the file paths are also used in other files. For example, home/ubuntu/project_d/project_d.sock is the actual file that will be created under the project directory when the socket is enabled. The same path should be written in the socket unit file and the Nginx configuration file, which will be explained in the next section.

Create a socket unit file

A socket unit file is used to establish socket communication between Gunicorn and Nginx. To create and edit the file, use the vim command.

Command Line - INPUT
sudo vim /etc/systemd/system/project_d.socket

Edit the file like in the example below.

/etc/systemd/system/project_d.socket
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/home/ubuntu/project_d/project_d.sock

[Install]
WantedBy=sockets.target

Enable the unit files and start the units

You can enable and start the service and socket by running the command below. The systemctl enable command is used to enable units. With the --now option, you can start the unit at the same time. You can omit .service when you run the command for a service unit.

Command Line - INPUT
sudo systemctl enable --now project_d
sudo systemctl enable --now project_d.socket

Check if the units are running

To check the socket unit status, run the systemctl status command.

Command Line - INPUT
systemctl status project_d.socket

If the socket is properly running, you can see the message like the one below.

Command Line - RESPONSE
● project_d.socket - gunicorn socket
Loaded: loaded (/etc/systemd/system/project_d.socket; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-04-21 11:55:33 UTC; 22h ago
Triggers: ● project_d.service
Listen: /home/ubuntu/project_d/project_d.sock (Stream)
Tasks: 0 (limit: 2372)
Memory: 0B
CGroup: /system.slice/project_d.socket

To check the service unit status, run the command below.

Command Line - INPUT
systemctl status project_d

If the service is running properly, you can see the message like the one below.

Command Line - RESPONSE
● project_d.service - gunicorn daemon
Loaded: loaded (/etc/systemd/system/project_d.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2023-04-21 16:05:41 UTC; 18h ago
TriggeredBy: ● project_d.socket
Main PID: 123874 (gunicorn)
Tasks: 4 (limit: 2372)
Memory: 119.5M
CGroup: /system.slice/project_d.service

To learn about the unit files for Gunicorn, you can check the Gunicorn official documentation.

Gunicorn documentation reference: Deploying Gunicorn systemd

IdeaNote: Check Gunicorn process

We checked if the socket and service units were running, but we didn't check if the application was running using Gunicorn. Here is a way to check if Gunicorn is working.

Run the command below after activating the virtual environment. We use port 8001 in case port 8000 is used for the local app.

Command Line - INPUT
gunicorn --bind=0.0.0.0:8001 config.wsgi:application

If Gunicorn starts appropriately, you'll see a pop-up in the VS Code window like the one below. Click the Open in Browser button to see the app.

VS Code message

Go to the admin page. You'll see that the app is running, but CSS has not been properly applied yet.

Django Admin login page

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

How to Create HTML Templates in Django

Create HTML Templates

Defining Many-To-Many Relationships with ManyToManyField

Django Models – ManyToManyField

Restricting Page Access with LoginRequiredMixin

Login Required – LoginRequiredMixin

Integrating Bootstrap with Django Templates

Django Templates with Bootstrap

How to Create HTML Templates in Django

Create HTML Templates

Defining Many-To-Many Relationships with ManyToManyField

Django Models – ManyToManyField

Restricting Page Access with LoginRequiredMixin

Login Required – LoginRequiredMixin

Integrating Bootstrap with Django Templates

Django Templates with Bootstrap

Tags:

Daemon Process

App Deployment

Gunicorn

Service Unit

Socket Unit

systemctl

Unit Files

systemd

Django Introduction
Course Content

Chapter 1. Django Key Concepts

Web Framework and Django

Websites vs. Django Web Apps

How Django Handles HTTP Request and HTTP Response

Django's MVT Framework

Django Templates vs. Django APIs

Chapter 2. Django Quick Start Guide

Install Python

Install Visual Studio Code

Create Project Directory

Set Up Virtual Environment

Install Django

Start Django Project

Run Server

Database Migration

URL dispatcher – urls.py

Create Superuser and Log In to Django Admin

Start App

Create HTML Templates

Create Views

Add URL Patterns

Project vs. App

Chapter 3. Django Models and Databases

Create a Database in Django

Relational Database

Create Django Models

Makemigrations and Migrate

Add Models in Django Admin – admin.py

Change Display Name of Record Objects

Django Models – Data Field Type

Django Models – Field Options

Django Models – Help Text Option

Django Models – Choices Option

Django Models – DateField with datetime Module

Django Models – Relationship Fields

Django Models – ID

Django Models – ForeignKey (OneToMany Relationship)

Django Models – OneToOneField

Django Models – ManyToManyField

Chapter 4. Create CRUD Web Application

CRUD Web Application

Basic CRUD Structure in Django

Django Generic Views

How To Write Class-Based Views with Generic Views

Generic View Basic Attributes

URL Dispatcher for CRUD Views

Django Templates for CRUD Views

Django Template Language (DTL)

Template for List Page

get_FOO_display method

Template for Detail Page

Template with Model Relations

Template for Create and Update Page

Template for Delete Page

Add Links – {% url %} tag

Extend Templates – {% extends %} tag

Check Developing App UI on Mobile Device

Django Templates with Bootstrap

Crispy Forms

Customize Views (1) – Change List Order

Customizing Views (2) – Filter Lists

Context

Customize Views (3) – Add Extra Context

Modularize Templates – {% include %} tag

Static Files in Development Environment – {% static %} tag

STATIC_URL and STATICFILES_DIRS

Create Index HTML

Chapter 5. User Management

User Authentication

Overview of User Management Functions

User Management Function Development with Django

Approaches to Building User Management Functions in Django

Django Allauth (1) – Introduction

Django Allauth (2) – Installation and Initial Settings

Django Allauth (3) – Email Verification via Console

Django Allauth (4) – Email Verification via Gmail

Django Allauth (5) – Social Login with GitHub

Django Allauth (6) – Social Login with Google

Django Allauth (7) – Allauth Template File Setup

Django Allauth (8) – Add Basic Styling with Bootstrap and Crispy Forms

Django Allauth (9) – Customize Sign-in and Sign-up Pages

User Models

Login Required – LoginRequiredMixin

User Login Status Icon on Navigation Bar

Chapter 6. Deploy Django App

Overview of Django App Deployment (1)

Overview of Django App Deployment (2)

Key Steps of Django App Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Hosting Service Initial Settings (2) – SSH Remote Connection

Manage Local Computer and Remote Server Simultaneously

Tips for Managing Local Development and Remote Production Environment

Hosting Service Initial Settings (3) – Clone Project Directory with GitHub

Production Database Setup

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (2) – Production Settings

Django Production Settings (3) – django-environ and .env file

Static File Settings

Django and Dependency Installation on Production Server

Web Server and Application Server in Django

Application Server Setup – Gunicorn

Web Server Setup – Nginx

Domain Setup

SSL Setup – Certbot

Email Setting – SendGrid

Social Login for Production

Manage Local Development and Remote Production Environment

Chapter 1. Django Key Concepts

Web Framework and Django

Websites vs. Django Web Apps

How Django Handles HTTP Request and HTTP Response

Django's MVT Framework

Django Templates vs. Django APIs

Chapter 2. Django Quick Start Guide

Install Python

Install Visual Studio Code

Create Project Directory

Set Up Virtual Environment

Install Django

Start Django Project

Run Server

Database Migration

URL dispatcher – urls.py

Create Superuser and Log In to Django Admin

Start App

Create HTML Templates

Create Views

Add URL Patterns

Project vs. App

Chapter 3. Django Models and Databases

Create a Database in Django

Relational Database

Create Django Models

Makemigrations and Migrate

Add Models in Django Admin – admin.py

Change Display Name of Record Objects

Django Models – Data Field Type

Django Models – Field Options

Django Models – Help Text Option

Django Models – Choices Option

Django Models – DateField with datetime Module

Django Models – Relationship Fields

Django Models – ID

Django Models – ForeignKey (OneToMany Relationship)

Django Models – OneToOneField

Django Models – ManyToManyField

Chapter 4. Create CRUD Web Application

CRUD Web Application

Basic CRUD Structure in Django

Django Generic Views

How To Write Class-Based Views with Generic Views

Generic View Basic Attributes

URL Dispatcher for CRUD Views

Django Templates for CRUD Views

Django Template Language (DTL)

Template for List Page

get_FOO_display method

Template for Detail Page

Template with Model Relations

Template for Create and Update Page

Template for Delete Page

Add Links – {% url %} tag

Extend Templates – {% extends %} tag

Check Developing App UI on Mobile Device

Django Templates with Bootstrap

Crispy Forms

Customize Views (1) – Change List Order

Customizing Views (2) – Filter Lists

Context

Customize Views (3) – Add Extra Context

Modularize Templates – {% include %} tag

Static Files in Development Environment – {% static %} tag

STATIC_URL and STATICFILES_DIRS

Create Index HTML

Chapter 5. User Management

User Authentication

Overview of User Management Functions

User Management Function Development with Django

Approaches to Building User Management Functions in Django

Django Allauth (1) – Introduction

Django Allauth (2) – Installation and Initial Settings

Django Allauth (3) – Email Verification via Console

Django Allauth (4) – Email Verification via Gmail

Django Allauth (5) – Social Login with GitHub

Django Allauth (6) – Social Login with Google

Django Allauth (7) – Allauth Template File Setup

Django Allauth (8) – Add Basic Styling with Bootstrap and Crispy Forms

Django Allauth (9) – Customize Sign-in and Sign-up Pages

User Models

Login Required – LoginRequiredMixin

User Login Status Icon on Navigation Bar

Chapter 6. Deploy Django App

Overview of Django App Deployment (1)

Overview of Django App Deployment (2)

Key Steps of Django App Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Hosting Service Initial Settings (2) – SSH Remote Connection

Manage Local Computer and Remote Server Simultaneously

Tips for Managing Local Development and Remote Production Environment

Hosting Service Initial Settings (3) – Clone Project Directory with GitHub

Production Database Setup

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (2) – Production Settings

Django Production Settings (3) – django-environ and .env file

Static File Settings

Django and Dependency Installation on Production Server

Web Server and Application Server in Django

Application Server Setup – Gunicorn

Web Server Setup – Nginx

Domain Setup

SSL Setup – Certbot

Email Setting – SendGrid

Social Login for Production

Manage Local Development and Remote Production Environment