DEV Community

shailendra khade
shailendra khade

Posted on

Building My First REST API on Linux VM using FastAPI (with Real DevOps Troubleshooting)

Introduction

As a DevOps & AI enthusiast, I wanted to understand how APIs actually work at the system level — not just theory, but hands-on on a Linux VM.

So I decided to build a simple REST API using FastAPI on a Linux virtual machine and expose it to my host system.
During this journey, I faced real-world networking and Python environment issues — and solved them like a DevOps engineer.

This post documents my complete journey, steps, and learnings.

What is an API?
An API (Application Programming Interface) allows applications or users to communicate with a server using HTTP requests.

Example:

  • Client → sends request
  • Server → processes logic
  • API → returns JSON response

Architecture

 .

Simple Architecture

Step 1: Setting up FastAPI on Linux VM
Install Python and pip
sudo apt update
sudo apt install python3 python3-pip -y

Create Virtual Environment (Best Practice)
python3 -m venv api-env
source api-env/bin/activate

Install FastAPI and Uvicorn
pip install fastapi uvicorn

Step 2: Create a Simple API
Create main.py

`from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def home():
    return {"message": "Hello from Linux VM API"}

@app.get("/health")
def health():
    return {"status": "UP"}`
Enter fullscreen mode Exit fullscreen mode

Step 3: Run the API Server
uvicorn main:app --host 0.0.0.0 --port 8000

Step 4: Test API
From VM terminal
curl http://localhost:8000

Output:
{"message":"Hello from Linux VM API"}

From Host Machine (Desktop Browser)
http://192.168.204.128:8000/

Real Issues I Faced (and Solved)
1 SSL / HTTPS Error

_Problem:
Browser tried HTTPS but API was HTTP.

Solution:
Use HTTP explicitly._

2 0.0.0.0Access Issue

Problem:
Tried accessing API using 0.0.0.0.

Reality:
0.0.0.0 is a bind address, not a client address.

Solution:
Use VM IP or localhost.

3 Python PEP 668 Error
externally-managed-environment
Reason:
System Python is protected in modern Linux.

Solution:
Use virtual environment (venv).

Key Learnings

  • 0.0.0.0 is for server binding, not browser access.
  • Always use virtual environments in Linux.
  • Networking issues are common in VM-based setups.
  • FastAPI is powerful and developer-friendly.
  • Real DevOps work is about debugging, not just coding.

Top comments (0)