From 1d11d346ea4d6778e13c41a4d121e504bb18f4f9 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sat, 22 Oct 2022 01:44:49 +0200 Subject: [PATCH 01/34] scripts --- run/acme.sh | 6 ++++-- run/build.sh | 7 ------- run/data.sh | 6 ++++++ run/get-repo.sh | 2 -- run/services/postgresql.sh | 6 ++++++ run/services/redis.sh | 8 ++++++++ run/setup.sh | 12 +++++++++++- 7 files changed, 35 insertions(+), 12 deletions(-) delete mode 100644 run/build.sh create mode 100644 run/data.sh delete mode 100644 run/get-repo.sh create mode 100644 run/services/postgresql.sh create mode 100644 run/services/redis.sh diff --git a/run/acme.sh b/run/acme.sh index 75f4a24..37c7a72 100644 --- a/run/acme.sh +++ b/run/acme.sh @@ -1,3 +1,4 @@ +# `acme/acme.json` file for storing https certificates DIR=acme FILE=acme/acme.json @@ -7,5 +8,6 @@ fi if [ ! -f "$FILE" ]; then touch "$FILE" - chmod 600 "$FILE" -fi \ No newline at end of file +fi + +chmod 600 "$FILE" \ No newline at end of file diff --git a/run/build.sh b/run/build.sh deleted file mode 100644 index a910c6c..0000000 --- a/run/build.sh +++ /dev/null @@ -1,7 +0,0 @@ -FILES="-f docker-compose.yml -f docker-compose.nextjs-tailwindcss.yml" - -# first setup -sh run/setup.sh - -# building containers -docker-compose $FILES build && docker-compose $FILES up -d \ No newline at end of file diff --git a/run/data.sh b/run/data.sh new file mode 100644 index 0000000..bc00772 --- /dev/null +++ b/run/data.sh @@ -0,0 +1,6 @@ +# `data/` folder for storing DB, Redis, etc. data +DIR=data + +if [ ! -d "$DIR" ]; then + mkdir data +fi \ No newline at end of file diff --git a/run/get-repo.sh b/run/get-repo.sh deleted file mode 100644 index c1d2d9d..0000000 --- a/run/get-repo.sh +++ /dev/null @@ -1,2 +0,0 @@ -rm -rf ../$1 -git clone https://github.com/starters-dev/$1.git ../$1 \ No newline at end of file diff --git a/run/services/postgresql.sh b/run/services/postgresql.sh new file mode 100644 index 0000000..1f9e97e --- /dev/null +++ b/run/services/postgresql.sh @@ -0,0 +1,6 @@ +# -- creating folder for PostgreSQL +DIR=data/postgresql + +if [ ! -d "$DIR" ]; then + mkdir data/postgresql +fi \ No newline at end of file diff --git a/run/services/redis.sh b/run/services/redis.sh new file mode 100644 index 0000000..c1b21b1 --- /dev/null +++ b/run/services/redis.sh @@ -0,0 +1,8 @@ +# -- creating folder for Redis +DIR=data/redis + +if [ ! -d "$DIR" ]; then + mkdir data/redis +fi + +sudo chown -R 1001:1001 data/redis \ No newline at end of file diff --git a/run/setup.sh b/run/setup.sh index 75fec29..325e3bc 100644 --- a/run/setup.sh +++ b/run/setup.sh @@ -1 +1,11 @@ -bash run/acme.sh \ No newline at end of file +# -- running system bash files +bash run/acme.sh +bash run/data.sh + +# -- running services' bash files +BASH_FILES="" +for F in `find ./run/services -type f -maxdepth 1 -name "*.sh"` +do + BASH_FILES="$BASH_FILES bash $F;" +done +eval $BASH_FILES \ No newline at end of file From 479d2047126c57b71304463047bed3ab9c3fc154 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sat, 22 Oct 2022 01:45:05 +0200 Subject: [PATCH 02/34] `services` folder --- services/docker-compose.postgres.yml | 23 +++++++++++++++++++++++ services/docker-compose.redis.yml | 22 ++++++++++++++++++++++ services/docker-compose.website.yml | 15 +++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 services/docker-compose.postgres.yml create mode 100644 services/docker-compose.redis.yml create mode 100644 services/docker-compose.website.yml diff --git a/services/docker-compose.postgres.yml b/services/docker-compose.postgres.yml new file mode 100644 index 0000000..67ca6a3 --- /dev/null +++ b/services/docker-compose.postgres.yml @@ -0,0 +1,23 @@ +version: "3.7" + +services: + postgresql: + image: bitnami/postgresql:latest + restart: always + environment: + - POSTGRESQL_USERNAME=${POSTGRESQL_USERNAME} + - POSTGRESQL_PASSWORD=${POSTGRESQL_PASSWORD} + - POSTGRESQL_DATABASE=${POSTGRESQL_DATABASE} + volumes: + - ../data/postgresql:/bitnami/postgresql + labels: + - "traefik.enable=true" + - "traefik.tcp.routers.postgresql.rule=HostSNI(`*`)" + - "traefik.tcp.routers.postgresql.entryPoints=postgresql" + - "traefik.tcp.routers.postgresql.service=postgresql" + # services (needed for TCP) + - "traefik.tcp.services.postgresql.loadbalancer.server.port=5432" + # doing https + - "traefik.http.routers.postgresql.tls.certresolver=mydnschallenge" + - "traefik.http.routers.postgresql.tls.domains[0].main=backend.${DOMAIN_NAME}" + - "traefik.http.routers.postgresql.tls.domains[0].sans=db.backend.${DOMAIN_NAME}" diff --git a/services/docker-compose.redis.yml b/services/docker-compose.redis.yml new file mode 100644 index 0000000..e644437 --- /dev/null +++ b/services/docker-compose.redis.yml @@ -0,0 +1,22 @@ +version: "3.7" + +services: + redis: + image: bitnami/redis:latest + restart: always + environment: + - REDIS_PASSWORD=${REDIS_PASSWORD} + - REDIS_DISABLE_COMMANDS=${REDIS_DISABLE_COMMANDS} + volumes: + - ../data/redis:/bitnami/postgresql + labels: + - "traefik.enable=true" + - "traefik.tcp.routers.redis.rule=HostSNI(`*`)" + - "traefik.tcp.routers.redis.entryPoints=redis" + - "traefik.tcp.routers.redis.service=redis" + # services (needed for TCP) + - "traefik.tcp.services.redis.loadbalancer.server.port=6379" + # doing https + - "traefik.http.routers.redis.tls.certresolver=mydnschallenge" + - "traefik.http.routers.redis.tls.domains[0].main=backend.${DOMAIN_NAME}" + - "traefik.http.routers.redis.tls.domains[0].sans=redis.backend.${DOMAIN_NAME}" diff --git a/services/docker-compose.website.yml b/services/docker-compose.website.yml new file mode 100644 index 0000000..a7bc958 --- /dev/null +++ b/services/docker-compose.website.yml @@ -0,0 +1,15 @@ +version: "3.7" + +services: + website: + build: ./website + restart: always + labels: + - "traefik.enable=true" + - "traefik.http.routers.website.rule=Host(`backend.${DOMAIN_NAME}`)" + - "traefik.http.routers.website.entryPoints=http" + - "traefik.http.routers.website.middlewares=https_redirect" + - "traefik.http.middlewares.https_redirect.redirectscheme.scheme=https" + - "traefik.http.routers.website_tls.rule=Host(`backend.${DOMAIN_NAME}`)" + - "traefik.http.routers.website_tls.entryPoints=https" + - "traefik.http.routers.website_tls.tls.certresolver=mydnschallenge" From dec21697d22fca6fd89d5a33b2ddb45fe9414d56 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sat, 22 Oct 2022 01:45:18 +0200 Subject: [PATCH 03/34] main file --- docker-compose.yml => docker-compose.main.yml | 6 ++++-- docker-compose.nextjs-tailwindcss.yml | 15 --------------- 2 files changed, 4 insertions(+), 17 deletions(-) rename docker-compose.yml => docker-compose.main.yml (79%) delete mode 100644 docker-compose.nextjs-tailwindcss.yml diff --git a/docker-compose.yml b/docker-compose.main.yml similarity index 79% rename from docker-compose.yml rename to docker-compose.main.yml index d5bc492..b9302c9 100644 --- a/docker-compose.yml +++ b/docker-compose.main.yml @@ -11,14 +11,16 @@ services: - "--entrypoints.https.address=:443" - "--certificatesresolvers.mydnschallenge.acme.dnschallenge=true" - "--certificatesresolvers.mydnschallenge.acme.dnschallenge.provider=digitalocean" - - "--certificatesresolvers.mydnschallenge.acme.email=example@domain.com" + - "--certificatesresolvers.mydnschallenge.acme.email=${ACME_EMAIL}" - "--certificatesresolvers.mydnschallenge.acme.storage=/acme.json" environment: - DO_AUTH_TOKEN=${DO_AUTH_TOKEN} ports: - "80:80" - "443:443" - - "6969:8080" + - "6969:8080" # Traefik dashboard + - "5432:5432" # PostgreSQL port + - "6379:6379" # Redis port volumes: - /var/run/docker.sock:/var/run/docker.sock - ./acme/acme.json:/acme.json diff --git a/docker-compose.nextjs-tailwindcss.yml b/docker-compose.nextjs-tailwindcss.yml deleted file mode 100644 index cd15101..0000000 --- a/docker-compose.nextjs-tailwindcss.yml +++ /dev/null @@ -1,15 +0,0 @@ -version: "3.7" - -services: - nextjs-tailwindcss: - build: ../nextjs-tailwindcss - restart: always - labels: - - "traefik.enable=true" - - "traefik.http.routers.nextjs-tailwindcss.rule=Host(`nextjs-tailwindcss.${DOMAIN_NAME}`)" - - "traefik.http.routers.nextjs-tailwindcss.entryPoints=http" - - "traefik.http.routers.nextjs-tailwindcss.middlewares=https_redirect" - - "traefik.http.middlewares.https_redirect.redirectscheme.scheme=https" - - "traefik.http.routers.nextjs-tailwindcss_tls.rule=Host(`nextjs-tailwindcss.${DOMAIN_NAME}`)" - - "traefik.http.routers.nextjs-tailwindcss_tls.entryPoints=https" - - "traefik.http.routers.nextjs-tailwindcss_tls.tls.certresolver=mydnschallenge" From 023136aaa195e4466b3e55a0dcf977751507b387 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sat, 22 Oct 2022 01:45:28 +0200 Subject: [PATCH 04/34] `build` script --- build.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 build.sh diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..d907638 --- /dev/null +++ b/build.sh @@ -0,0 +1,14 @@ +# -- running setup script +bash run/setup.sh + +# -- getting all docker-compose files +DC_FILES="docker-compose.main.yml" +for F in `find ./services -type f -maxdepth 1 -name "docker-compose*.yml"` +do + DC_FILES="$DC_FILES -f $F " +done + +# -- building containers +# docker-compose $DC_FILES build && docker-compose $DC_FILES up -d + +echo $DC_FILES \ No newline at end of file From 5649fa39cc8b8dd2da17894c3e23397e251515a2 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sat, 22 Oct 2022 01:45:38 +0200 Subject: [PATCH 05/34] `env` example file --- .env.example | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index a494073..b66667c 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,14 @@ -# [SYSTEM] -DOMAIN_NAME=your-domain.com -DO_AUTH_TOKEN=XXXXXXXXXXXXXXXXX \ No newline at end of file +# [GENERAL] +DOMAIN_NAME=your-website.com +DO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # for DNS challenge +ACME_EMAIL=email@your-website.com # for https certificates +GITHUB_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # for private repos + +# [REDIS] +REDIS_PASSWORD= +REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL + +# [POSTGRES] +POSTGRESQL_USERNAME=admin +POSTGRESQL_PASSWORD=123456qwerty +POSTGRESQL_DATABASE=db \ No newline at end of file From e52053a18e9a9e95c426c54074b3b39a101d1278 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sat, 22 Oct 2022 01:45:42 +0200 Subject: [PATCH 06/34] Update .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ab3171a..680ac2d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_Store .env -acme/ \ No newline at end of file +acme/ +data/ \ No newline at end of file From daf98c47e7f5191569cc8c8da07887bf5a187439 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sat, 22 Oct 2022 01:45:47 +0200 Subject: [PATCH 07/34] Update README.md --- README.md | 67 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 4a3d671..b2a8188 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,14 @@ -# https-backend +# 🔐 Universal Backend Setup -This repository is a minimal setup for running your services with Docker (Compose) behind Traefik proxy and with autogenerated HTTPS certificates. That means if you have the `API` service (e.g. NodeJS app) and would like to make it available through `https://api.your-domain.com`, then you are at the right place. +This repository is a minimalistic setup for running your services with Docker Compose behind Traefik with autogenerated `https` certificates. -## Quickstart +Let's say, you have the `API` service (e.g. NodeJS app), React app for frontend and would like to make it available via `https://api.your-domain.com` and `https://dashboard.your-domain.com`, then you are at the right place! + +> _HTTPS Backend_ is a part of [starters.dev](https://github.com/starters-dev) collection. + +## Getting Started + +### Quickstart 1. Connect to the remote server @@ -10,6 +16,8 @@ This repository is a minimal setup for running your services with Docker (Compos > ssh root@IP_ADDRESS ``` +> Hint: you can use a cloud computing providers such as [DigitalOcean](https://digitalocean.com). + 2. Clone this repo ```bash @@ -24,33 +32,62 @@ This repository is a minimal setup for running your services with Docker (Compos > nano .env ``` -4. Get the service repo (e.g. `nextjs-tailwindcss`) +Check [Environment variables](#environment-variables) section for more information. + +4. Get a service repo + +> Our example is based on [backend.starters.dev-website](https://github.com/starters-dev/backend.starters.dev-website) repo. ```bash -> bash run/get-repo.sh nextjs-tailwindcss +> git clone https://github.com/starters-dev/backend.starters.dev-website services/website ``` -It will fetch the following repo `https://github.com/starters-dev/nextjs-tailwindcss` - 5. Build and run ```bash -> bash run/build.sh +> bash build.sh ``` -It will setup everything, including ACME (https certificates), and will run docker. +It will setup everything, including https certificates, and will start Docker Compose services in the background. -## Tips +## What's inside ---- +This setup is using [Traefik](https://github.com/traefik/traefik), the cloud native application proxy, that simplifies the process of getting `https` certificates and the way to describe services. -If you'd like to add your service or from [starters-dev](https://github.com/starters-dev), create a dedicated `docker-compose.your-service.yml` file and add it to `run/build.sh` and then run: +### Services -```bash -> bash run/get-repo.sh +- [PostgreSQL](https://www.postgresql.org) - open source object-relational database known for reliability and data integrity. It uses [Bitnami Docker image](https://hub.docker.com/r/bitnami/postgresql). +- [Redis](https://redis.io) - open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker. It uses [Bitnami Docker image](https://hub.docker.com/r/bitnami/redis). +- [React App](https://backend.starters.dev) - sample frontend app. It uses [backend.starters.dev-website](https://github.com/starters-dev/backend.starters.dev-website) repo + +> more services will be added... + +## Environment variables + +Current setup requires you to fill in `.env` file with variables that are used in services. + +``` +# [GENERAL] +DOMAIN_NAME=your-website.com +DO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # for DNS challenge +ACME_EMAIL=email@your-website.com # for https certificates +GITHUB_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # (optional) for private repos + +# [REDIS] +REDIS_PASSWORD= +REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL + +# [POSTGRES] +POSTGRESQL_USERNAME=admin +POSTGRESQL_PASSWORD=123456qwerty +POSTGRESQL_DATABASE=db ``` ---- +#### `DOMAIN_NAME` + + + +## Tips --- From a6b103e9acf198eb677ea1a6a78496939f1a11f3 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sat, 22 Oct 2022 02:08:47 +0200 Subject: [PATCH 08/34] better readme --- .env.example | 14 +++++++------- README.md | 34 +++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/.env.example b/.env.example index b66667c..c5c295f 100644 --- a/.env.example +++ b/.env.example @@ -1,14 +1,14 @@ # [GENERAL] DOMAIN_NAME=your-website.com DO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # for DNS challenge -ACME_EMAIL=email@your-website.com # for https certificates -GITHUB_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # for private repos - -# [REDIS] -REDIS_PASSWORD= -REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL +ACME_EMAIL=email@your-website.com # for let's encrypt +GITHUB_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # (optional) for private repos # [POSTGRES] POSTGRESQL_USERNAME=admin POSTGRESQL_PASSWORD=123456qwerty -POSTGRESQL_DATABASE=db \ No newline at end of file +POSTGRESQL_DATABASE=db + +# [REDIS] +REDIS_PASSWORD= +REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL \ No newline at end of file diff --git a/README.md b/README.md index b2a8188..2d09f3b 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ It will setup everything, including https certificates, and will start Docker Co This setup is using [Traefik](https://github.com/traefik/traefik), the cloud native application proxy, that simplifies the process of getting `https` certificates and the way to describe services. +You can always delete existing (if you don't need them) or add your own services by following the same structure. + ### Services - [PostgreSQL](https://www.postgresql.org) - open source object-relational database known for reliability and data integrity. It uses [Bitnami Docker image](https://hub.docker.com/r/bitnami/postgresql). @@ -70,29 +72,39 @@ Current setup requires you to fill in `.env` file with variables that are used i # [GENERAL] DOMAIN_NAME=your-website.com DO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # for DNS challenge -ACME_EMAIL=email@your-website.com # for https certificates +ACME_EMAIL=email@your-website.com # for let's encrypt GITHUB_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # (optional) for private repos -# [REDIS] -REDIS_PASSWORD= -REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL - # [POSTGRES] POSTGRESQL_USERNAME=admin POSTGRESQL_PASSWORD=123456qwerty POSTGRESQL_DATABASE=db + +# [REDIS] +REDIS_PASSWORD= +REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL ``` -#### `DOMAIN_NAME` +#### General + +- `DOMAIN_NAME` - your registered domain. +- `DO_AUTH_TOKEN` - Digital Ocean token that is going to be used for DNS challenge and generating https certificates. It's required by Traefik and they provide other options, you can find them [here](https://doc.traefik.io/traefik/https/acme/#providers). If you'd like to continue with Digital Ocean, then you can create a token in `Dashboard` -> `API` -> `Tokens/Keys`. +- `ACME_EMAIL` - email that is used for [Let's Encrypt](https://letsencrypt.org) and `https` certificates. +- `GITHUB_TOKEN` - github token for private repos. +#### PostreSQL +- `POSTGRESQL_USERNAME` - username for PostgreSQL. +- `POSTGRESQL_PASSWORD` - password for PostgreSQL. +- `POSTGRESQL_DATABASE` - name of the database in PostgreSQL. -## Tips +#### Redis ---- +- `REDIS_PASSWORD` - password for Redis. +- `REDIS_DISABLE_COMMANDS` - commands disabled from execution. -You can find example `env` file in the root folder. +## Why? -`DO_AUTH_TOKEN` is used to generate https certificates against [DigitalOcean](https://digitalocean.com) challenge. You can generate one in the DO Networking dashboard or choose one of the [available providers](https://doc.traefik.io/traefik/https/acme/#providers). +While developing `API` and similar services for mobile apps, you can not really access `localhost` (on mobile device) if, let's say, you have running Docker image on you local machine. Also you have to make only `https` requests from mobile app. ---- +Another reason was to have `PostgreSQL` and `Redis` always running in the cloud but for cheap cost. You can run both of them easily on less than $5 server. From 0d5b10e882501849330f39d8e4b29e6c5fe156b9 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sat, 22 Oct 2022 02:18:06 +0200 Subject: [PATCH 09/34] badges added --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2d09f3b..edff207 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ -# 🔐 Universal Backend Setup +# 🔐 Universal Backend + +![Traefik](https://img.shields.io/badge/Proxy-Traefik-skyblue) +![PostgreSQL](https://img.shields.io/badge/Database-PostgreSQL-blue) +![Redis](https://img.shields.io/badge/Cache-Redis-red) +![Website](https://img.shields.io/badge/Website-React%20App-blue) This repository is a minimalistic setup for running your services with Docker Compose behind Traefik with autogenerated `https` certificates. @@ -107,4 +112,4 @@ REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL While developing `API` and similar services for mobile apps, you can not really access `localhost` (on mobile device) if, let's say, you have running Docker image on you local machine. Also you have to make only `https` requests from mobile app. -Another reason was to have `PostgreSQL` and `Redis` always running in the cloud but for cheap cost. You can run both of them easily on less than $5 server. +Another reason was to have `PostgreSQL` and `Redis` always running in the cloud but for cheap cost. You can run both of them easily on less than $5 server. From 6643533f62e0f4844f39cef9d9cd71c4be19856b Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sat, 22 Oct 2022 02:51:42 +0200 Subject: [PATCH 10/34] `frontend` service --- README.md | 18 +++++++++++++++--- build.sh | 4 +--- services/docker-compose.frontend.yml | 15 +++++++++++++++ services/docker-compose.website.yml | 15 --------------- 4 files changed, 31 insertions(+), 21 deletions(-) create mode 100644 services/docker-compose.frontend.yml delete mode 100644 services/docker-compose.website.yml diff --git a/README.md b/README.md index edff207..3de0e0a 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Check [Environment variables](#environment-variables) section for more informati > Our example is based on [backend.starters.dev-website](https://github.com/starters-dev/backend.starters.dev-website) repo. ```bash -> git clone https://github.com/starters-dev/backend.starters.dev-website services/website +> git clone https://github.com/starters-dev/backend.starters.dev-website services/frontend ``` 5. Build and run @@ -55,11 +55,23 @@ Check [Environment variables](#environment-variables) section for more informati It will setup everything, including https certificates, and will start Docker Compose services in the background. +## Structure + +The folder structure is pretty simple but makes you reuse and write less boilerplate code. + +- `docker-compose.main.yml` - main Docker Compose file with DNS challence, ACME and ports configuration. +- `build.sh` - `build` script that runs `setup` script with all scripts under `run/services` folder, builds and runs Docker images in background. +- `.env` - list of environment variables. +- `run/` - folder with general bash scripts. +- `run/services/` - folder with bash scripts for a specific service. +- `services/` - folder with Docker services files. For example, if you have `redis` service, then you would have `services/docker-compose.redis.yml` with specific configuration for `redis` service. Or if your service is a frontend app, then you'll need to clone it to `services/frontend/` and create `services/docker-compose.frontend.yml`. +- `services/your-service/` - folder with your service, for ex. `frontend`. + ## What's inside -This setup is using [Traefik](https://github.com/traefik/traefik), the cloud native application proxy, that simplifies the process of getting `https` certificates and the way to describe services. +The setup uses [Traefik](https://github.com/traefik/traefik), the cloud native application proxy, to simplify the process of getting `https` certificates and the way to describe services. -You can always delete existing (if you don't need them) or add your own services by following the same structure. +You can always delete existing (if you don't need them) or add your own services, just make sure to apply necessary changes. ### Services diff --git a/build.sh b/build.sh index d907638..03907ec 100644 --- a/build.sh +++ b/build.sh @@ -9,6 +9,4 @@ do done # -- building containers -# docker-compose $DC_FILES build && docker-compose $DC_FILES up -d - -echo $DC_FILES \ No newline at end of file +docker-compose $DC_FILES build && docker-compose $DC_FILES up -d \ No newline at end of file diff --git a/services/docker-compose.frontend.yml b/services/docker-compose.frontend.yml new file mode 100644 index 0000000..c4f280a --- /dev/null +++ b/services/docker-compose.frontend.yml @@ -0,0 +1,15 @@ +version: "3.7" + +services: + frontend: + build: ./frontend + restart: always + labels: + - "traefik.enable=true" + - "traefik.http.routers.frontend.rule=Host(`backend.${DOMAIN_NAME}`)" + - "traefik.http.routers.frontend.entryPoints=http" + - "traefik.http.routers.frontend.middlewares=https_redirect" + - "traefik.http.middlewares.https_redirect.redirectscheme.scheme=https" + - "traefik.http.routers.frontend_tls.rule=Host(`backend.${DOMAIN_NAME}`)" + - "traefik.http.routers.frontend_tls.entryPoints=https" + - "traefik.http.routers.frontend_tls.tls.certresolver=mydnschallenge" diff --git a/services/docker-compose.website.yml b/services/docker-compose.website.yml deleted file mode 100644 index a7bc958..0000000 --- a/services/docker-compose.website.yml +++ /dev/null @@ -1,15 +0,0 @@ -version: "3.7" - -services: - website: - build: ./website - restart: always - labels: - - "traefik.enable=true" - - "traefik.http.routers.website.rule=Host(`backend.${DOMAIN_NAME}`)" - - "traefik.http.routers.website.entryPoints=http" - - "traefik.http.routers.website.middlewares=https_redirect" - - "traefik.http.middlewares.https_redirect.redirectscheme.scheme=https" - - "traefik.http.routers.website_tls.rule=Host(`backend.${DOMAIN_NAME}`)" - - "traefik.http.routers.website_tls.entryPoints=https" - - "traefik.http.routers.website_tls.tls.certresolver=mydnschallenge" From 2e2fb42b0c7284db238468e5e87ec86f5ed53b7d Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sat, 22 Oct 2022 15:30:33 +0200 Subject: [PATCH 11/34] Update .env.example --- .env.example | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index c5c295f..535cd58 100644 --- a/.env.example +++ b/.env.example @@ -1,8 +1,8 @@ # [GENERAL] DOMAIN_NAME=your-website.com -DO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # for DNS challenge -ACME_EMAIL=email@your-website.com # for let's encrypt -GITHUB_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # (optional) for private repos +DO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxx # for DNS challenge +ACME_EMAIL=email@your-website.com # for let's encrypt +GITHUB_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # (optional) for private repos # [POSTGRES] POSTGRESQL_USERNAME=admin @@ -10,5 +10,5 @@ POSTGRESQL_PASSWORD=123456qwerty POSTGRESQL_DATABASE=db # [REDIS] -REDIS_PASSWORD= +REDIS_PASSWORD=123456qwerty REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL \ No newline at end of file From a6b35d5619b5d63850deac24ed8f92d74df3a84e Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sat, 22 Oct 2022 15:30:53 +0200 Subject: [PATCH 12/34] `setup` script became independent --- build.sh | 5 +---- run/setup.sh => setup.sh | 10 +++++----- 2 files changed, 6 insertions(+), 9 deletions(-) rename run/setup.sh => setup.sh (52%) diff --git a/build.sh b/build.sh index 03907ec..ffefd01 100644 --- a/build.sh +++ b/build.sh @@ -1,7 +1,4 @@ -# -- running setup script -bash run/setup.sh - -# -- getting all docker-compose files +# -- getting docker-compose files DC_FILES="docker-compose.main.yml" for F in `find ./services -type f -maxdepth 1 -name "docker-compose*.yml"` do diff --git a/run/setup.sh b/setup.sh similarity index 52% rename from run/setup.sh rename to setup.sh index 325e3bc..92ce413 100644 --- a/run/setup.sh +++ b/setup.sh @@ -1,11 +1,11 @@ -# -- running system bash files -bash run/acme.sh -bash run/data.sh - # -- running services' bash files +BASH_SYSTEM_FILES="bash run/acme.sh; bash run/data.sh;" BASH_FILES="" for F in `find ./run/services -type f -maxdepth 1 -name "*.sh"` do BASH_FILES="$BASH_FILES bash $F;" done -eval $BASH_FILES \ No newline at end of file +eval "$BASH_SYSTEM_FILES $BASH_FILES" + +# -- .env.example -> .env +mv .env.example .env \ No newline at end of file From cf0382098dfdc408d2804695144d8dc00fc67a93 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sat, 22 Oct 2022 15:31:05 +0200 Subject: [PATCH 13/34] better docs --- README.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3de0e0a..8d9bcaa 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ -# 🔐 Universal Backend +# 🔐 Universal HTTPS Backend ![Traefik](https://img.shields.io/badge/Proxy-Traefik-skyblue) +![HTTPS](https://img.shields.io/badge/HTTPS-Configured-green) ![PostgreSQL](https://img.shields.io/badge/Database-PostgreSQL-blue) ![Redis](https://img.shields.io/badge/Cache-Redis-red) ![Website](https://img.shields.io/badge/Website-React%20App-blue) @@ -15,7 +16,7 @@ Let's say, you have the `API` service (e.g. NodeJS app), React app for frontend ### Quickstart -1. Connect to the remote server +1. Connect to your remote server ```bash > ssh root@IP_ADDRESS @@ -30,10 +31,10 @@ Let's say, you have the `API` service (e.g. NodeJS app), React app for frontend > cd backend ``` -3. Fill in `.env` file with your information +3. Run `setup` script and fill in `env` variables ```bash -> mv .env.example .env +> bash setup.sh > nano .env ``` @@ -57,10 +58,10 @@ It will setup everything, including https certificates, and will start Docker Co ## Structure -The folder structure is pretty simple but makes you reuse and write less boilerplate code. +The folder structure is pretty simple but lets you reuse and write less boilerplate code. - `docker-compose.main.yml` - main Docker Compose file with DNS challence, ACME and ports configuration. -- `build.sh` - `build` script that runs `setup` script with all scripts under `run/services` folder, builds and runs Docker images in background. +- `build.sh` - `build` script that runs `setup` script with all scripts under `run/services` folder and builds and runs Docker images in background. - `.env` - list of environment variables. - `run/` - folder with general bash scripts. - `run/services/` - folder with bash scripts for a specific service. @@ -125,3 +126,7 @@ REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL While developing `API` and similar services for mobile apps, you can not really access `localhost` (on mobile device) if, let's say, you have running Docker image on you local machine. Also you have to make only `https` requests from mobile app. Another reason was to have `PostgreSQL` and `Redis` always running in the cloud but for cheap cost. You can run both of them easily on less than $5 server. + +#### Note + +This backend setup is a great fit if you just want to start writing business logic without messing with DevOps and spending a few days on that what's already has been done for you. It can be used for development and early production stages (of course, depends on your project), however, it's strongly **recommended** to have services like `PostgreSQL`, `Redis`, and similar to be seperated and independent on production stage. From e7a81b9efa269bb1a1854efbb991a1782de0ae4b Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sat, 22 Oct 2022 15:33:56 +0200 Subject: [PATCH 14/34] `React App` in docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d9bcaa..393e8b2 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ You can always delete existing (if you don't need them) or add your own services - [PostgreSQL](https://www.postgresql.org) - open source object-relational database known for reliability and data integrity. It uses [Bitnami Docker image](https://hub.docker.com/r/bitnami/postgresql). - [Redis](https://redis.io) - open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker. It uses [Bitnami Docker image](https://hub.docker.com/r/bitnami/redis). -- [React App](https://backend.starters.dev) - sample frontend app. It uses [backend.starters.dev-website](https://github.com/starters-dev/backend.starters.dev-website) repo +- [React App](https://backend.starters.dev) - simple [frontend app](https://github.com/starters-dev/backend.starters.dev-website). > more services will be added... From 36d4f0c17c5486f617c2b7d22e7576a21c2aaa4b Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 01:44:28 +0200 Subject: [PATCH 15/34] improvements --- build.sh | 4 ++-- run/services/postgresql.sh | 5 ++++- run/services/redis.sh | 1 + services/docker-compose.frontend.yml | 2 +- ...er-compose.postgres.yml => docker-compose.postgresql.yml} | 2 +- services/docker-compose.redis.yml | 2 +- setup.sh | 2 +- 7 files changed, 11 insertions(+), 7 deletions(-) rename services/{docker-compose.postgres.yml => docker-compose.postgresql.yml} (94%) diff --git a/build.sh b/build.sh index ffefd01..5707155 100644 --- a/build.sh +++ b/build.sh @@ -1,6 +1,6 @@ # -- getting docker-compose files -DC_FILES="docker-compose.main.yml" -for F in `find ./services -type f -maxdepth 1 -name "docker-compose*.yml"` +DC_FILES="-f docker-compose.main.yml" +for F in `find ./services -maxdepth 1 -type f -name "docker-compose*.yml"` do DC_FILES="$DC_FILES -f $F " done diff --git a/run/services/postgresql.sh b/run/services/postgresql.sh index 1f9e97e..13bd05e 100644 --- a/run/services/postgresql.sh +++ b/run/services/postgresql.sh @@ -3,4 +3,7 @@ DIR=data/postgresql if [ ! -d "$DIR" ]; then mkdir data/postgresql -fi \ No newline at end of file +fi + +# -- bitnami image needs permissions +sudo chown -R 1001:1001 data/postgresql \ No newline at end of file diff --git a/run/services/redis.sh b/run/services/redis.sh index c1b21b1..458f208 100644 --- a/run/services/redis.sh +++ b/run/services/redis.sh @@ -5,4 +5,5 @@ if [ ! -d "$DIR" ]; then mkdir data/redis fi +# -- bitnami image needs permissions sudo chown -R 1001:1001 data/redis \ No newline at end of file diff --git a/services/docker-compose.frontend.yml b/services/docker-compose.frontend.yml index c4f280a..5385b43 100644 --- a/services/docker-compose.frontend.yml +++ b/services/docker-compose.frontend.yml @@ -2,7 +2,7 @@ version: "3.7" services: frontend: - build: ./frontend + build: ./services/frontend restart: always labels: - "traefik.enable=true" diff --git a/services/docker-compose.postgres.yml b/services/docker-compose.postgresql.yml similarity index 94% rename from services/docker-compose.postgres.yml rename to services/docker-compose.postgresql.yml index 67ca6a3..794d08e 100644 --- a/services/docker-compose.postgres.yml +++ b/services/docker-compose.postgresql.yml @@ -9,7 +9,7 @@ services: - POSTGRESQL_PASSWORD=${POSTGRESQL_PASSWORD} - POSTGRESQL_DATABASE=${POSTGRESQL_DATABASE} volumes: - - ../data/postgresql:/bitnami/postgresql + - ./data/postgresql:/bitnami/postgresql labels: - "traefik.enable=true" - "traefik.tcp.routers.postgresql.rule=HostSNI(`*`)" diff --git a/services/docker-compose.redis.yml b/services/docker-compose.redis.yml index e644437..e8761d0 100644 --- a/services/docker-compose.redis.yml +++ b/services/docker-compose.redis.yml @@ -8,7 +8,7 @@ services: - REDIS_PASSWORD=${REDIS_PASSWORD} - REDIS_DISABLE_COMMANDS=${REDIS_DISABLE_COMMANDS} volumes: - - ../data/redis:/bitnami/postgresql + - ./data/redis:/bitnami/postgresql labels: - "traefik.enable=true" - "traefik.tcp.routers.redis.rule=HostSNI(`*`)" diff --git a/setup.sh b/setup.sh index 92ce413..b26018b 100644 --- a/setup.sh +++ b/setup.sh @@ -1,7 +1,7 @@ # -- running services' bash files BASH_SYSTEM_FILES="bash run/acme.sh; bash run/data.sh;" BASH_FILES="" -for F in `find ./run/services -type f -maxdepth 1 -name "*.sh"` +for F in `find ./run/services -maxdepth 1 -type f -name "*.sh"` do BASH_FILES="$BASH_FILES bash $F;" done From 4f8d1e3ad011a7c260c4e40e2b19beb61d3efc84 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 14:47:50 +0200 Subject: [PATCH 16/34] entry points for `redis` and `postgresql` --- docker-compose.main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docker-compose.main.yml b/docker-compose.main.yml index b9302c9..1311823 100644 --- a/docker-compose.main.yml +++ b/docker-compose.main.yml @@ -9,6 +9,8 @@ services: - "--providers.docker.exposedbydefault=false" - "--entrypoints.http.address=:80" - "--entrypoints.https.address=:443" + - "--entrypoints.postgresql.address=:5432" # PostgreSQL entry point + - "--entrypoints.redis.address=:6379" # Redis entry point - "--certificatesresolvers.mydnschallenge.acme.dnschallenge=true" - "--certificatesresolvers.mydnschallenge.acme.dnschallenge.provider=digitalocean" - "--certificatesresolvers.mydnschallenge.acme.email=${ACME_EMAIL}" From e9d134e4100c7ce2207d29e6f0eb33cfff03460f Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 14:48:23 +0200 Subject: [PATCH 17/34] `Enhancements` section --- README.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 393e8b2..a6c7e1f 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,12 @@ ![Redis](https://img.shields.io/badge/Cache-Redis-red) ![Website](https://img.shields.io/badge/Website-React%20App-blue) -This repository is a minimalistic setup for running your services with Docker Compose behind Traefik with autogenerated `https` certificates. +This repository is a minimalistic setup for running services with Docker Compose behind Traefik and autogenerated `https` certificates. Let's say, you have the `API` service (e.g. NodeJS app), React app for frontend and would like to make it available via `https://api.your-domain.com` and `https://dashboard.your-domain.com`, then you are at the right place! +It includes the collection of `docker-compose` files and bash scripts that automate and simplify startup of your project, by taking care of some DevOps routine. List with available services can be found at [Services section](#services). + > _HTTPS Backend_ is a part of [starters.dev](https://github.com/starters-dev) collection. ## Getting Started @@ -121,11 +123,21 @@ REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL - `REDIS_PASSWORD` - password for Redis. - `REDIS_DISABLE_COMMANDS` - commands disabled from execution. +## Enhancements + +There are still some things I would like to add to the backend setup: + +- [x] [PostgreSQL](https://www.postgresql.org) - open source object-relational database known for reliability and data integrity. +- [x] [Redis](https://redis.io) - open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker. +- [ ] [Plausible](https://plausible.io) - simple and privacy-friendly Google Analytics alternative. +- [ ] [GlitchTip](https://glitchtip.com) - open source reimplementation of Sentry error tracking platform. +- [ ] Github actions or similar technique + ## Why? While developing `API` and similar services for mobile apps, you can not really access `localhost` (on mobile device) if, let's say, you have running Docker image on you local machine. Also you have to make only `https` requests from mobile app. -Another reason was to have `PostgreSQL` and `Redis` always running in the cloud but for cheap cost. You can run both of them easily on less than $5 server. +Another reason was to have `PostgreSQL` and `Redis` always running in the cloud but for cheap cost. You can run both of them easily on the $6 server. #### Note From 89461596b3dd5b33fda08787451945ddafab25b1 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 14:55:21 +0200 Subject: [PATCH 18/34] more todos --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index a6c7e1f..ceac4a7 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,9 @@ There are still some things I would like to add to the backend setup: - [x] [Redis](https://redis.io) - open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker. - [ ] [Plausible](https://plausible.io) - simple and privacy-friendly Google Analytics alternative. - [ ] [GlitchTip](https://glitchtip.com) - open source reimplementation of Sentry error tracking platform. +- [ ] [Mattermost](https://mattermost.com) - open source platform for developer collaboration. [Github repo](https://github.com/starters-dev/mattermost). +- [ ] [Taiga](https://www.taiga.io) - open source, self-hosted project management tool. [Github repo](https://github.com/starters-dev/taiga). +- [ ] [Focalboard](https://focalboard.com) - open source, self-hosted alternative to Trello, Notion, and Asana. [Github repo](https://github.com/starters-dev/focalboard). - [ ] Github actions or similar technique ## Why? From 3e8da0cd1580bd85d160ce0d68b27595f515b6c2 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 17:32:25 +0200 Subject: [PATCH 19/34] better scripts --- run/services/postgresql.sh | 15 ++++++++++----- run/services/redis.sh | 15 ++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/run/services/postgresql.sh b/run/services/postgresql.sh index 13bd05e..b1e5992 100644 --- a/run/services/postgresql.sh +++ b/run/services/postgresql.sh @@ -1,9 +1,14 @@ -# -- creating folder for PostgreSQL -DIR=data/postgresql - +# -- creating folder for PostgreSQL service +DIR=services/postgresql if [ ! -d "$DIR" ]; then - mkdir data/postgresql + mkdir $DIR +fi + +# -- creating folder for storing data +DIR_DATA=$DIR/data +if [ ! -d "$DIR_DATA" ]; then + mkdir $DIR_DATA fi # -- bitnami image needs permissions -sudo chown -R 1001:1001 data/postgresql \ No newline at end of file +sudo chown -R 1001:1001 $DIR_DATA \ No newline at end of file diff --git a/run/services/redis.sh b/run/services/redis.sh index 458f208..421cb2f 100644 --- a/run/services/redis.sh +++ b/run/services/redis.sh @@ -1,9 +1,14 @@ -# -- creating folder for Redis -DIR=data/redis - +# -- creating folder for Redis service +DIR=services/redis if [ ! -d "$DIR" ]; then - mkdir data/redis + mkdir $DIR +fi + +# -- creating folder for storing data +DIR_DATA=$DIR/data +if [ ! -d "$DIR_DATA" ]; then + mkdir $DIR_DATA fi # -- bitnami image needs permissions -sudo chown -R 1001:1001 data/redis \ No newline at end of file +sudo chown -R 1001:1001 $DIR_DATA \ No newline at end of file From eb111271d521c706edac1819bc3063b46f46e430 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 17:32:32 +0200 Subject: [PATCH 20/34] `sdel` script --- run/sdel.sh | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 run/sdel.sh diff --git a/run/sdel.sh b/run/sdel.sh new file mode 100644 index 0000000..97aeb52 --- /dev/null +++ b/run/sdel.sh @@ -0,0 +1,19 @@ +SNAME=$1 + +# -- deleting service folder +SDIR=services/$SNAME +if [ -d "$SDIR" ]; then + rm -rf $SDIR +fi + +# -- deleting docker compose file +SDC=services/docker-compose.$SNAME.yml +if [ -f "$SDC" ]; then + rm -rf $SDC +fi + +# -- deleting script +SSH=run/services/$SNAME.sh +if [ -f "$SSH" ]; then + rm -rf $SSH +fi \ No newline at end of file From f84f6b1aa475344816be0936ece9e6c30bd2e3b5 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 17:32:35 +0200 Subject: [PATCH 21/34] Delete data.sh --- run/data.sh | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 run/data.sh diff --git a/run/data.sh b/run/data.sh deleted file mode 100644 index bc00772..0000000 --- a/run/data.sh +++ /dev/null @@ -1,6 +0,0 @@ -# `data/` folder for storing DB, Redis, etc. data -DIR=data - -if [ ! -d "$DIR" ]; then - mkdir data -fi \ No newline at end of file From 293a4f9b0305932090221a070d14d4152458b3b3 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 17:32:45 +0200 Subject: [PATCH 22/34] data folders moved --- services/docker-compose.postgresql.yml | 2 +- services/docker-compose.redis.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/docker-compose.postgresql.yml b/services/docker-compose.postgresql.yml index 794d08e..7573c96 100644 --- a/services/docker-compose.postgresql.yml +++ b/services/docker-compose.postgresql.yml @@ -9,7 +9,7 @@ services: - POSTGRESQL_PASSWORD=${POSTGRESQL_PASSWORD} - POSTGRESQL_DATABASE=${POSTGRESQL_DATABASE} volumes: - - ./data/postgresql:/bitnami/postgresql + - ./services/postgresql/data:/bitnami/postgresql labels: - "traefik.enable=true" - "traefik.tcp.routers.postgresql.rule=HostSNI(`*`)" diff --git a/services/docker-compose.redis.yml b/services/docker-compose.redis.yml index e8761d0..aafe6bf 100644 --- a/services/docker-compose.redis.yml +++ b/services/docker-compose.redis.yml @@ -8,7 +8,7 @@ services: - REDIS_PASSWORD=${REDIS_PASSWORD} - REDIS_DISABLE_COMMANDS=${REDIS_DISABLE_COMMANDS} volumes: - - ./data/redis:/bitnami/postgresql + - ./services/redis/data:/bitnami/redis/data labels: - "traefik.enable=true" - "traefik.tcp.routers.redis.rule=HostSNI(`*`)" From 7a426c10f6f3278891e9f3651814bf518da2dc8b Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 17:33:24 +0200 Subject: [PATCH 23/34] Update README.md --- README.md | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index ceac4a7..fd5beee 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,26 @@ Check [Environment variables](#environment-variables) section for more informati It will setup everything, including https certificates, and will start Docker Compose services in the background. +### Service creation + +If you'd like to add your own service, then you should follow the steps below to keep the same structure: + +1. Add service's Docker Compose file `services/docker-compose.YOUR_SERVICE.yml`. + +2. `(optional)` Add service's folder `services/YOUR_SERVICE`. For example, if you are going to store some data or configuration files only related to that service. + +3. `(optional)` Add service's script file `run/services/YOUR_SERVICE.sh`. For example, if you need to craete some folders in advance on `setup` step. + +### Service deletion + +If you don't need a service, you can easily delete it by running: + +```bash +> bash run/sdel.sh SERVICE_NAME +``` + +For example, if you don't need `postgresql` service, you will run `bash run/sdel.sh postgresql` and it will delete related files. + ## Structure The folder structure is pretty simple but lets you reuse and write less boilerplate code. @@ -88,41 +108,30 @@ You can always delete existing (if you don't need them) or add your own services Current setup requires you to fill in `.env` file with variables that are used in services. -``` -# [GENERAL] -DOMAIN_NAME=your-website.com -DO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # for DNS challenge -ACME_EMAIL=email@your-website.com # for let's encrypt -GITHUB_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # (optional) for private repos - -# [POSTGRES] -POSTGRESQL_USERNAME=admin -POSTGRESQL_PASSWORD=123456qwerty -POSTGRESQL_DATABASE=db - -# [REDIS] -REDIS_PASSWORD= -REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL -``` - #### General - `DOMAIN_NAME` - your registered domain. - `DO_AUTH_TOKEN` - Digital Ocean token that is going to be used for DNS challenge and generating https certificates. It's required by Traefik and they provide other options, you can find them [here](https://doc.traefik.io/traefik/https/acme/#providers). If you'd like to continue with Digital Ocean, then you can create a token in `Dashboard` -> `API` -> `Tokens/Keys`. - `ACME_EMAIL` - email that is used for [Let's Encrypt](https://letsencrypt.org) and `https` certificates. -- `GITHUB_TOKEN` - github token for private repos. +- `GITHUB_TOKEN` - `(optional)` github token for private repos. -#### PostreSQL +
+PostgreSQL - `POSTGRESQL_USERNAME` - username for PostgreSQL. - `POSTGRESQL_PASSWORD` - password for PostgreSQL. - `POSTGRESQL_DATABASE` - name of the database in PostgreSQL. -#### Redis +
+ +
+Redis - `REDIS_PASSWORD` - password for Redis. - `REDIS_DISABLE_COMMANDS` - commands disabled from execution. +
+ ## Enhancements There are still some things I would like to add to the backend setup: From 775a502b9431c066b4b55a5b77a9fec7bb86acf0 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 17:36:28 +0200 Subject: [PATCH 24/34] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fd5beee..a9606d9 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ There are still some things I would like to add to the backend setup: - [x] [PostgreSQL](https://www.postgresql.org) - open source object-relational database known for reliability and data integrity. - [x] [Redis](https://redis.io) - open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker. -- [ ] [Plausible](https://plausible.io) - simple and privacy-friendly Google Analytics alternative. +- [ ] [Plausible](https://plausible.io) - simple and privacy-friendly Google Analytics alternative. [Github repo](https://github.com/plausible/hosting). - [ ] [GlitchTip](https://glitchtip.com) - open source reimplementation of Sentry error tracking platform. - [ ] [Mattermost](https://mattermost.com) - open source platform for developer collaboration. [Github repo](https://github.com/starters-dev/mattermost). - [ ] [Taiga](https://www.taiga.io) - open source, self-hosted project management tool. [Github repo](https://github.com/starters-dev/taiga). From fedd1b0be24d905c88a198148658db9781402b9b Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 17:36:30 +0200 Subject: [PATCH 25/34] Update setup.sh --- setup.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/setup.sh b/setup.sh index b26018b..184a75b 100644 --- a/setup.sh +++ b/setup.sh @@ -8,4 +8,7 @@ done eval "$BASH_SYSTEM_FILES $BASH_FILES" # -- .env.example -> .env -mv .env.example .env \ No newline at end of file +ENVEXF=.env.example +if [ -f "$ENVEXF" ]; then + mv .env.example .env +fi \ No newline at end of file From c826a351dc6563568091a045e0c89d0d188e353c Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 17:36:33 +0200 Subject: [PATCH 26/34] Update .gitignore --- .gitignore | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 680ac2d..42c111b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ +# General .DS_Store .env -acme/ -data/ \ No newline at end of file +acme/ \ No newline at end of file From a47d8516407eed64465870402e4a7fc7c524e76d Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 18:06:38 +0200 Subject: [PATCH 27/34] plausible + other improvments --- .env.example | 5 +- README.md | 19 ++++++ run/sadd.sh | 19 ++++++ run/sdel.sh | 8 +-- services/docker-compose.plausible.yml | 62 +++++++++++++++++++ .../clickhouse/clickhouse-config.xml | 15 +++++ .../clickhouse/clickhouse-user-config.xml | 9 +++ services/plausible/conf.env | 5 ++ services/plausible/index.sh | 13 ++++ .../postgresql/index.sh | 4 +- .../redis.sh => services/redis/index.sh | 4 +- setup.sh | 6 +- 12 files changed, 156 insertions(+), 13 deletions(-) create mode 100644 run/sadd.sh create mode 100644 services/docker-compose.plausible.yml create mode 100644 services/plausible/clickhouse/clickhouse-config.xml create mode 100644 services/plausible/clickhouse/clickhouse-user-config.xml create mode 100644 services/plausible/conf.env create mode 100644 services/plausible/index.sh rename run/services/postgresql.sh => services/postgresql/index.sh (87%) rename run/services/redis.sh => services/redis/index.sh (88%) diff --git a/.env.example b/.env.example index 535cd58..0f339e2 100644 --- a/.env.example +++ b/.env.example @@ -11,4 +11,7 @@ POSTGRESQL_DATABASE=db # [REDIS] REDIS_PASSWORD=123456qwerty -REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL \ No newline at end of file +REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL + +# [PLAUSIBLE] +PLAUSIBLE_POSTGRES_PASSWORD=123456qwerty \ No newline at end of file diff --git a/README.md b/README.md index a9606d9..b058e7d 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,25 @@ Current setup requires you to fill in `.env` file with variables that are used i +
+Plausible + +`.env` file: + +- `PLAUSIBLE_POSTGRES_PASSWORD` - password for Plausible PostgreSQL. + +`services/plausible/conf.env` file: + +- `SECRET_KEY_BASE` - secret key of your app. Can be generated by running `openssl rand -base64 64 | tr -d '\n' ; echo`. +- `BASE_URL` - base url where this instance is accessible, including the scheme (eg. `http://` or `https://`), the domain name, and optionally a port. +- `ADMIN_USER_EMAIL` - admin email. +- `ADMIN_USER_NAME` - admin usenamename. +- `ADMIN_USER_PWD` - admin password. + +> [Original docs](https://plausible.io/docs/self-hosting). + +
+ ## Enhancements There are still some things I would like to add to the backend setup: diff --git a/run/sadd.sh b/run/sadd.sh new file mode 100644 index 0000000..97743e5 --- /dev/null +++ b/run/sadd.sh @@ -0,0 +1,19 @@ +SNAME=$1 + +# -- adding docker compose file +SDC=services/docker-compose.$SNAME.yml +if [ ! -f "$SDC" ]; then + touch $SDC +fi + +# -- adding service folder +SDIR=services/$SNAME +if [ ! -d "$SDIR" ]; then + mkdir $SDIR +fi + +# -- adding script +SSH=services/$SNAME/index.sh +if [ ! -f "$SSH" ]; then + touch $SSH > SNAME=$SNAME +fi \ No newline at end of file diff --git a/run/sdel.sh b/run/sdel.sh index 97aeb52..6291933 100644 --- a/run/sdel.sh +++ b/run/sdel.sh @@ -10,10 +10,4 @@ fi SDC=services/docker-compose.$SNAME.yml if [ -f "$SDC" ]; then rm -rf $SDC -fi - -# -- deleting script -SSH=run/services/$SNAME.sh -if [ -f "$SSH" ]; then - rm -rf $SSH -fi \ No newline at end of file +fi \ No newline at end of file diff --git a/services/docker-compose.plausible.yml b/services/docker-compose.plausible.yml new file mode 100644 index 0000000..b92744b --- /dev/null +++ b/services/docker-compose.plausible.yml @@ -0,0 +1,62 @@ +# taken from -- https://github.com/plausible/hosting/blob/master/docker-compose.yml +version: "3.3" + +services: + mail: + image: bytemark/smtp + restart: always + + plausible_db: + image: postgres:14-alpine + restart: always + volumes: + - ./services/plausible/data:/var/lib/postgresql/data + environment: + - POSTGRES_PASSWORD=${PLAUSIBLE_POSTGRES_PASSWORD} + labels: + - "traefik.enable=false" + + plausible_events_db: + image: clickhouse/clickhouse-server:22.6-alpine + restart: always + volumes: + - event-data:/var/lib/clickhouse + - ./services/plausible/clickhouse/clickhouse-config.xml:/etc/clickhouse-server/config.d/logging.xml:ro + - ./services/plausible/clickhouse/clickhouse-user-config.xml:/etc/clickhouse-server/users.d/logging.xml:ro + ulimits: + nofile: + soft: 262144 + hard: 262144 + labels: + - "traefik.enable=false" + + plausible: + image: plausible/analytics:latest + restart: always + command: sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh db init-admin && /entrypoint.sh run" + depends_on: + - plausible_db + - plausible_events_db + - mail + # ports: + # - 8000:8000 + env_file: + - ./services/plausible/conf.env + labels: + - "traefik.enable=true" + - "traefik.http.routers.plausible.rule=Host(`plausible.${DOMAIN_NAME}`)" + - "traefik.http.routers.plausible.entryPoints=http" + - "traefik.http.routers.plausible.middlewares=https_redirect" + - "traefik.http.middlewares.https_redirect.redirectscheme.scheme=https" + - "traefik.http.routers.plausible_tls.rule=Host(`plausible.${DOMAIN_NAME}`)" + - "traefik.http.routers.plausible_tls.entryPoints=https" + - "traefik.http.routers.plausible_tls.tls.certresolver=mydnschallenge" + - "traefik.http.services.plausible.loadbalancer.server.port=8000" + +volumes: + db-data: + driver: local + event-data: + driver: local + geoip: + driver: local diff --git a/services/plausible/clickhouse/clickhouse-config.xml b/services/plausible/clickhouse/clickhouse-config.xml new file mode 100644 index 0000000..54746b3 --- /dev/null +++ b/services/plausible/clickhouse/clickhouse-config.xml @@ -0,0 +1,15 @@ + + + + warning + true + + + + + + + + + + \ No newline at end of file diff --git a/services/plausible/clickhouse/clickhouse-user-config.xml b/services/plausible/clickhouse/clickhouse-user-config.xml new file mode 100644 index 0000000..520794f --- /dev/null +++ b/services/plausible/clickhouse/clickhouse-user-config.xml @@ -0,0 +1,9 @@ + + + + + 0 + 0 + + + \ No newline at end of file diff --git a/services/plausible/conf.env b/services/plausible/conf.env new file mode 100644 index 0000000..ce2fbee --- /dev/null +++ b/services/plausible/conf.env @@ -0,0 +1,5 @@ +ADMIN_USER_EMAIL=replace-me +ADMIN_USER_NAME=replace-me +ADMIN_USER_PWD=replace-me +BASE_URL=replace-me +SECRET_KEY_BASE=replace-me \ No newline at end of file diff --git a/services/plausible/index.sh b/services/plausible/index.sh new file mode 100644 index 0000000..96742ff --- /dev/null +++ b/services/plausible/index.sh @@ -0,0 +1,13 @@ +SNAME=plausible + +# -- creating folder for Plausible service +DIR=services/$SNAME +if [ ! -d "$DIR" ]; then + mkdir $DIR +fi + +# -- creating folder for storing data +DIR_DATA=$DIR/data +if [ ! -d "$DIR_DATA" ]; then + mkdir $DIR_DATA +fi \ No newline at end of file diff --git a/run/services/postgresql.sh b/services/postgresql/index.sh similarity index 87% rename from run/services/postgresql.sh rename to services/postgresql/index.sh index b1e5992..06b1a17 100644 --- a/run/services/postgresql.sh +++ b/services/postgresql/index.sh @@ -1,5 +1,7 @@ +SNAME=postgresql + # -- creating folder for PostgreSQL service -DIR=services/postgresql +DIR=services/$SNAME if [ ! -d "$DIR" ]; then mkdir $DIR fi diff --git a/run/services/redis.sh b/services/redis/index.sh similarity index 88% rename from run/services/redis.sh rename to services/redis/index.sh index 421cb2f..2e072dd 100644 --- a/run/services/redis.sh +++ b/services/redis/index.sh @@ -1,5 +1,7 @@ +SNAME=redis + # -- creating folder for Redis service -DIR=services/redis +DIR=services/$SNAME if [ ! -d "$DIR" ]; then mkdir $DIR fi diff --git a/setup.sh b/setup.sh index 184a75b..35acd8b 100644 --- a/setup.sh +++ b/setup.sh @@ -1,11 +1,11 @@ # -- running services' bash files -BASH_SYSTEM_FILES="bash run/acme.sh; bash run/data.sh;" +BASH_GENERAL_FILES="bash run/acme.sh; bash run/data.sh;" BASH_FILES="" -for F in `find ./run/services -maxdepth 1 -type f -name "*.sh"` +for F in `find ./services -maxdepth 2 -type f -name "index.sh"` do BASH_FILES="$BASH_FILES bash $F;" done -eval "$BASH_SYSTEM_FILES $BASH_FILES" +eval "$BASH_GENERAL_FILES $BASH_FILES" # -- .env.example -> .env ENVEXF=.env.example From 1a075bea6e4a7c9ee39fe7461d268378bfb9d7e2 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 18:08:29 +0200 Subject: [PATCH 28/34] Create verify_emails.sh --- services/plausible/verify_emails.sh | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 services/plausible/verify_emails.sh diff --git a/services/plausible/verify_emails.sh b/services/plausible/verify_emails.sh new file mode 100644 index 0000000..9ebef34 --- /dev/null +++ b/services/plausible/verify_emails.sh @@ -0,0 +1,2 @@ +# as stated here -- https://plausible.io/docs/self-hosting#3-start-the-server +docker-compose exec plausible_db psql -U postgres -d plausible_db -c "UPDATE users SET email_verified = true;" \ No newline at end of file From 1a013d6d6a0f215cc72e209fe9aaefc73f4190b5 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 19:40:09 +0200 Subject: [PATCH 29/34] improvements --- .env.example | 5 +-- README.md | 40 ++++++++++++------- build.sh | 2 +- docker-compose.main.yml => docker-compose.yml | 0 services/docker-compose.plausible.yml | 9 +++-- setup.sh | 2 +- 6 files changed, 33 insertions(+), 25 deletions(-) rename docker-compose.main.yml => docker-compose.yml (100%) diff --git a/.env.example b/.env.example index 0f339e2..535cd58 100644 --- a/.env.example +++ b/.env.example @@ -11,7 +11,4 @@ POSTGRESQL_DATABASE=db # [REDIS] REDIS_PASSWORD=123456qwerty -REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL - -# [PLAUSIBLE] -PLAUSIBLE_POSTGRES_PASSWORD=123456qwerty \ No newline at end of file +REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL \ No newline at end of file diff --git a/README.md b/README.md index b058e7d..8dbbe26 100644 --- a/README.md +++ b/README.md @@ -96,27 +96,21 @@ The setup uses [Traefik](https://github.com/traefik/traefik), the cloud native a You can always delete existing (if you don't need them) or add your own services, just make sure to apply necessary changes. -### Services - -- [PostgreSQL](https://www.postgresql.org) - open source object-relational database known for reliability and data integrity. It uses [Bitnami Docker image](https://hub.docker.com/r/bitnami/postgresql). -- [Redis](https://redis.io) - open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker. It uses [Bitnami Docker image](https://hub.docker.com/r/bitnami/redis). -- [React App](https://backend.starters.dev) - simple [frontend app](https://github.com/starters-dev/backend.starters.dev-website). - -> more services will be added... - -## Environment variables +### Env variables Current setup requires you to fill in `.env` file with variables that are used in services. -#### General - - `DOMAIN_NAME` - your registered domain. - `DO_AUTH_TOKEN` - Digital Ocean token that is going to be used for DNS challenge and generating https certificates. It's required by Traefik and they provide other options, you can find them [here](https://doc.traefik.io/traefik/https/acme/#providers). If you'd like to continue with Digital Ocean, then you can create a token in `Dashboard` -> `API` -> `Tokens/Keys`. - `ACME_EMAIL` - email that is used for [Let's Encrypt](https://letsencrypt.org) and `https` certificates. - `GITHUB_TOKEN` - `(optional)` github token for private repos. +### Services + +- [PostgreSQL](https://www.postgresql.org) - open source object-relational database known for reliability and data integrity. It uses [Bitnami Docker image](https://hub.docker.com/r/bitnami/postgresql). +
-PostgreSQL +Env variables - `POSTGRESQL_USERNAME` - username for PostgreSQL. - `POSTGRESQL_PASSWORD` - password for PostgreSQL. @@ -124,16 +118,20 @@ Current setup requires you to fill in `.env` file with variables that are used i
+- [Redis](https://redis.io) - open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker. It uses [Bitnami Docker image](https://hub.docker.com/r/bitnami/redis). +
-Redis +Env variables - `REDIS_PASSWORD` - password for Redis. - `REDIS_DISABLE_COMMANDS` - commands disabled from execution.
+- [Plausible](https://plausible.io) - simple and privacy-friendly Google Analytics alternative +
-Plausible +Env variables `.env` file: @@ -151,14 +149,26 @@ Current setup requires you to fill in `.env` file with variables that are used i
+
+Troubleshooting + +- [ClickHouse] _Database plausible_events_db doesn't exist_. If you encounter this issue, check out [this comment](https://github.com/plausible/hosting/issues/49#issuecomment-1229183219) for the solution. + +
+ +- [React App](https://backend.starters.dev) - simple [frontend app](https://github.com/starters-dev/backend.starters.dev-website). + +> more services will be added... + ## Enhancements There are still some things I would like to add to the backend setup: - [x] [PostgreSQL](https://www.postgresql.org) - open source object-relational database known for reliability and data integrity. - [x] [Redis](https://redis.io) - open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker. -- [ ] [Plausible](https://plausible.io) - simple and privacy-friendly Google Analytics alternative. [Github repo](https://github.com/plausible/hosting). +- [x] [Plausible](https://plausible.io) - simple and privacy-friendly Google Analytics alternative. - [ ] [GlitchTip](https://glitchtip.com) - open source reimplementation of Sentry error tracking platform. +- [ ] [Cal.com](https://cal.com) - scheduling infrastructure for absolutely everyone. [Github repo](https://github.com/calcom/docker). - [ ] [Mattermost](https://mattermost.com) - open source platform for developer collaboration. [Github repo](https://github.com/starters-dev/mattermost). - [ ] [Taiga](https://www.taiga.io) - open source, self-hosted project management tool. [Github repo](https://github.com/starters-dev/taiga). - [ ] [Focalboard](https://focalboard.com) - open source, self-hosted alternative to Trello, Notion, and Asana. [Github repo](https://github.com/starters-dev/focalboard). diff --git a/build.sh b/build.sh index 5707155..38bfacc 100644 --- a/build.sh +++ b/build.sh @@ -1,5 +1,5 @@ # -- getting docker-compose files -DC_FILES="-f docker-compose.main.yml" +DC_FILES="-f docker-compose.yml" for F in `find ./services -maxdepth 1 -type f -name "docker-compose*.yml"` do DC_FILES="$DC_FILES -f $F " diff --git a/docker-compose.main.yml b/docker-compose.yml similarity index 100% rename from docker-compose.main.yml rename to docker-compose.yml diff --git a/services/docker-compose.plausible.yml b/services/docker-compose.plausible.yml index b92744b..ddb127e 100644 --- a/services/docker-compose.plausible.yml +++ b/services/docker-compose.plausible.yml @@ -5,6 +5,8 @@ services: mail: image: bytemark/smtp restart: always + labels: + - "traefik.enable=false" plausible_db: image: postgres:14-alpine @@ -12,7 +14,7 @@ services: volumes: - ./services/plausible/data:/var/lib/postgresql/data environment: - - POSTGRES_PASSWORD=${PLAUSIBLE_POSTGRES_PASSWORD} + - POSTGRES_PASSWORD=postgres labels: - "traefik.enable=false" @@ -38,8 +40,8 @@ services: - plausible_db - plausible_events_db - mail - # ports: - # - 8000:8000 + expose: + - 8000 env_file: - ./services/plausible/conf.env labels: @@ -51,7 +53,6 @@ services: - "traefik.http.routers.plausible_tls.rule=Host(`plausible.${DOMAIN_NAME}`)" - "traefik.http.routers.plausible_tls.entryPoints=https" - "traefik.http.routers.plausible_tls.tls.certresolver=mydnschallenge" - - "traefik.http.services.plausible.loadbalancer.server.port=8000" volumes: db-data: diff --git a/setup.sh b/setup.sh index 35acd8b..db2cc9d 100644 --- a/setup.sh +++ b/setup.sh @@ -1,5 +1,5 @@ # -- running services' bash files -BASH_GENERAL_FILES="bash run/acme.sh; bash run/data.sh;" +BASH_GENERAL_FILES="bash run/acme.sh;" BASH_FILES="" for F in `find ./services -maxdepth 2 -type f -name "index.sh"` do From 479fbd2dae6834923d801c03bb340769ee2f02b5 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 19:47:08 +0200 Subject: [PATCH 30/34] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8dbbe26..f888a4c 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,8 @@ Current setup requires you to fill in `.env` file with variables that are used i - [Plausible](https://plausible.io) - simple and privacy-friendly Google Analytics alternative +> [Official docs](https://plausible.io/docs/self-hosting). +
Env variables @@ -145,8 +147,6 @@ Current setup requires you to fill in `.env` file with variables that are used i - `ADMIN_USER_NAME` - admin usenamename. - `ADMIN_USER_PWD` - admin password. -> [Original docs](https://plausible.io/docs/self-hosting). -
From 62485fc07af9fe0a4948575b5ad3df82e93d6194 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 20:01:47 +0200 Subject: [PATCH 31/34] imprv --- README.md | 45 ++++++++++++++++++----------- services/plausible/verify_emails.sh | 2 -- 2 files changed, 28 insertions(+), 19 deletions(-) delete mode 100644 services/plausible/verify_emails.sh diff --git a/README.md b/README.md index f888a4c..02c6eb0 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ ![HTTPS](https://img.shields.io/badge/HTTPS-Configured-green) ![PostgreSQL](https://img.shields.io/badge/Database-PostgreSQL-blue) ![Redis](https://img.shields.io/badge/Cache-Redis-red) +![Plausible](https://img.shields.io/badge/Analytics-Plausible-purple) ![Website](https://img.shields.io/badge/Website-React%20App-blue) This repository is a minimalistic setup for running services with Docker Compose behind Traefik and autogenerated `https` certificates. @@ -96,18 +97,13 @@ The setup uses [Traefik](https://github.com/traefik/traefik), the cloud native a You can always delete existing (if you don't need them) or add your own services, just make sure to apply necessary changes. -### Env variables +### Services -Current setup requires you to fill in `.env` file with variables that are used in services. +--- -- `DOMAIN_NAME` - your registered domain. -- `DO_AUTH_TOKEN` - Digital Ocean token that is going to be used for DNS challenge and generating https certificates. It's required by Traefik and they provide other options, you can find them [here](https://doc.traefik.io/traefik/https/acme/#providers). If you'd like to continue with Digital Ocean, then you can create a token in `Dashboard` -> `API` -> `Tokens/Keys`. -- `ACME_EMAIL` - email that is used for [Let's Encrypt](https://letsencrypt.org) and `https` certificates. -- `GITHUB_TOKEN` - `(optional)` github token for private repos. +[PostgreSQL](https://www.postgresql.org) - open source object-relational database known for reliability and data integrity. -### Services - -- [PostgreSQL](https://www.postgresql.org) - open source object-relational database known for reliability and data integrity. It uses [Bitnami Docker image](https://hub.docker.com/r/bitnami/postgresql). +It uses [Bitnami Docker image](https://hub.docker.com/r/bitnami/postgresql).
Env variables @@ -118,7 +114,11 @@ Current setup requires you to fill in `.env` file with variables that are used i
-- [Redis](https://redis.io) - open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker. It uses [Bitnami Docker image](https://hub.docker.com/r/bitnami/redis). +--- + +[Redis](https://redis.io) - open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker. + +It uses [Bitnami Docker image](https://hub.docker.com/r/bitnami/redis).
Env variables @@ -128,17 +128,15 @@ Current setup requires you to fill in `.env` file with variables that are used i
-- [Plausible](https://plausible.io) - simple and privacy-friendly Google Analytics alternative +--- + +[Plausible](https://plausible.io) - simple and privacy-friendly Google Analytics alternative. -> [Official docs](https://plausible.io/docs/self-hosting). +[Official documentation](https://plausible.io/docs/self-hosting).
Env variables -`.env` file: - -- `PLAUSIBLE_POSTGRES_PASSWORD` - password for Plausible PostgreSQL. - `services/plausible/conf.env` file: - `SECRET_KEY_BASE` - secret key of your app. Can be generated by running `openssl rand -base64 64 | tr -d '\n' ; echo`. @@ -156,10 +154,23 @@ Current setup requires you to fill in `.env` file with variables that are used i
-- [React App](https://backend.starters.dev) - simple [frontend app](https://github.com/starters-dev/backend.starters.dev-website). +--- + +[React App](https://backend.starters.dev) - simple [frontend app](https://github.com/starters-dev/backend.starters.dev-website). + +--- > more services will be added... +### Env variables + +Current setup requires you to fill in `.env` file with variables that are used in services. + +- `DOMAIN_NAME` - your registered domain. +- `DO_AUTH_TOKEN` - Digital Ocean token that is going to be used for DNS challenge and generating https certificates. It's required by Traefik and they provide other options, you can find them [here](https://doc.traefik.io/traefik/https/acme/#providers). If you'd like to continue with Digital Ocean, then you can create a token in `Dashboard` -> `API` -> `Tokens/Keys`. +- `ACME_EMAIL` - email that is used for [Let's Encrypt](https://letsencrypt.org) and `https` certificates. +- `GITHUB_TOKEN` - `(optional)` github token for private repos. + ## Enhancements There are still some things I would like to add to the backend setup: diff --git a/services/plausible/verify_emails.sh b/services/plausible/verify_emails.sh deleted file mode 100644 index 9ebef34..0000000 --- a/services/plausible/verify_emails.sh +++ /dev/null @@ -1,2 +0,0 @@ -# as stated here -- https://plausible.io/docs/self-hosting#3-start-the-server -docker-compose exec plausible_db psql -U postgres -d plausible_db -c "UPDATE users SET email_verified = true;" \ No newline at end of file From db2f0ef032cef7a991b92bce38b2560882f7ec5b Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 20:11:11 +0200 Subject: [PATCH 32/34] better structure --- .env.example | 9 --------- services/docker-compose.postgresql.yml | 6 ++---- services/docker-compose.redis.yml | 5 ++--- services/plausible/conf.env | 1 + services/postgresql/conf.env | 4 ++++ services/redis/conf.env | 3 +++ setup.sh | 3 +++ 7 files changed, 15 insertions(+), 16 deletions(-) create mode 100644 services/postgresql/conf.env create mode 100644 services/redis/conf.env diff --git a/.env.example b/.env.example index 535cd58..d74b496 100644 --- a/.env.example +++ b/.env.example @@ -3,12 +3,3 @@ DOMAIN_NAME=your-website.com DO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxx # for DNS challenge ACME_EMAIL=email@your-website.com # for let's encrypt GITHUB_TOKEN=xxxxxxxxxxxxxxxxxxxxxx # (optional) for private repos - -# [POSTGRES] -POSTGRESQL_USERNAME=admin -POSTGRESQL_PASSWORD=123456qwerty -POSTGRESQL_DATABASE=db - -# [REDIS] -REDIS_PASSWORD=123456qwerty -REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL \ No newline at end of file diff --git a/services/docker-compose.postgresql.yml b/services/docker-compose.postgresql.yml index 7573c96..de07abd 100644 --- a/services/docker-compose.postgresql.yml +++ b/services/docker-compose.postgresql.yml @@ -4,10 +4,8 @@ services: postgresql: image: bitnami/postgresql:latest restart: always - environment: - - POSTGRESQL_USERNAME=${POSTGRESQL_USERNAME} - - POSTGRESQL_PASSWORD=${POSTGRESQL_PASSWORD} - - POSTGRESQL_DATABASE=${POSTGRESQL_DATABASE} + env_file: + - ./services/postgresql/conf.env volumes: - ./services/postgresql/data:/bitnami/postgresql labels: diff --git a/services/docker-compose.redis.yml b/services/docker-compose.redis.yml index aafe6bf..b803e54 100644 --- a/services/docker-compose.redis.yml +++ b/services/docker-compose.redis.yml @@ -4,9 +4,8 @@ services: redis: image: bitnami/redis:latest restart: always - environment: - - REDIS_PASSWORD=${REDIS_PASSWORD} - - REDIS_DISABLE_COMMANDS=${REDIS_DISABLE_COMMANDS} + env_file: + - ./services/redis/conf.env volumes: - ./services/redis/data:/bitnami/redis/data labels: diff --git a/services/plausible/conf.env b/services/plausible/conf.env index ce2fbee..486c859 100644 --- a/services/plausible/conf.env +++ b/services/plausible/conf.env @@ -1,3 +1,4 @@ +# [PLAUSIBLE] ADMIN_USER_EMAIL=replace-me ADMIN_USER_NAME=replace-me ADMIN_USER_PWD=replace-me diff --git a/services/postgresql/conf.env b/services/postgresql/conf.env new file mode 100644 index 0000000..832b30b --- /dev/null +++ b/services/postgresql/conf.env @@ -0,0 +1,4 @@ +# [POSTGRES] +POSTGRESQL_USERNAME=admin +POSTGRESQL_PASSWORD=123456qwerty +POSTGRESQL_DATABASE=db \ No newline at end of file diff --git a/services/redis/conf.env b/services/redis/conf.env new file mode 100644 index 0000000..fed14a5 --- /dev/null +++ b/services/redis/conf.env @@ -0,0 +1,3 @@ +# [REDIS] +REDIS_PASSWORD=123456qwerty +REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL \ No newline at end of file diff --git a/setup.sh b/setup.sh index db2cc9d..2a5a63c 100644 --- a/setup.sh +++ b/setup.sh @@ -11,4 +11,7 @@ eval "$BASH_GENERAL_FILES $BASH_FILES" ENVEXF=.env.example if [ -f "$ENVEXF" ]; then mv .env.example .env + + echo "Don't forget to fill in .env file!" + echo " > nano .env" fi \ No newline at end of file From 404656bf31f73f1f3feb4cb5ccfd4d7c3ea126f9 Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 22:18:35 +0200 Subject: [PATCH 33/34] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 02c6eb0..120e1fb 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ![PostgreSQL](https://img.shields.io/badge/Database-PostgreSQL-blue) ![Redis](https://img.shields.io/badge/Cache-Redis-red) ![Plausible](https://img.shields.io/badge/Analytics-Plausible-purple) -![Website](https://img.shields.io/badge/Website-React%20App-blue) +![Website](https://img.shields.io/badge/Frontend-React%20App-blue) This repository is a minimalistic setup for running services with Docker Compose behind Traefik and autogenerated `https` certificates. From 7a6eff104083fb6be20d88094178ae5c18af6d0f Mon Sep 17 00:00:00 2001 From: Batyr Kanzitdinov Date: Sun, 23 Oct 2022 22:21:07 +0200 Subject: [PATCH 34/34] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 120e1fb..5f3d288 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ If you'd like to add your own service, then you should follow the steps below to 2. `(optional)` Add service's folder `services/YOUR_SERVICE`. For example, if you are going to store some data or configuration files only related to that service. -3. `(optional)` Add service's script file `run/services/YOUR_SERVICE.sh`. For example, if you need to craete some folders in advance on `setup` step. +3. `(optional)` Add service's script file `services/YOUR_SERVICE/index.sh`. For example, if you need to craete some folders in advance on `setup` step. ### Service deletion @@ -87,7 +87,6 @@ The folder structure is pretty simple but lets you reuse and write less boilerpl - `build.sh` - `build` script that runs `setup` script with all scripts under `run/services` folder and builds and runs Docker images in background. - `.env` - list of environment variables. - `run/` - folder with general bash scripts. -- `run/services/` - folder with bash scripts for a specific service. - `services/` - folder with Docker services files. For example, if you have `redis` service, then you would have `services/docker-compose.redis.yml` with specific configuration for `redis` service. Or if your service is a frontend app, then you'll need to clone it to `services/frontend/` and create `services/docker-compose.frontend.yml`. - `services/your-service/` - folder with your service, for ex. `frontend`.