Chapter 6. Deploy Django App

Key Steps of Django App Deployment

Key Steps of Django App Deployment
Tag:

There are eight key steps to deploy a Django app from scratch. If you are handling app deployment for the first time, it may take time to go through all the processes, but once you have familiarized yourself with them, you can manage the deployment a lot faster. As there are many potential pitfalls in the deployment process, we try to explain it in as much detail as possible in this chapter. Here is an overview of the eight key steps in the Django app development. You don't need to understand everything now. We'll go through them one by one in this chapter.

  1. Hosting Service Initial Settings
  2. Production Database Setup – PostgreSQL setup
  3. Django App Production Configuration – Edit settings.py
  4. Django and Dependency Installation
  5. Application and Web Server Setup
  6. Domain Setup
  7. SSL Setup and Port Setting – Certbot setup
  8. Social Login and Email Settings

1. Hosting Service Initial Settings

Once you decide on a hosting service (e.g., AWS Lightsail) for your Django app, you need to sign up for the hosting service and set up a server (instance) with a specific OS (e.g., Ubuntu).

As the default IP address is usually a dynamic IP address, you need to get a static IP address and attach it to the instance.

You also need to connect your local computer to the server using SSH to control the server from your local computer.

You can use VS Code to edit code remotely in the GUI environment. To transfer your code from the local computer to the server, GitHub (a git repository platform) is often used.

At this stage, you also need to confirm that a correct Python version has been installed on the server.

2. Production Database Setup – PostgreSQL setup

To change a database from the default database SQLite, you need to install database software that you want to use in the production stage (e.g., PostgreSQL).

You also need an initial setup for the database.

3. Django App Production Configuration – Edit settings.py

In the production stage, you need to adjust several development configurations.

You'll need to continue using two settings – one for development and one for production.

One of the approaches to handle two or multiple settings is to create multiple settings files for switching configurations between the development environment and production environment.

To manage highly confidential information, such as passwords or secret keys, you need to make an .env file and save the information separately from the settings.py file.

4. Django and Dependency Installation

Django software is usually not preinstalled on a server, so you need to install it for deployment.

To make sure that the computing environment is the same as the one on your local computer, you need to install all required software under the virtual environment (in the status where venv is activated).

You also need to install the required libraries for your app. For example, gunicorn for an application server and psycopg2-binary for a PostgreSQL adapter.

5. Application and Web Server Setup

Application Server Setup (Gunicorn)

As Gunicorn (an application server) runs in a different process from Django on Linux OS, you need to make additional settings to run it.

  • Create two unit files (gunicorn.socket and gunicorn.service)
  • Enable the unit files using the systemctl command.

With these settings, Gunicorn will automatically start and run as a background job when the server boots.

Web Server Setup (Nginx setup)

If Nginx is not preinstalled, you need to install it first.

To customize the Nginx configuration to your development environment and the Djngo app, you need to create a configuration file under /etc/nginx/sites-available/.

As Nginx handles static files directly, so you only need to define the static file path in the configuration file.

The static file path should be the same as the one defined by STATIC_ROOT in settings.py.

To enable the configuration, you need to create a symbolic link of the configuration file under /etc/nginx/sites-enabled/.

6. Domain Setup

You may want to make your web application accessible through domain name (not through IP address only). To convert an IP address to domain name, you need to register a domain name and add a DNS record in the DNS server. As Django limits host names that can access the application, you need to edit ALLOWED_HOSTS in the setting file for production along with the Nginx configuration adjustment.

7. SSL Setup and Port Setting – Certbot setup

To build a more secured communication using HTTPS(Hypertext Transfer Protocol Secure), you need to set up SSL (Secure Sockets Layer). Certbot is a useful tool for setting up SSL. It is a free open-source software tool that automatically provides a Let's Encrypt certificate used for SSL communication. As ports for HTTP and HTTPS are different, you need to change port settings to open in the firewall setting.

8. Social Login and Email Settings

Lastly, you need to adjust social login settings as you start to use a different host address. You need to register it in each social login provider's platform and Django admin. Also, for the production, there are some platforms that can send machine-generated emails. Using one of the email platforms is preferable from security and reliability point of view. SendGrid is one of the popular choices.

