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.
apt
or apt-get
command for installation.venv
command to create another virtual environment on the production server first, and run the pip
command with requirement.txt.This lesson will cover the third step with a quick check of the Python version.
To check if Python 3 is already installed in your instance, run the python3 --version
command.
python3 --version
Python 3.8.10
If it's not installed, you can install the latest available version on Ubuntu by running the command below.
sudo apt update
sudo apt install python3
To replicate the same environment as the one on your local computer, create a virtual environment first.
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.
cd ~/project_d
python3 -m venv d_env
Activate the virtual environment using the source command.
source d_env/bin/activate
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:
Django==4.1.7
django-crispy-forms
crispy-bootstrap5
django-allauth
django-environ
gunicorn
psycopg2-binary
Finally, you can install Django and dependencies by running the pip
command using requirements.txt.
pip install -r requirements.txt
If you successfully installed them, you'll see a message like the one below.
:
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
No database is migrated at this stage, and no superuser is created. Run the as-usual commands on the server.
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
When you run the migrate command, you may encounter an error like the one shown below.
...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.
ALTER DATABASE project_d OWNER TO project_d_user;
When it is successful, the command line returns the message below.
ALTER DATABASE
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.
:
STATIC_ROOT = BASE_DIR / 'collectstatic_path'
Then, run the collectstatic command.
python manage.py collectstatic
If there is no error, you'll see the message like the one below.
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.
apt
or apt-get
command for installation.venv
command to create another virtual environment on the production server first, and run the pip
command with requirement.txt.This lesson will cover the third step with a quick check of the Python version.
To check if Python 3 is already installed in your instance, run the python3 --version
command.
python3 --version
Python 3.8.10
If it's not installed, you can install the latest available version on Ubuntu by running the command below.
sudo apt update
sudo apt install python3
To replicate the same environment as the one on your local computer, create a virtual environment first.
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.
cd ~/project_d
python3 -m venv d_env
Activate the virtual environment using the source command.
source d_env/bin/activate
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:
Django==4.1.7
django-crispy-forms
crispy-bootstrap5
django-allauth
django-environ
gunicorn
psycopg2-binary
Finally, you can install Django and dependencies by running the pip
command using requirements.txt.
pip install -r requirements.txt
If you successfully installed them, you'll see a message like the one below.
:
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
No database is migrated at this stage, and no superuser is created. Run the as-usual commands on the server.
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
When you run the migrate command, you may encounter an error like the one shown below.
...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.
ALTER DATABASE project_d OWNER TO project_d_user;
When it is successful, the command line returns the message below.
ALTER DATABASE
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.
:
STATIC_ROOT = BASE_DIR / 'collectstatic_path'
Then, run the collectstatic command.
python manage.py collectstatic
If there is no error, you'll see the message like the one below.
138 static files copied to '/home/ubuntu/project_d/collectstatic_path'.