DEV Community

HyperNexus
HyperNexus

Posted on • Originally published at tormentnexus.site

Deploy a Production AI Agent on a $5 VPS: The Complete Systemd, Nginx, & HTTPS Walkthrough

Deploy a Production AI Agent on a $5 VPS: The Complete Systemd, Nginx, & HTTPS Walkthrough

Learn to deploy an AI agent to production on a minimal $5 VPS. This step-by-step guide covers server setup, process management with systemd, reverse proxying with nginx, and securing with free Let's Encrypt SSL certificates.

The Production Reality: Why a $5 VPS is All You Need for Your AI Agent

Moving an AI agent from your local machine to a live, accessible endpoint is the critical bridge between a prototype and a product. The perceived barrier to entry for "production AI" is often high, involving complex cloud services and significant costs. However, the modern developer toolkit makes this process straightforward and affordable. A basic VPS from providers like DigitalOcean, Linode, or Vultr costs as little as $5/month and provides more than enough resources to run a capable AI agent, handle initial traffic, and serve as a robust deployment target.

This guide walks you through the entire process, assuming you have a trained model or API-based agent (e.g., one using the OpenAI API). We'll focus on the operational "plumbing": creating a secure, reliable, and publicly accessible service on a fresh Ubuntu 22.04 server. You will learn to deploy your AI agent as a background service, manage it with systemd, expose it to the world via nginx, and secure it with free TLS certificates from Let's Encrypt. By the end, you'll have a production-ready, self-hosted AI agent running at a fraction of the cost of many managed platforms.

Prerequisites & Initial Server Setup

Before deploying, ensure you have a registered domain name (e.g., `myaiagent.com`) and an A record pointing to your new VPS's public IP address. After provisioning your $5 VPS (choose Ubuntu 22.04), SSH in as root.

First, perform essential system updates and create a dedicated, non-root user for your application. This follows security best practices.

sudo apt update && sudo apt upgrade -y
adduser aiagent
usermod -aG sudo aiagent
su - aiagent

Now, as the `aiagent` user, install the core dependencies. Assuming your agent is a Python application, we'll set up a robust environment. Install Python, pip, and create a virtual environment to isolate dependencies.

sudo apt install -y python3-pip python3.10-venv git
mkdir -p ~/projects && cd ~/projects
git clone https://github.com/your-username/your-agent-repo.git
cd your-agent-repo
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
# Install Gunicorn as a production WSGI/ASGI server
pip install gunicorn

We'll use Gunicorn to run your application (e.g., a FastAPI or Flask app) as a production-grade process manager. Verify your application works locally first.

Process Management with Systemd: The Heart of Your Deployed Agent

Systemd is the init system for most Linux distributions. Its core benefit is managing your AI agent as a persistent service that starts on boot, restarts on failure, and can be controlled with simple commands. We'll create a service unit file.

Create the file `/etc/systemd/system/aiagent.service` using sudo and your preferred editor:

sudo nano /etc/systemd/system/aiagent.service

Paste the following configuration, adjusting paths and the Gunicorn command to match your project's ASGI/WSGI interface:

[Unit]
Description=Your AI Agent Gunicorn Service
After=network.target

[Service]
User=aiagent
Group=aiagent
WorkingDirectory=/home/aiagent/projects/your-agent-repo
Environment="PATH=/home/aiagent/projects/your-agent-repo/venv/bin"
ExecStart=/home/aiagent/projects/your-agent-repo/venv/bin/gunicorn \
          --workers 3 \
          --bind unix:aiagent.sock \
          --timeout 120 \
          app:app

Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

Key points: We run as our `aiagent` user, set the correct working directory, and use a virtual environment path. Gunicorn is instructed to bind to a Unix socket (`aiagent.sock`) rather than a TCP port. This is more secure and efficient for local communication with nginx. The `app:app` part refers to your Python module and Flask/FastAPI instance name.

Now, enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable aiagent.service
sudo systemctl start aiagent.service
sudo systemctl status aiagent.service # Check for active status

You can now manage your agent with `sudo systemctl stop|restart|status aiagent`. Logs are accessible via `journalctl -u aiagent -f`.

Nginx as a Reverse Proxy & Free HTTPS with Let's Encrypt

Nginx will serve as the public face of your application, handling incoming HTTP/HTTPS traffic and forwarding it to your agent's Unix socket. This provides buffering, static file serving, and terminates SSL.

Install nginx and Certbot, the Let's Encrypt client:

sudo apt install -y nginx certbot python3-certbot-nginx

Create an nginx configuration for your site. Create the file `/etc/nginx/sites-available/aiagent`:

sudo nano /etc/nginx/sites-available/aiagent

Add this configuration:

server {
    listen 80;
    listen [::]:80;
    server_name myaiagent.com www.myaiagent.com;

    location / {
        proxy_pass http://unix:/home/aiagent/projects/your-agent-repo/aiagent.sock;
        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;
        proxy_redirect off;
    }
}

Enable this site and remove the default. Test the configuration and reload nginx:

sudo ln -s /etc/nginx/sites-available/aiagent /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx

Now, use Certbot to fetch and automatically configure your free SSL certificate. This will also modify your nginx config for HTTPS.

sudo certbot --nginx -d myaiagent.com -d www.myaiagent.com --non-interactive --agree-tos -m your-email@domain.com

Certbot handles obtaining the certificate, updating nginx with SSL directives, and setting up a systemd timer for automatic renewal. Visit `https://myaiagent.com` to see your live, secure AI agent.

Monitoring, Maintenance, and Scaling Considerations

Your AI agent is now deployed. Monitor its health by checking service status (`systemctl status aiagent`) and nginx logs (`/var/log/nginx/error.log`, `access.log`). Use tools like `htop` to watch resource usage on your $5 VPS.

For a production deployment, implement application-level logging (e.g., to a file) and consider adding basic uptime monitoring. When traffic grows, your first bottleneck will likely be the VPS's memory/CPU. The graceful solution is to scale horizontally: duplicate this entire setup on a second VPS and load-balance between them using nginx's upstream directive or a managed load balancer.

Remember to regularly update your system packages (`sudo apt update && sudo apt upgrade`) and your application's dependencies. The self-hosted nature of this setup gives you full control and deep insight into every layer of your production AI deployment.

Ready to deploy your own AI agent? TormentNexus provides the battle-tested tools and pre-configured templates to simplify building, deploying, and scaling production AI agents. Start your journey at tormentnexus.site.


Originally published at tormentnexus.site

Top comments (0)