Installing the Back-end from Source

Installing the Requirements

First things first, install the necessary dependencies and packages, and create the installation directory.

Below you’ll also need to set your BACKEND_URL, this is very important in production.

WORK_PATH="/var/lib/interlock"
BACKEND_PATH="/var/lib/interlock/interlock_backend"
BACKEND_URL=""
# E.g.: interlock-backend.example.com

# Install dependencies
apt-get update -y
apt-get install git python3 python3-virtualenv python3-pip postgresql nginx libpq-dev -y

# Create the install directory parent and the sslcerts directory
mkdir -p "$WORK_PATH/sslcerts"

Once you’ve installed all the requirements, you can pull the latest version of the repo.

git clone https://github.com/dblanque/interlock_backend $BACKEND_PATH

Creating the Database (PostgreSQL)

After you’ve cloned the Git repo you need to create your PSQL database.

# SHELL CONSOLE
su postgres
psql
/* PSQL */
CREATE ROLE interlockadmin WITH PASSWORD 'password'; -- ! CHANGE THIS !
CREATE DATABASE interlockdb;
ALTER ROLE interlockadmin WITH LOGIN;
ALTER DATABASE interlockdb OWNER to interlockadmin;
# Create local config file
touch "$BACKEND_PATH/interlock_backend/local_django_settings.py"
nano "$BACKEND_PATH/interlock_backend/local_django_settings.py"
# file: interlock_backend/local_django_settings.py
DATABASES = {
	"default": {
		"ENGINE": "django.db.backends.postgresql",
		"NAME": "interlockdb",
		"USER": "interlockadmin",
		"PASSWORD": "KoenigCc850",  # Change this password
		"HOST": "127.0.0.1",  # Or an IP Address that your DB is hosted on
		"PORT": "5432",
	}
}

FRONT_URL = "interlock-test.brconsulting.info"

# CORS and CSRF overrides
CORS_ALLOWED_ORIGINS = [
	# ! DEV
	"http://127.0.0.1",
	# PRODUCTION
	f"http://{FRONT_URL}",
	f"https://{FRONT_URL}",
]
CSRF_TRUSTED_ORIGINS = [
	# ! DEV
	"http://127.0.0.1",
	# PRODUCTION
	f"http://{FRONT_URL}",
	f"https://{FRONT_URL}",
]

Setting up the Virtual Environment

Once the database has been created and setup you can install the python requirements, inside the virtual environment that we’ll create below.

# Create the Virtual Env
virtualenv -p python3 $BACKEND_PATH

# Activate the Virtual Env
source "$BACKEND_PATH/bin/activate"

# Install Poetry
pip install poetry

# Install the Requirements
poetry install

Creating an SSL Certificate

Once the requirements are installed you’ll need to create an SSL Certificate (or use your own in the corresponding path) to run the application.

sudo openssl req -x509 -subj "/CN=$(hostname)/" -nodes -days 36500 -newkey rsa:2048 -keyout "$WORK_PATH/sslcerts/privkey.pem" -out "$WORK_PATH/sslcerts/fullchain.pem"

Setting up the systemd service

After this has been completed successfully, you can create a link of the Systemd service unit and start it:

# Create a symbolic link
ln -s "$BACKEND_PATH/interlock_backend/install/interlock-backend.service" /etc/systemd/system/interlock-backend.service

# Restart Daemon
systemctl daemon-reload

# Enable the service
systemctl enable interlock-backend

# Start the service
systemctl start interlock-backend

Local Configuration

Refer to the following files for local configuration:

  • interlock_backend/local_django_settings_sample.py
  • interlock_backend/settings.py