There are eight key steps to deploy a Django app from scratch. If you are handling app deployment for the first time, it may take time to go through all the processes, but once you have familiarized yourself with them, you can manage the deployment a lot faster. As there are many potential pitfalls in the deployment process, we try to explain it in as much detail as possible in this chapter. Here is an overview of the eight key steps in the Django app development. You don't need to understand everything now. We'll go through them one by one in this chapter.

  1. Hosting Service Initial Settings
  2. Production Database Setup – PostgreSQL setup
  3. Django App Production Configuration – Edit settings.py
  4. Django and Dependency Installation
  5. Application and Web Server Setup
  6. Domain Setup
  7. SSL Setup and Port Setting – Certbot setup
  8. Social Login and Email Settings

1. Hosting Service Initial Settings

Once you decide on a hosting service (e.g., AWS Lightsail) for your Django app, you need to sign up for the hosting service and set up a server (instance) with a specific OS (e.g., Ubuntu).

As the default IP address is usually a dynamic IP address, you need to get a static IP address and attach it to the instance.

You also need to connect your local computer to the server using SSH to control the server from your local computer.

You can use VS Code to edit code remotely in the GUI environment. To transfer your code from the local computer to the server, GitHub (a git repository platform) is often used.

At this stage, you also need to confirm that a correct Python version has been installed on the server.

2. Production Database Setup – PostgreSQL setup

To change a database from the default database SQLite, you need to install database software that you want to use in the production stage (e.g., PostgreSQL).

You also need an initial setup for the database.

3. Django App Production Configuration – Edit settings.py

In the production stage, you need to adjust several development configurations.

You'll need to continue using two settings – one for development and one for production.

One of the approaches to handle two or multiple settings is to create multiple settings files for switching configurations between the development environment and production environment.

To manage highly confidential information, such as passwords or secret keys, you need to make an .env file and save the information separately from the settings.py file.

4. Django and Dependency Installation

Django software is usually not preinstalled on a server, so you need to install it for deployment.

To make sure that the computing environment is the same as the one on your local computer, you need to install all required software under the virtual environment (in the status where venv is activated).

You also need to install the required libraries for your app. For example, gunicorn for an application server and psycopg2-binary for a PostgreSQL adapter.

5. Application and Web Server Setup

Application Server Setup (Gunicorn)

As Gunicorn (an application server) runs in a different process from Django on Linux OS, you need to make additional settings to run it.

  • Create two unit files (gunicorn.socket and gunicorn.service)
  • Enable the unit files using the systemctl command.

With these settings, Gunicorn will automatically start and run as a background job when the server boots.

Web Server Setup (Nginx setup)

If Nginx is not preinstalled, you need to install it first.

To customize the Nginx configuration to your development environment and the Djngo app, you need to create a configuration file under /etc/nginx/sites-available/.

As Nginx handles static files directly, so you only need to define the static file path in the configuration file.

The static file path should be the same as the one defined by STATIC_ROOT in settings.py.

To enable the configuration, you need to create a symbolic link of the configuration file under /etc/nginx/sites-enabled/.

6. Domain Setup

You may want to make your web application accessible through domain name (not through IP address only). To convert an IP address to domain name, you need to register a domain name and add a DNS record in the DNS server. As Django limits host names that can access the application, you need to edit ALLOWED_HOSTS in the setting file for production along with the Nginx configuration adjustment.

7. SSL Setup and Port Setting – Certbot setup

To build a more secured communication using HTTPS(Hypertext Transfer Protocol Secure), you need to set up SSL (Secure Sockets Layer). Certbot is a useful tool for setting up SSL. It is a free open-source software tool that automatically provides a Let's Encrypt certificate used for SSL communication. As ports for HTTP and HTTPS are different, you need to change port settings to open in the firewall setting.

8. Social Login and Email Settings

Lastly, you need to adjust social login settings as you start to use a different host address. You need to register it in each social login provider's platform and Django admin. Also, for the production, there are some platforms that can send machine-generated emails. Using one of the email platforms is preferable from security and reliability point of view. SendGrid is one of the popular choices.

Tag: