Chapter 6. Deploy Django App

Django and Dependency Installation on Production Server

Django and Dependency Installation on Production Server
Tag:

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'.

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'.
Tag: