Self-Hosted AI Agent Gateway on Ubuntu 24.04: Connect LLM APIs Securely
This tutorial explains how to deploy a general-purpose, self-hosted AI agent gateway on Ubuntu 24.04 using Node.js, connect it to a third-party LLM API through an interactive configuration wizard, and access its web interface securely via SSH port forwarding. It covers environment planning, npm setup, common installation issues, API connectivity checks, and production hardening practices.
As large language models (LLMs) become more widely available, many developers want a private, customizable AI assistant service: a deployment they control, flexible model switching, and full control over the access entry point. This article uses an Ubuntu 24.04 LTS server as an example and walks through deploying a general-purpose agent gateway tool with the Node.js ecosystem—a self-hosted AI service that provides a web interface and can connect to multiple LLM APIs—then connecting it to a third-party LLM provider’s chat API, and finally using it securely from a local browser through an SSH tunnel. The process does not depend on a specific vendor and is generally applicable.
1. Environment and Planning Before Deployment
Before you start, complete the following preparation steps. This will prevent most later issues.
System and runtime environment
- An Ubuntu 24.04 server (cloud host or local virtual machine is fine); at least 2 CPU cores and 4GB of memory are recommended.
- A Node.js runtime, preferably version 20.x or a later LTS release. You can install it with a version manager such as nvm for easier upgrades later.
- The npm package manager is installed together with Node.js.
Configure the npm global directory
To avoid permission issues caused by using sudo to install global packages, set a user-level global directory first:
mkdir -p ~/.npm-globalnpm config set prefix '~/.npm-global'echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrcsource ~/.bashrc
Obtain LLM API credentials
Apply for an API Key from the target LLM provider’s console (usually a key string with a specific prefix), and confirm the OpenAI-compatible Base URL and model name. You will use this information during configuration. Keep the key secure and never commit it to a code repository.
2. Install the Agent Gateway
Most similar tools are distributed globally through npm, so installation is straightforward:
npm install -g <agent-gateway-package>@latest
After installation, verify the version:
<agent-gateway-package> --version# A semantic version number should be printed
Common installation issues and fixes
- ENOTEMPTY error: This is a classic issue during npm global installation, usually caused by an interrupted previous installation or leftover files. Clean the residual directory and cache, then reinstall:
rm -rf ~/.npm-global/lib/node_modules/<agent-gateway-package>npm cache clean --forcenpm install -g <agent-gateway-package>@latest
- Deprecated warnings: Dependency deprecation notices during installation are normal. They usually come from indirect dependencies that have not yet been upgraded and do not affect core functionality, so you can ignore them.
- Slow downloads or timeouts: Switch the npm registry mirror, or check the server’s outbound network policy and security group rules.
3. Connect to a Third-Party LLM API
After installation, similar tools generally provide an interactive configuration wizard. Run:
<agent-gateway-package> config
The wizard usually includes the following key steps:
- Add custom provider: Choose to add a provider manually rather than selecting an option from the preset list.
- Enter a provider name: Use a recognizable custom name, such as
my-llm. - Enter the API base URL: Fill in the endpoint provided by the service provider. Make sure the path level matches the documentation; most OpenAI-compatible services end with
/v1or a similar path. - Enter the API Key: Paste the key you applied for earlier.
- Select or enter the model identifier: Enter the name of the corresponding lightweight/fast model from the provider’s documentation for daily chat, balancing response speed and cost.
After configuration, send a test message from the command line first to confirm connectivity before debugging in the web interface. If an authentication error is returned, check whether the key is complete, whether the account has available quota, and whether the endpoint URL is spelled correctly. If a timeout occurs, check the server’s outbound network and DNS resolution.
4. Access the Web Interface Securely via SSH Port Forwarding
By default, the agent gateway’s web interface usually listens only on a port on the local loopback address (127.0.0.1), such as 3000 or 8080. This is a good security design. Opening that port directly in a security group is risky—without authentication, anyone could operate your AI service and keys.
The recommended approach is to use SSH local port forwarding. Run the following on your local terminal:
ssh -N -L 3000:127.0.0.1:3000 user@<server-ip>
Parameter details: -L creates a local forward, mapping local port 3000 to the corresponding port on the server’s loopback address; -N means no remote command is executed and only the tunnel is maintained. Keep the terminal window open, then visit http://127.0.0.1:3000 in your local browser to open the web interface. All traffic goes through the encrypted SSH channel, so no additional ports need to be opened in the firewall.
For long-term use, you can combine it with autossh for automatic reconnection, or write the forwarding command into a systemd service.
5. Hardening Recommendations for Production Use
Getting the tutorial environment working is only the first step. For stable long-term use, also consider:
- Process supervision: Use systemd or a process manager such as pm2 to manage the gateway process, enabling startup on boot and automatic restart after failures.
- Key management: Inject API Keys through environment variables or a configuration file with restricted permissions (
chmod 600), and avoid plaintext keys appearing in shell history. - Minimal exposure: Keep the pattern of listening only on the loopback address plus an SSH tunnel. If external access is truly required, add authentication and HTTPS at the reverse proxy layer.
- Logs and usage monitoring: Regularly check call logs and token consumption on the provider side, set quota limits for API Keys, and prevent losses caused by abnormal calls.
- Timely upgrades: Follow new versions of the tool, back up the configuration directory before upgrading, and verify after upgrading before switching traffic.
Conclusion
Through the path of “npm global installation → interactive configuration to connect an LLM API → secure access via SSH tunnel,” we built a fully self-controlled AI assistant service on Ubuntu 24.04 at relatively low cost. This methodology also applies to most self-hosted AI applications: the core idea is always local listening, encrypted tunnel access, and minimal key exposure. On this basis, you can further explore advanced capabilities such as knowledge base integration and multi-channel messaging integration, so that AI can truly serve your own business scenarios.
