Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Server deployment

Jasper edited this page May 17, 2023 · 1 revision

Setting up the server environment

Accounts

The account provided by the course organisers (selab2) was only used to set up a user account for Jasper (jasper). Mainly so I can use my own sudo password.

Installed packages

(At least) The following packages were installed on the UGent server:

  • docker, using apt
  • docker-compose, manually (through this process):
    • sudo curl -L "https://github.com/docker/compose/releases/download/v2.16.0/docker-compose-linux-x86_64" -o /usr/local/bin/docker-compose
    • sudo chmod +x /usr/local/bin/docker-compose
    • sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  • certbot, using snap
  • git

Docker IP range

The default IP range used internally by docker is the same as the Eduroam IP range, causing conflicts. The docker IP range on the UGent server was changed following this answer. A new file /etc/docker/daemon.json was created with the following content:

{
  "bip": "10.200.0.1/24",
  "default-address-pools":[
    {"base":"10.201.0.0/16","size":24},
    {"base":"10.202.0.0/16","size":24}
  ]
}

Certbot

was used to setup https. It automatically renews our certificates, but needs the folder srv/Dr-Trottoir-2/certbot-webroot to do so. Removing that folder will stop the automatic renewal. The certbot certificates are located at /etc/letsencrypt/live/.... Docker provides these to NGINX.

Running the server

To start the server: Navigate to the project root, then run docker-compose -f docker-compose.prod.yml up -d --build. Surf to localhost and localhost/admin to check if everything is working.

This will do the following things:

  • Start gunicorn to host the deployed version of the django project
  • Build and start the next.js server (this hosts it's own static files)
  • Init and start the postgres database
  • Start nginx

Then, to configure django:

  • docker exec -it DrTrottoir-be python manage.py migrate
  • docker exec -it DrTrottoir-be python manage.py collectstatic

Initialize the db with dummy data and a superuser:

  • docker exec -it DrTrottoir-be python manage.py loaddata fixtures/prod_init_data.json
  • docker exec -it DrTrottoir-be python manage.py createsuperuser

When trying to diagnose django, this is helpful: https://stackoverflow.com/questions/15128135/setting-debug-false-causes-500-error

Orphans

Docker-compose somehow always 'loses' the NGINX container. So, when bringing the containers down or restarting them, always use --remove-orphans with the docker-compose command. docker-compose down --remove-orphans or docker-compose down -v --remove-orphans

.env files

The production environment requires three more .env files, described below:

.env.prod

DEBUG=0
SECRET_KEY=<chosen secret key>
DJANGO_ALLOWED_HOSTS="localhost 127.0.0.1 [::1] <url of server>"
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=<chosen db name>
SQL_USER=<chosen db username>
SQL_PASSWORD=<chosen db password>
SQL_HOST=db
SQL_PORT=5432
DATABASE=postgres

.env.prod.db

POSTGRES_USER=<chosen db username>
POSTGRES_PASSWORD=<chosen db password>
POSTGRES_DB=<chosen db name>

frontend/.env.local

NEXTAUTH_URL=<address of the next auth, such as: https://your-domain.com/next/auth>
NEXTAUTH_URL_INTERNAL=http://next:3000/next/auth
NEXTAUTH_SECRET=<any secret key string>
NEXT_PUBLIC_GOOGLE_API=<a key for the google maps embed API>
NEXT_PUBLIC_API_URL=http://localhost:8000/api/