Skip to content

ScratchPad

Edward Baitsewe edited this page Feb 16, 2025 · 1 revision

Scratch Pad

Create Shared Proxy

You can use a single, centralized nginx-proxy container to manage your applications. This container will act as a reverse proxy and route traffic based on the subdomain to the correct application.

Create Docker Compose

From the root directory of your VPS create a folder called shared proxy.

mkdir shared-proxy

Enter the directory

cd shared-proxy

Make a file called docker-compose.yml

touch docker-compose.yml

Enter file with VIM or NANO and enter contents.

vi docker.compose.yml

Enter contents

services:
  nginx-proxy:
    container_name: nginx-proxy
    image: nginxproxy/nginx-proxy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./nginx/html:/usr/share/nginx/html
      - ./nginx/certs:/etc/nginx/certs
      - ./nginx/vhost:/etc/nginx/vhost.d
      - ./nginx/acme:/etc/acme.sh
    networks:
      - proxy-network

  letsencrypt-companion:
    container_name: letsencrypt-companion
    image: jrcs/letsencrypt-nginx-proxy-companion
    restart: unless-stopped
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:z
      - ./nginx/acme:/etc/acme.sh:rw
    networks:
      - proxy-network
    environment:
      DEFAULT_EMAIL: <yourEmail>

networks:
  proxy-network:
    external: true

Create Proxy Network

Create the proxy-network Docker network by running this bash command (only once):

docker network create proxy-network

(back to top)

Create Vite Svelte Docker Compose

This is an example of how you may set up your vite project locally. Here we are setting up this app: https://github.com/monatemedia/svelte-counter

Create Docker Compose

From the root directory of your VPS create a folder called svelte-counter

mkdir svelte-counter

Enter the directory

cd svelte-counter

Make a file called docker-compose.yml

touch docker-compose.yml

Enter file with VIM or NANO and enter contents.

services:
  svelte-counter:
    container_name: svelte-counter
    image: ghcr.io/monatemedia/svelte-counter:latest
    environment:
      VIRTUAL_HOST: monatehub.monatemedia.com
      LETSENCRYPT_HOST: monatehub.monatemedia.com
    networks:
      - proxy-network

networks:
  proxy-network:
    external: true

Save and close the the file

(back to top)

Create Python Django Docker Project

This is an example of how you may set up your python django project locally. Here we are setting up this app: https://github.com/monatemedia/python-django-achievementhq

Create Docker Compose

From the root directory of your VPS create a folder called python-django-achievementhq

mkdir python-django-achievementhq

Enter the directory

cd python-django-achievementhq

Make a file called docker-compose.yml

touch docker-compose.yml

Enter file with VIM or NANO and enter contents.

services:
  achievementhq_db:
    image: postgres:latest
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - proxy-network

  achievementhq_web:
    image: ghcr.io/monatemedia/python-django-achievementhq:latest
    restart: unless-stopped
    env_file:
      - .env
    environment:
      VIRTUAL_HOST: achievementhq.monatemedia.com
      LETSENCRYPT_HOST: achievementhq.monatemedia.com
    networks:
      - proxy-network

volumes:
  postgres_data:

networks:
  proxy-network:
    external: true

Save and close the file

Start Centralized Proxy and Applications

Start the centralized nginx-proxy setup:

cd ~
cd shared-proxy
docker compose up

Start Each Application

cd /svelte-counter
docker compose up -d

cd /python-django-achievementhq
docker compose up -d

For more examples, please refer to the Documentation

(back to top)

Create Automation Scripts

You can make management of your various application scripts easier by creating scripts.

Create a Start Script

Navigate to your home directory

cd ~

Create a new shell script, start-all.sh

touch start-all.sh

Open the shell script

vi start-all.sh

Add the following lines to the start-all.sh file. Replace the paths with the actual paths to your docker-compose.yml files:

#!/bin/bash

echo "Starting centralized proxy..."
cd /shared-proxy
docker compose up -d

echo "Starting svelte-counter app..."
cd /svelte-counter
docker compose up -d

echo "Starting python-django-achievementhq app..."
cd /python-django-achievementhq
docker compose up -d

echo "All services started!"

Save and close the VIM editor

Make the script executable by running:

chmod +x start-all.sh

Run the script

start-all.sh

Create a Stop Script

Navigate to your home directory

cd ~

Create a new shell script, stop-all.sh

touch stop-all.sh

Open the shell script

vi stop-all.sh

Add the following lines to the start-all.sh file. Replace the paths with the actual paths to your docker-compose.yml files:

#!/bin/bash

echo "Stopping python-django-achievementhq app..."
cd /python-django-achievementhq
docker compose down

echo "Stopping svelte-counter app..."
cd /svelte-counter
docker compose down

echo "Stopping centralized proxy..."
cd /shared-proxy
docker compose down

echo "All services stopped!"

Save and close the VIM editor

Make the script executable by running:

chmod +x stop-all.sh

Run the script

stop-all.sh

Create a Reboot Script

Navigate to your home directory

cd ~

Create a new shell script, reboot-all.sh

touch reboot-all.sh

Open the shell script

vi reboot-all.sh

Add the following lines to the start-all.sh file. Replace the paths with the actual paths to your docker-compose.yml files:

#!/bin/bash

# Navigate to each project directory and restart the services
cd /path/to/svelte-counter
docker compose down && docker compose up -d

cd /path/to/python-django-achievementhq
docker compose down && docker compose up -d

# Add more directories as needed following the same pattern

echo "All services have been restarted successfully."

Save and close the VIM editor

Make the script executable by running:

chmod +x reboot-all.sh

Run the script

reboot-all.sh

Optional: Run on Server Reboot

If you want this script to run automatically when the server reboots:

edit your crontab

crontab -e

Add the following line to run the script at startup:

@reboot reboot-all.sh