Deploy Your First AI Agent on a $5 VPS: The Definitive Production Walkthrough
The $5 Production Reality Check
The chasm between a Jupyter notebook and a production AI agent is vast. In your local environment, you can restart cells. In production, your agent is a daemon. It must survive server reboots, handle errors gracefully, and be accessible securely over the internet. This guide demystifies that process.
We're using a minimal, cloud-agnostic approach. Our infrastructure bill? Approximately $5 per month. Our stack: a basic Ubuntu 22.04 VPS (1 vCPU, 1GB RAM), systemd for process management, nginx as a reverse proxy, and Let's Encrypt for HTTPS. The goal is a functional, self-hosted AI backend that is both cost-effective and professionally architected.
Phase 1: Foundational VPS Setup & Agent Code
First, provision your VPS. Any provider offering a basic Ubuntu 22.04 instance will work (Hetzner, DigitalOcean, Vultr). SSH in and perform initial system hardening.
sudo apt update && sudo apt upgrade -y
sudo apt install python3-pip python3-venv -y
# Create a non-root user for our agent
sudo adduser agentuser
sudo usermod -aG sudo agentuser
su - agentuser
As `agentuser`, we'll set up the project. Let's assume our AI agent is a simple FastAPI application that processes text with a small local model.
# Inside agentuser's home directory
mkdir -p ~/torment-agent
cd ~/torment-agent
python3 -m venv venv
source venv/bin/activate
pip install fastapi uvicorn[standard] torch --extra-index-url https://download.pytorch.org/whl/cpu
Create `main.py`. This is our barebones production agent, complete with health checks and a single inference endpoint.
from fastapi import FastAPI
import torch
app = FastAPI()
# In production, you'd load a pre-trained model from a file
model = torch.nn.Linear(10, 2) # Placeholder model
@app.get("/health")
def health_check():
return {"status": "healthy", "model_loaded": True}
@app.post("/predict")
def predict(data: list[float]):
input_tensor = torch.tensor([data[:10]]) # Truncate to model dim
with torch.no_grad():
output = model(input_tensor)
return {"prediction": output.tolist()[0]}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
Phase 2: Systemd - Your Guardian Daemon
Systemd is the Linux init system. It will ensure our AI agent starts on boot and restarts if it crashes. We create a service file. This is a critical step for any production AI agent deployment.
sudo nano /etc/systemd/system/torment-agent.service
Paste the following configuration. This file tells systemd to run our agent as the `agentuser`, activate its virtual environment, and restart on failure.
[Unit]
Description=TormentNexus AI Agent Service
After=network.target
[Service]
User=agentuser
Group=agentuser
WorkingDirectory=/home/agentuser/torment-agent
ExecStart=/home/agentuser/torment-agent/venv/bin/python main.py
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
Enable and start the service. Your AI agent is now a managed system process.
sudo systemctl daemon-reload
sudo systemctl enable torment-agent
sudo systemctl start torment-agent
# Check its status and logs
sudo systemctl status torment-agent
sudo journalctl -u torment-agent -f
Phase 3: Nginx Reverse Proxy & SSL
Running FastAPI on port 8000 is not production-ready. We need nginx to serve as a reverse proxy, handling incoming traffic on standard HTTP/HTTPS ports (80/443) and forwarding it to our agent.
sudo apt install nginx -y
sudo nano /etc/nginx/sites-available/agent
Configure nginx. We start with HTTP to get our SSL certificate, then we'll upgrade it.
server {
listen 80;
server_name your-domain.com; # Or your VPS IP for testing
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Enable the site and test nginx configuration.
sudo ln -s /etc/nginx/sites-available/agent /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
Now, deploy a free, automated SSL certificate with Let's Encrypt and Certbot. This secures your AI agent API.
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your-domain.com --non-interactive --agree-tos -m your@email.com
Certbot will automatically modify your nginx config to handle HTTPS and set up a redirect from HTTP to HTTPS. Your endpoint is now `https://your-domain.com/predict`.
Testing, Monitoring & Production Hardening
Verify the full deployment. Use `curl` to test the health and prediction endpoints.
# Health Check
curl https://your-domain.com/health
# Prediction Request
curl -X POST "https://your-domain.com/predict" \
-H "Content-Type: application/json" \
-d '{"data": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]}'
For monitoring, watch the systemd journal and set up basic log rotation. For scaling, you could place multiple instances of the agent behind the same nginx upstream block. The key is starting with a reproducible, documented deployment path like this one.
The TormentNexus Advantage: Focus on Your Agent, Not the Infra
This guide provides a rock-solid foundation for a self-hosted AI agent. However, managing VPS updates, security patches, and scaling can become your full-time job. TormentNexus abstracts this layer entirely. Our platform lets you deploy AI agent instances with a single command, complete with built-in monitoring, automatic scaling, and managed TLS—allowing you to focus exclusively on model improvement and application logic. Whether you're launching your first production AI or scaling a fleet, TormentNexus provides the robust, dedicated infrastructure your agent deserves.
Ready to skip the infrastructure headaches? Explore TormentNexus and deploy your production AI agent in minutes, not days.