This commit is contained in:
Philip Cheung 2024-10-28 19:13:10 +08:00
parent 44212834ac
commit e073cefe73
3 changed files with 24 additions and 85 deletions

2
.env
View File

@ -1,6 +1,6 @@
# Domain
# This would be set to the production domain with an env var on deployment
DOMAIN=onandallmusic.net
DOMAIN=oneandallmusic.net
# Environment: local, staging, production
ENVIRONMENT=production

View File

@ -1,106 +1,45 @@
# FastAPI Project - Deployment
# Public Traefik
You can deploy the project using Docker Compose to a remote server.
This project expects you to have a Traefik proxy handling communication to the outside world and HTTPS certificates.
You can use CI/CD (continuous integration and continuous deployment) systems to deploy automatically, there are already configurations to do it with GitHub Actions.
But you have to configure a couple things first. 🤓
## Preparation
* Have a remote server ready and available.
* Configure the DNS records of your domain to point to the IP of the server you just created.
* Configure a wildcard subdomain for your domain, so that you can have multiple subdomains for different services, e.g. `*.fastapi-project.example.com`. This will be useful for accessing different components, like `traefik.fastapi-project.example.com`, `adminer.fastapi-project.example.com`, etc. And also for `staging`, like `staging.fastapi-project.example.com`, `staging.adminer.fastapi-project.example.com`, etc.
* Install and configure [Docker](https://docs.docker.com/engine/install/) on the remote server (Docker Engine, not Docker Desktop).
## Public Traefik
We need a Traefik proxy to handle incoming connections and HTTPS certificates.
You need to do these next steps only once.
### Traefik Docker Compose
* Create a remote directory to store your Traefik Docker Compose file:
Go to local usr/local/
```bash
mkdir -p /root/code/traefik-public/
mkdir traefik-public
```
Copy the Traefik Docker Compose file to your server. You could do it by running the command `rsync` in your local terminal:
```bash
rsync -a docker-compose.traefik.yml root@your-server.example.com:/root/code/traefik-public/
mkdir web_backend
```
### Traefik Public Network
```bash
cd web_backend
```
This Traefik will expect a Docker "public network" named `traefik-public` to communicate with your stack(s).
```bash
git init
```
This way, there will be a single public Traefik proxy that handles the communication (HTTP and HTTPS) with the outside world, and then behind that, you could have one or more stacks with different domains, even if they are on the same single server.
```bash
git pull https://git.develop-cat.com/all_in_one/backend_and_cms.git
```
To create a Docker "public network" named `traefik-public` run the following command in your remote server:
```bash
cp docker-compose.traefik.yml ../traefik-public
```
```bash
cd ../
```
```bash
docker network create traefik-public
```
### Traefik Environment Variables
The Traefik Docker Compose file expects some environment variables to be set in your terminal before starting it. You can do it by running the following commands in your remote server.
* Create the username for HTTP Basic Auth, e.g.:
```bash
export USERNAME=admin
cd traefik-public
```
* Create an environment variable with the password for HTTP Basic Auth, e.g.:
```bash
export PASSWORD=changethis
```
* Use openssl to generate the "hashed" version of the password for HTTP Basic Auth and store it in an environment variable:
```bash
export HASHED_PASSWORD=$(openssl passwd -apr1 $PASSWORD)
```
To verify that the hashed password is correct, you can print it:
```bash
echo $HASHED_PASSWORD
```
* Create an environment variable with the domain name for your server, e.g.:
```bash
export DOMAIN=fastapi-project.example.com
```
* Create an environment variable with the email for Let's Encrypt, e.g.:
```bash
export EMAIL=admin@example.com
```
**Note**: you need to set a different email, an email `@example.com` won't work.
### Start the Traefik Docker Compose
Go to the directory where you copied the Traefik Docker Compose file in your remote server:
```bash
cd /root/code/traefik-public/
```
Now with the environment variables set and the `docker-compose.traefik.yml` in place, you can start the Traefik Docker Compose running the following command:
```bash
docker compose -f docker-compose.traefik.yml up -d
USERNAME=admin PASSWORD=Wingwingk3 DOMAIN=oneandallmusic.net EMAIL=oneandall.music@gmail.com HASHED_PASSWORD=$(openssl passwd -apr1 $PASSWORD) docker compose -f docker-compose.traefik.yml up -d
```
## Deploy the FastAPI Project

View File

@ -1,2 +1,2 @@
VITE_API_URL=https://localhost
VITE_IMAGE_URL=https://images.onandallmusic.net
VITE_IMAGE_URL=https://images.oneandallmusic.net