Deployment Guide
This guide will walk you through deploying the entire stack (back-end, front-end, and database) for the Craft Your Startup Boilerplate project on DigitalOcean App Platform. We'll also cover setting up a PostgreSQL database instance, configuring DigitalOcean Spaces for object storage, and running migrations from your local machine using the prod.env
file.
Table of Contents
- Deploying to DigitalOcean App Platform
- Setting Up PostgreSQL Database on DigitalOcean
- Running Database Migrations from Your Local Machine
- Using DigitalOcean Spaces for Object Storage
- Best Practices for Security and Scaling
Deploying to DigitalOcean App Platform
DigitalOcean App Platform automatically detects and builds applications based on your GitHub repository, handling the entire deployment process for you.
Steps to Deploy the Full Stack:
- Create a DigitalOcean Account:
Sign up for DigitalOcean if you don't already have an account.
-
Create a New App on DigitalOcean App Platform:
-
Navigate to the Apps section in the DigitalOcean dashboard.
- Click Create App.
-
Select GitHub as the source and link your repository where the Craft Your Startup Boilerplate project is hosted.
-
Automatic Build Detection:
DigitalOcean App Platform will automatically detect the Dockerfile in your repository and use it to build your project. The Dockerfile is responsible for installing dependencies, building the front-end, and serving both the front-end and back-end using FastAPI.
-
Set Environment Variables:
In the Environment Variables section of the App Platform, set the necessary environment variables, mirroring the prod.env
file that you use locally. Here's an example of how the environment variables should look:
db_username=craftyourstartup
db_password=craftyourstartup
db_host=<your-database-host>
db_port=<your-database-port>
db_database=craftyourstartup
db_sslmode=require
redirect_after_login=https://your-production-url.com
support_email=[email protected]
aws_access_key_id=<your-spaces-access-key>
aws_secret_access_key=<your-spaces-secret-key>
google_oauth2_client_id=xxx
google_oauth2_secret=xxx
stripe_api_key=xxx
stripe_webhook_secret=xxx
mailchimp_api_key=xxx
env=prod
These values should reflect your production configuration. The aws_access_key_id
and aws_secret_access_key
are for DigitalOcean Spaces (acting as a replacement for AWS S3), and the redirect_after_login
URL should point to your production front-end.
-
Deploy the App:
Once your environment variables are set, click Deploy. DigitalOcean will automatically build and deploy the app, making it accessible through a public URL.
Setting Up PostgreSQL Database on DigitalOcean
-
Create a Managed PostgreSQL Database:
-
In the Databases section of your DigitalOcean dashboard, click Create Database Cluster.
- Select PostgreSQL as the database engine.
-
Choose your desired configuration (region, pricing tier, etc.).
-
Retrieve Database Credentials:
After the database is created, you’ll receive connection details such as:
- Host
- Port
- Username
- Password
- Database name
Update these values in your prod.env
file and ensure they are reflected in the environment variables set in DigitalOcean App Platform.
Running Database Migrations from Your Local Machine
You can run database migrations from your local machine using the prod.env
file to connect to the production database on DigitalOcean.
Steps to Run Migrations Locally:
- Ensure your
prod.env
file is set up correctly with the production database credentials:
db_username=craftyourstartup
db_password=craftyourstartup
db_host=<your-database-host>
db_port=<your-database-port>
db_database=craftyourstartup
db_sslmode=require
- Run migrations locally by pointing to the production database:
This command will apply the latest migrations to your production PostgreSQL database on DigitalOcean.
Using DigitalOcean Spaces for Object Storage
DigitalOcean Spaces is a cost-effective object storage solution, fully compatible with the S3 API.
Steps to Set Up DigitalOcean Spaces:
-
Create a Space:
- Go to the Spaces section in the DigitalOcean dashboard and create a new Space.
- Select a region (e.g.,
nyc3
) and note the Access Key, Secret Key, and Endpoint URL.
-
Configure Environment Variables:
Set the following environment variables in your
prod.env
file and ensure they are reflected in your DigitalOcean App Platform configuration: -
Back-End Configuration:
Modify your FastAPI back-end to use DigitalOcean Spaces for object storage. Here’s a sample configuration using
boto3
for Spaces (S3-compatible API):import boto3 spaces_client = boto3.client( "s3", region_name="nyc3", endpoint_url="https://nyc3.digitaloceanspaces.com", aws_access_key_id=os.getenv("aws_access_key_id"), aws_secret_access_key=os.getenv("aws_secret_access_key"), ) def upload_to_spaces(file_name, file_data): spaces_client.put_object( Bucket=os.getenv("SPACES_BUCKET_NAME"), Key=file_name, Body=file_data, ACL="public-read", )
This code uploads files to the specified Space.
Best Practices for Security and Scaling
-
Use SSL/TLS:
Ensure your app is using HTTPS. DigitalOcean App Platform automatically configures SSL certificates for you, so make sure your domain is correctly set up.
-
Database Backups:
Enable automatic backups for your PostgreSQL database. You can configure this in the Databases section of the DigitalOcean dashboard to ensure your data is secure.
-
Scaling:
DigitalOcean App Platform supports horizontal scaling. You can scale your app by adding more instances via the Scaling settings in the App Platform dashboard.
-
Monitoring and Alerts:
Set up monitoring and alerts in DigitalOcean to keep track of your app's performance. You can configure alerts for resource usage, such as CPU and memory, to ensure your app runs smoothly.
By following this guide, you'll have your project deployed on DigitalOcean App Platform, connected to a managed PostgreSQL database, and using DigitalOcean Spaces for object storage. The setup is simple, secure, and scalable, with built-in support for cloud services.