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

Web Server Setup – Nginx

Web Server Setup – Nginx

Configuring Nginx as a Web Server for Django

Nginx is one of the most popular web servers. With a focus on static content handling, Nginx can be more scalable and faster than other web servers. This lesson will explain how to set up Nginx for Django app deployment.

There are five steps to set up Nginx:

  1. Install Nginx
  2. Add an Nginx configuration file to sites-available
  3. Add a symbolic link of the configuration file to sites-enabled
  4. Update static file location
  5. Adjust firewall settings
  6. Check if the Django app is running

Note: For the Nginx setting, the virtual environment is not needed as it's directly installed on the OS.

Install Nginx

Install Nginx using the apt-get command.

Command Line - INPUT
sudo apt-get update
sudo apt-get install -y nginx

As Nginx runs when it is installed, check if it is running by the systemctl status command.

Command Line - INPUT
systemctl status nginx

Unless you encounter an issue, you'll see the message below.

Command Line - RESPONSE
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-04-21 16:05:47 UTC; 21h ago
Docs: man:nginx(8)
Process: 123945 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (cod>
:

Add an Nginx configuration file

Nginx has several configuration setting points.

  1. nginx.conf file: the original settings are written here. Usually, we don't touch this file.
  2. conf.d directory: you can save your custom configuration file in this directory. The nginx.conf file will include your custom configuration. As the configuration files saved in this directory are always read by Nginx, there is limited flexibility to switch configurations.
  3. sites-available directory: this is another directory where you can save your custom configuration files; however, Nginx does not read the files directly. To enable the configuration, you need to create a symbolic link of the file in the sites-enabled directory. By following this rule, you can easily switch configuration files by controlling symbolic links.
  4. sites-enabled directory: the location where you can put the symbolic link of the configuration file that was created in the sites-available directory.

To create a new configuration file, run the command below.

Command Line - INPUT
sudo vim /etc/nginx/sites-available/project_d

Edit the file like in the example below.

/etc/nginx/sites-available/ project_d
server {
listen 80;
server_name xx.xx.xx.xx;

  location = /favicon.ico { access_log off; log_not_found off;}
  location /static{
  alias /usr/share/nginx/html/static;
}

location / {
  include proxy_params;
  proxy_pass http://unix:/home/ubuntu/project_d/project_d.sock;
  }
}

There are three key parts that you need to type carefully.

  1. server name: the static IP address attached to the Ubuntu instance
  2. location /static: the address should be aligned with STATIC_ROOT
  3. proxy_pass: the path of the sock file defined in the Gunicorn service and socket unit files in the previous section

Add a symbolic link and verify the syntax

To enable the new settings, you need to create a symbolic link of the file in the sites-enabled directory. Run the command below to create the symbolic link.

Command Line - INPUT
sudo ln -s /etc/nginx/sites-available/project_d /etc/nginx/sites-enabled 

Use the Nginx command to check if the configuration file is aligned with the Nginx syntax.

Command Line - INPUT
sudo nginx -t

If the configuration file is properly written, you'll see the message below.

Command Line - RESPONSE
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Update static file location

Update the static file location to the directory that Nginx usually uses. To update the location, edit STATIC_ROOT in the settings file and run the collectstatic command.

Update STATIC_ROOT to the directory path we used in the Nginx configuration file.

sconfig/settings/production.py
:
STATIC_ROOT = '/usr/share/nginx/html/static'

Create the static directory and change the owner to ubuntu (the user of the instance). Then, run the collectstatic command under the virtual environment.

Command Line - INPUT
sudo mkdir /usr/share/nginx/html/static
sudo chown -R ubuntu /usr/share/nginx/html/static
cd ~/project_d
source d_env/bin/activate
Command Line - INPUT
python manage.py collectstatic

You'll see that the static files are successfully copied to the new directory.

Command Line - RESPONSE
138 static files copied to '/usr/share/nginx/html/static'

As the old static file directory is not needed anymore, delete it.

Command Line - INPUT
rm -r collectstatic_path

Firewall settings

This process is optional as AWS Lightsail has its firewall. On Ubuntu OS, you can use UFW (Uncomplicated Firewall) to manage iptables.

To enable UFW, run the command below.

Command Line - INPUT
sudo ufw enable

You'll see the message below. This explains the importance of opening an SSH port to keep the SSH connection.

Command Line - INTERACTIVE
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

Open the SSH port and ports for Nginx by running the following commands.

Command Line - INPUT
sudo ufw allow ssh
sudo ufw allow 'Nginx Full'

Run the status command to check the status.

Command Line - INPUT
sudo ufw status

You can see that the UFW is active now with two allowed ports settings.

Command Line - RESPONSE
Status: active

To Action From
-- ------ ----
22 ALLOW Anywhere 
Nginx Full ALLOW Anywhere 

For more details about UFW, please check our 'Linux OS Introduction' course.

Linux OS Introduction 'UFW (Uncomplicated Firewall)'

Check if the app is running

If all settings are properly done, you'll be able to see your app through the static IP address.

Index page UI example

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Setting Up Gunicorn as Your Django Application Server

Application Server Setup – Gunicorn

Decision Making in Django App Deployment

Overview of Django App Deployment (2)

Setting Up Gunicorn as Your Django Application Server

Application Server Setup – Gunicorn

Decision Making in Django App Deployment

Overview of Django App Deployment (2)

Tags:

Symbolic Link

Vim

UFW

App Deployment

systemctl

Unit Files

Nginx conf

sites-available

sites-enabled

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