Hosting an AI agent on a virtual private server (VPS) keeps its process available even when your computer is off. You’ll install the agent and its dependencies on a Linux server, configure credentials and persistent storage, then run it as a supervised service that restarts after failures or reboots. Before starting, make sure your agent can run unattended and that its provider permits your intended use.
What You’ll Need
- A VPS running a supported Linux distribution, such as a current Ubuntu LTS release, with administrator access. Choose memory and storage based on the agent’s documented requirements; an API-based agent usually needs less than one running a local model.
- An SSH client and the server’s IP address and login credentials or SSH key.
- The agent’s source code or installation instructions, plus its runtime requirements, such as Python or Node.js.
- Any required API keys, stored securely. Never put a live key in a public repository or a command that may be saved in shell history.
- A domain name and DNS access only if you need a public web endpoint. For an agent that makes scheduled API calls without accepting inbound requests, a domain may not be needed.
Step-by-Step
- Connect to the VPS. From your computer, open a terminal and connect using the provider’s instructions, for example
ssh username@SERVER_IP. If you use a private key, specify it with your SSH client’s key option. Confirm the host key when you have verified it against your provider’s information. Once connected, update the operating system using its package manager; on Ubuntu or Debian, runsudo apt updatefollowed bysudo apt upgrade. Reboot if updates require it, then reconnect. - Create a dedicated account. Avoid running an internet-facing agent as root. Create a restricted user for the service, for example with
sudo adduser agent, and switch to it withsudo -iu agent. Install the runtime and system packages listed in the agent’s official setup instructions. Use a virtual environment for Python projects or a project-local dependency installation for Node.js to reduce conflicts with system software. - Install the agent in a controlled directory. Put its code in a directory owned by the service account, such as
/opt/agentif permissions are arranged for that account, or a directory under its home folder. Obtain code only from a source you trust. Follow the project’s documented installation steps, pin or lock dependencies where supported, and run its tests or basic command-line check before setting up automatic startup. - Configure credentials and behavior. Set required API keys, model names, prompts, tool permissions, and any other settings according to the agent’s documentation. Prefer a protected environment file or a secrets manager over hard-coding keys. For a simple environment file, restrict access with
chmod 600and ensure it is excluded from source control. Do not paste secrets into public logs, issue reports, or chat. Set sensible request limits, timeouts, and spending limits with the relevant provider where available. - Test it in the foreground. Run the documented start command as the service account. Check that it can reach required services, handles a normal task, and exits cleanly when stopped. Watch the output for missing configuration, permission errors, or repeated requests. Do not expose a development dashboard or unauthenticated control endpoint to the internet. If the agent needs inbound traffic, identify its listening port and authentication requirements before proceeding.
- Run it under systemd. On a systemd-based Linux VPS, create a unit file with
sudo nano /etc/systemd/system/agent.service. Use the correct paths and command from your installation; a typical unit is:[Unit]
Description=AI agent
After=network-online.target
Wants=network-online.target[Service]
User=agent
WorkingDirectory=/home/agent/app
EnvironmentFile=/home/agent/.env
ExecStart=/home/agent/app/start-agent
Restart=on-failure
RestartSec=10[Install]
WantedBy=multi-user.target
ReplaceWorkingDirectory,EnvironmentFile, andExecStartwith real paths. The start command must run in the foreground, not launch a detached background process. Protect the environment file so only the service account and administrators can read it. Then runsudo systemctl daemon-reload,sudo systemctl enable --now agent, andsudo systemctl status agent. - Check logs and restart behavior. Read recent output with
sudo journalctl -u agent -n 100 --no-pager; follow new messages withsudo journalctl -u agent -f. Restart it after configuration changes usingsudo systemctl restart agent. Verify that the agent starts after a VPS reboot by scheduling a maintenance reboot when safe, then checkingsystemctl statusand the logs again. - Secure any public endpoint. If users or external services must reach the agent, expose only the necessary port. Configure the VPS firewall to allow that port and SSH from trusted addresses where practical. Put web traffic behind a reverse proxy with HTTPS, and require authentication or signed requests. Keep the agent’s admin interface private. If it only initiates outbound connections, do not open an inbound port unnecessarily.
- Set up maintenance and monitoring. Check disk space, memory, service status, and logs periodically. Configure alerts for downtime if the agent is important, and decide how to handle queued work after outages. Back up configuration and necessary data securely, but keep secrets encrypted or separately protected. Apply operating system and agent updates deliberately, then retest the service; avoid unattended updates that could change a critical dependency without notice.
Common Mistakes
- Using the wrong startup command: systemd expects a foreground process. A command that immediately detaches can make the service appear stopped.
- Assuming a process manager guarantees uptime: automatic restart cannot fix invalid credentials, provider outages, exhausted quotas, or a broken application.
- Exposing ports by default: an open port is not a security plan. Keep private interfaces off the public network and use authentication and HTTPS for endpoints.
- Leaving secrets in code or logs: rotate any key that has been exposed, remove it from active configuration, and check repository history and logs where feasible.
- Ignoring costs and resource limits: a VPS can run out of memory or disk, while API calls may incur separate charges. Monitor both server capacity and provider usage.
Tips
- Give the service account only the file and network access it needs; avoid granting broad administrator privileges.
- Use a lockfile or documented dependency versions so a rebuild is repeatable.
- Make the agent handle shutdown signals and temporary network failures gracefully. Retry with a delay rather than rapidly repeating failed requests.
- Keep a short recovery note with the service name, install path, start command, log command, and backup location—but not secret values.
Troubleshooting
The service fails immediately: Run sudo systemctl status agent and inspect sudo journalctl -u agent -n 100 --no-pager. Check that ExecStart points to an executable command, the working directory exists, dependencies are installed, and the service user can read its files. Test the same command manually as that user.
It works manually but not under systemd: Services do not necessarily inherit your interactive shell’s PATH, environment, or current directory. Use absolute paths, set WorkingDirectory, and check the environment file’s permissions and syntax. Reload systemd after editing the unit, then restart the service.
The agent is running but cannot reach an API: Verify DNS and outbound connectivity, the server clock, credentials, quotas, and provider status. Check whether a firewall or proxy blocks the required outbound connection. Avoid printing full authorization headers while debugging.
It becomes slow or stops after running for a while: Check memory, CPU, disk space, and logs around the failure. Reduce concurrency or workload, increase VPS resources if measurements justify it, and investigate unbounded logs or queues. A restart policy helps recover from crashes but does not resolve resource leaks.
Users cannot reach the endpoint: Confirm the process is listening on the expected interface and port, then check the firewall, DNS, reverse proxy, and HTTPS certificate. Keep the service private until authentication and access controls are working.
Frequently Asked Questions
Does a VPS keep an AI agent running if my computer is off?
Yes. The VPS runs independently, provided it is operating and the service is enabled. External APIs and network access must also be available.
Do I need a GPU?
Usually not if the agent sends requests to a hosted AI API. A GPU may be useful for local model inference, but choose hardware based on that model’s actual requirements.
Will systemd restart the agent after a server reboot?
Yes, when the unit is enabled with systemctl enable. A restart policy can also restart it after certain failures, but confirm the behavior with a reboot test.
Can I host an agent without a domain name?
Yes, if it only runs scheduled tasks or makes outbound requests. A domain is useful for a public endpoint, but it does not replace HTTPS or authentication.
Is the agent guaranteed to be available 24/7?
No. A VPS and restart policy improve continuity, but maintenance, outages, application errors, and third-party service failures can still interrupt it. Monitoring and recovery plans help you respond.
How should I update the agent?
Back up needed data, review the project’s update instructions, install the change in a controlled way, and test it before relying on it. Restart the service and check logs and a representative task afterward.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Zoom Desktop Client: Features, Setup, and Fixes
- Python Integer Identity: Why It Varies by Run
- Can Technology Solve Poverty? Facts & Limits
- Wireless Controller Charging: Can You Play While Plugged In?
- Secure a Node.js API: Layered Checklist
- Cannot Detect Device? Fixes for USB & Bluetooth