Skip to content

Commit f439098

Browse files
committed
📝 Update deployment docs
1 parent 9ba7930 commit f439098

File tree

1 file changed

+51
-16
lines changed

1 file changed

+51
-16
lines changed

deployment.md

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,32 @@ Now that you have Traefik in place you can deploy your FastAPI project with Dock
109109

110110
**Note**: You might want to jump ahead to the section about Continuous Deployment with GitHub Actions.
111111

112+
## Copy the Code
113+
114+
```bash
115+
rsync -av --filter=":- .gitignore" ./ root@your-server.example.com:/root/code/app/
116+
```
117+
118+
Note: `--filter=":- .gitignore"` tells `rsync` to use the same rules as git, ignore files ignored by git, like the Python virtual environment.
119+
112120
## Environment Variables
113121

114122
You need to set some environment variables first.
115123

124+
### Generate secret keys
125+
126+
Some environment variables in the `.env` file have a default value of `changethis`.
127+
128+
You have to change them with a secret key, to generate secret keys you can run the following command:
129+
130+
```bash
131+
python -c "import secrets; print(secrets.token_urlsafe(32))"
132+
```
133+
134+
Copy the content and use that as password / secret key. And run that again to generate another secure key.
135+
136+
### Required Environment Variables
137+
116138
Set the `ENVIRONMENT`, by default `local` (for development), but when deploying to a server you would put something like `staging` or `production`:
117139

118140
```bash
@@ -125,21 +147,44 @@ Set the `DOMAIN`, by default `localhost` (for development), but when deploying y
125147
export DOMAIN=fastapi-project.example.com
126148
```
127149

128-
You can set several variables, like:
150+
Set the `POSTGRES_PASSWORD` to something different than `changethis`:
151+
152+
```bash
153+
export POSTGRES_PASSWORD="changethis"
154+
```
155+
156+
Set the `SECRET_KEY`, used to sign tokens:
157+
158+
```bash
159+
export SECRET_KEY="changethis"
160+
```
161+
162+
Note: you can use the Python command above to generate a secure secret key.
163+
164+
Set the `FIRST_SUPER_USER_PASSWORD` to something different than `changethis`:
165+
166+
```bash
167+
export FIRST_SUPERUSER_PASSWORD="changethis"
168+
```
169+
170+
Set the `BACKEND_CORS_ORIGINS` to include your domain:
171+
172+
```bash
173+
export BACKEND_CORS_ORIGINS="https://dashboard.${DOMAIN?Variable not set},https://api.${DOMAIN?Variable not set}"
174+
```
175+
176+
You can set several other environment variables:
129177

130178
* `PROJECT_NAME`: The name of the project, used in the API for the docs and emails.
131179
* `STACK_NAME`: The name of the stack used for Docker Compose labels and project name, this should be different for `staging`, `production`, etc. You could use the same domain replacing dots with dashes, e.g. `fastapi-project-example-com` and `staging-fastapi-project-example-com`.
132180
* `BACKEND_CORS_ORIGINS`: A list of allowed CORS origins separated by commas.
133-
* `SECRET_KEY`: The secret key for the FastAPI project, used to sign tokens.
134181
* `FIRST_SUPERUSER`: The email of the first superuser, this superuser will be the one that can create new users.
135-
* `FIRST_SUPERUSER_PASSWORD`: The password of the first superuser.
136182
* `SMTP_HOST`: The SMTP server host to send emails, this would come from your email provider (E.g. Mailgun, Sparkpost, Sendgrid, etc).
137183
* `SMTP_USER`: The SMTP server user to send emails.
138184
* `SMTP_PASSWORD`: The SMTP server password to send emails.
139185
* `EMAILS_FROM_EMAIL`: The email account to send emails from.
140186
* `POSTGRES_SERVER`: The hostname of the PostgreSQL server. You can leave the default of `db`, provided by the same Docker Compose. You normally wouldn't need to change this unless you are using a third-party provider.
141187
* `POSTGRES_PORT`: The port of the PostgreSQL server. You can leave the default. You normally wouldn't need to change this unless you are using a third-party provider.
142-
* `POSTGRES_PASSWORD`: The Postgres password.
143188
* `POSTGRES_USER`: The Postgres user, you can leave the default.
144189
* `POSTGRES_DB`: The database name to use for this application. You can leave the default of `app`.
145190
* `SENTRY_DSN`: The DSN for Sentry, if you are using it.
@@ -151,23 +196,13 @@ There are some environment variables only used by GitHub Actions that you can co
151196
* `LATEST_CHANGES`: Used by the GitHub Action [latest-changes](https://github.com/tiangolo/latest-changes) to automatically add release notes based on the PRs merged. It's a personal access token, read the docs for details.
152197
* `SMOKESHOW_AUTH_KEY`: Used to handle and publish the code coverage using [Smokeshow](https://github.com/samuelcolvin/smokeshow), follow their instructions to create a (free) Smokeshow key.
153198

154-
### Generate secret keys
155-
156-
Some environment variables in the `.env` file have a default value of `changethis`.
157-
158-
You have to change them with a secret key, to generate secret keys you can run the following command:
159-
160-
```bash
161-
python -c "import secrets; print(secrets.token_urlsafe(32))"
162-
```
163-
164-
Copy the content and use that as password / secret key. And run that again to generate another secure key.
165-
166199
### Deploy with Docker Compose
167200

168201
With the environment variables in place, you can deploy with Docker Compose:
169202

170203
```bash
204+
cd /root/code/app/
205+
docker compose -f compose.yml build
171206
docker compose -f compose.yml up -d
172207
```
173208

0 commit comments

Comments
 (0)