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

Django and Dependency Installation on Production Server

Django and Dependency Installation on Production Server

Django Dependency Installation on Production Servers

If you are a beginner, you may not know how to build the production environment and which code should be transferred from the local computer to the production server. There are four groups of code layers in the Django app.

  1. OS: On the local computer, you may be using Mac OS or Windows. On the server, LinuxOS is the most popular choice. We have already prepared Ubuntu OS in AWS Lightsail at the beginning of this chapter.
  2. Programs directly installed onto the production server: Python3, database software (e.g., PosgreSQL), and web server software (e.g., Nginx) should be directly installed onto the production server. For Ubuntu OS, use apt or apt-get command for installation.
  3. Programs installed in the virtual environment: Django itself, gunicorn and other Django libraries should be installed in the virtual environment to make sure the consistency of dependencies. Use the venv command to create another virtual environment on the production server first, and run the pip command with requirement.txt.
  4. Project code (your code): This is the code you developed on the local computer. We have already explained how to transfer the code to the production server using GitHub at the beginning of this chapter.

This lesson will cover the third step with a quick check of the Python version.

Confirm Python3 is installed on the instance

To check if Python 3 is already installed in your instance, run the python3 --version command.

Command Line - INPUT
python3 --version
Command Line - RESPONSE
Python 3.8.10

If it's not installed, you can install the latest available version on Ubuntu by running the command below.

Command Line - INPUT
sudo apt update
sudo apt install python3

Install and create a virtual environment

To replicate the same environment as the one on your local computer, create a virtual environment first.

Command Line - INPUT
sudo apt install -y python3-venv

Go to the project directory and run the venv command. We use d_env as a virtual environment name.

Command Line - INPUT
cd ~/project_d
python3 -m venv d_env

Activate the virtual environment using the source command.

Command Line - INPUT
source d_env/bin/activate

Prepare a requirements file for production

As additional packages need to be installed for production, we must add them to the requirements.txt file. There is a technique to structure the requirement files for development and production nicely. We will explain it later in this chapter.

On top of the current listed dependencies, you need to add:

  • gunicorn (application server)
  • psycopg2-binary (adapter of PosgreSQL)
requirements.txt
Django==4.1.7
django-crispy-forms
crispy-bootstrap5
django-allauth
django-environ
gunicorn
psycopg2-binary

Install Django and dependencies

Finally, you can install Django and dependencies by running the pip command using requirements.txt.

Command Line - INPUT
pip install -r requirements.txt

If you successfully installed them, you'll see a message like the one below.

Command Line - RESPONSE
 :
Successfully installed Django-4.1.7 asgiref-3.6.0 backports.zoneinfo-0.2.1 certifi-2022.12.7 cffi-1.15.1 charset-normalizer-3.1.0 crispy-bootstrap5-0.7 cryptography-40.0.2 defusedxml-0.7.1 django-allauth-0.54.0 django-crispy-forms-2.0 django-environ-0.10.0 gunicorn-20.1.0 idna-3.4 oauthlib-3.2.2 psycopg2-binary-2.9.6 pycparser-2.21 pyjwt-2.6.0 python3-openid-3.2.0 requests-2.28.2 requests-oauthlib-1.3.1 sqlparse-0.4.4 urllib3-1.26.15

Run the Django setup commands

No database is migrated at this stage, and no superuser is created. Run the as-usual commands on the server.

Command Line - INPUT
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser

IdeaNote: Database Owner

When you run the migrate command, you may encounter an error like the one shown below.

Command Line - RESPONSE
...django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (permission denied for schema public
LINE 1: CREATE TABLE "django_migrations" ("id" bigint NOT NULL PRIMA...

This may be because of the database owner setting for PostgreSQL. This setting was not required in the older version of PostgreSQL, but it is required in the recent versions. To set it up, run the command below after entering the PostgreSQL prompt.

Command Line - INPUT
ALTER DATABASE project_d OWNER TO project_d_user;

When it is successful, the command line returns the message below.

Command Line - RESPONSE
ALTER DATABASE

Set STARTIC_ROOT and run the collectstatic command

For the production environment, you need to set the static file location (STATIC_ROOT) and collect static files by running the collectstatic command.

In our case example, set the directory in BASE_DIR / 'collectstatic_path' for now. Add the setting in the settings file. This setting will be adjusted when we establish the Nginx server.

config/settings/production.py
:
STATIC_ROOT = BASE_DIR / 'collectstatic_path'

Then, run the collectstatic command.

Command Line - INPUT
python manage.py collectstatic

If there is no error, you'll see the message like the one below.

Command Line - RESPONSE
138 static files copied to '/home/ubuntu/project_d/collectstatic_path'.

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Initial Setup with AWS Lightsail for Django Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Displaying Human-Readable Choices in Django

get_FOO_display method

Initial Setup with AWS Lightsail for Django Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Displaying Human-Readable Choices in Django

get_FOO_display method

Tags:

requirements.txt

Production Environment

App Deployment

Gunicorn

collectstatic

PosgreSQL

STARTIC_ROOT

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