Python FastAPI Tutorial (Part 18): Deploy to a VPS - Security, Nginx, SSL, and Custom Domain

| Programming | April 19, 2026 | 817 views | 1:32:05

TL;DR

Corey Schafer demonstrates how to deploy a production-ready FastAPI application to a Virtual Private Server (VPS), emphasizing fundamental deployment concepts including security hardening, SSH key authentication, and health check implementation before moving to managed cloud solutions.

🚀 VPS Fundamentals & Application Readiness 2 insights

Start with VPS to learn universal deployment concepts

Beginning with manual VPS deployment teaches underlying infrastructure fundamentals that transfer to any provider (Linode, AWS, DigitalOcean) and significantly improves debugging capabilities when using managed services later.

Implement database health check endpoints

Add a health check route that executes SQLAlchemy's `text("SELECT 1")` query to verify database connectivity, returning HTTP 503 when unreachable to signal load balancers and monitoring systems to redirect traffic.

⚙️ Server Provisioning 2 insights

Select cost-effective Ubuntu LTS instances

Provision an Ubuntu 24.04 LTS server with a $5/month shared CPU plan in a geographic region closest to your users, ensuring long-term support for security updates.

Immediately update system packages

Execute `apt update && apt upgrade` upon first login to install the latest security patches and bug fixes before configuring any services.

🔒 Security Hardening & Access Control 3 insights

Create non-root user with sudo privileges

Immediately create a dedicated non-root user with sudo access to limit the 'blast radius' of potential security breaches, avoiding the unlimited system power of the root account for daily operations.

Configure ED25519 SSH key authentication

Generate modern ED25519 SSH key pairs for passwordless login, which provide superior security and performance compared to legacy RSA keys or password-based authentication methods.

Deploy code via GitHub with proper ignore rules

Push application code to GitHub with a comprehensive .gitignore file excluding sensitive .env files, then clone the repository onto the server rather than using manual file transfer methods.

Bottom Line

Master fundamental VPS deployment with SSH key authentication and non-root user security before adopting containerized solutions, as these core skills remain essential for debugging and securing any production web application.

More from Corey Schafer

View all
Python FastAPI Tutorial (Part 17): Testing the API - Pytest, Fixtures, and Mocking External Services
1:22:26
Corey Schafer Corey Schafer

Python FastAPI Tutorial (Part 17): Testing the API - Pytest, Fixtures, and Mocking External Services

This tutorial demonstrates how to implement comprehensive testing for FastAPI applications using pytest with async support, covering critical setup patterns like environment variable configuration before app imports, using AsyncClient for async endpoints, mocking AWS S3 with Moto, and maintaining a separate PostgreSQL test database to ensure production parity.

2 days ago · 9 points