I Created A Bash Script To Quickly Deploy Fastapi To Any Vps
Welcome to this tutorial where we'll walk through creating a bash script for deploying FastAPI applications on any Virtual Private Server (VPS). This self-hosted infrastructure setup is ideal for your.
# I Created a Bash Script to Quickly Deploy FastAPI on Any VPS
Welcome to this tutorial where we’ll walk through creating a bash script for deploying FastAPI applications on any Virtual Private Server (VPS). This self-hosted infrastructure setup is ideal for your homelab and offers the advantage of automation in DevOps environments. Let’s dive in!
Prerequisites
To follow along, ensure you have the following tools installed:
- Ubuntu 20.04 LTS or newer (or equivalent Debian-based distribution)
- Docker CE 5.0.8 (installation guide)
- Git 2.17.1 (installation guide)
- curl and wget (usually installed by default on Ubuntu)
- Bash 5.0 or newer (usually installed by default on Ubuntu)
- Python 3.7 or newer (installation guide)
- pip, Python’s package installer (installation guide)
Solution Steps
1. Install FastAPI and Uvicorn
We’ll use Uvicorn, the recommended ASGI server for FastAPI, to run our application. First, let’s install FastAPI and Uvicorn:
1
pip install fastapi uvicorn
2. Write your FastAPI Application
Create a new file named app.py
containing your FastAPI application code. Here’s a simple example:
1
2
3
4
5
6
7
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
3. Create the Deployment Script
Create a new file called deploy.sh
with the following content:
1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
# Environment Variables
export APP_NAME="your-app-name"
export APP_PORT=8000
export Dockerfile_path="/path/to/Dockerfile"
# Build the Docker image
docker build -t $APP_NAME:latest $Dockerfile_path
# Run the built image as a service
docker run -d --name $APP_NAME -p $APP_PORT:$APP_PORT $APP_NAME
Replace /path/to/Dockerfile
with the actual path to your Dockerfile.
4. Create the Dockerfile
Create a new file named Dockerfile
in the same directory as your FastAPI application (app.py
):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FROM python:3.9-slim-buster
# Set working directory to /app
WORKDIR /app
# Copy the requirements.txt file into the container
COPY requirements.txt ./
# Install dependencies
RUN pip install -r requirements.txt
# Copy the FastAPI application code into the container
COPY . /app
# Run Uvicorn when container starts
CMD [ "uvicorn", "--host", "0.0.0.0", "--port", "8000", "-r", "5", $APP_NAME.__file__ ]
5. Create a requirements.txt file
Create a new file named requirements.txt
in the same directory as your application (app.py
) and include any required Python packages:
1
2
fastapi==0.67.0
uvicorn[standard]==0.15.0
Replace the versions with the ones you need for your project.
6. Make the Script Executable
Make sure your deploy.sh
file is executable:
1
chmod +x deploy.sh
Troubleshooting
- Docker not found: Ensure Docker is installed and added to your system’s PATH.
- Permission denied errors: Run the script with sudo or adjust the file permissions to be executable by the user running the script.
Conclusion
This bash script provides a simple yet effective means of deploying FastAPI applications on any VPS, allowing for easy automation in your DevOps infrastructure. Incorporate performance optimization tips and security considerations according to your project’s requirements. Keep experimenting and optimizing!
Happy deploying! 🚀🐳🐾