Deploying an AI Application with External LLM Access on Ubuntu 24.04

    This article walks through the deployment of a Node.js-based intelligent application on a remote Ubuntu 24.04 LTS server. The application calls an external large language model through an HTTP-compatible API and is accessed securely from a local browser via an SSH tunnel. It covers environment setup, Node.js installation, app configuration, model API testing, systemd service management, SSH port forwarding, and security hardening.

    1. Solution Overview

    This guide demonstrates a generic deployment pattern: a Node.js application running on a remote Ubuntu 24.04 LTS server calls an external large language model service through an HTTP-compatible API, and you access the web interface from your local browser.

    The overall data flow:

    1. Local browser (127.0.0.1:8080)
    2. │
    3. │ SSH tunnel (encrypted)
    4. ▼
    5. Server web app (127.0.0.1:3000)
    6. │
    7. │ HTTPS API request
    8. ▼
    9. External LLM service

    The web service listens only on the loopback address 127.0.0.1 and does not accept public connections. You reach it through an SSH port forward, minimizing the attack surface and simplifying maintenance on a personal server.

    Assumptions made in this article (replace with your own values):

    • OS: Ubuntu 24.04 LTS, with a login user named deploy
    • Application package name: example uses my-ai-app, replace it with the package name from the official docs
    • Application port: example uses 3000
    • Model service: OpenAI-compatible API; the exact request format, authentication method, and model names follow the provider’s official documentation

    2. Prerequisites

    Ensure the following before starting:

    1. A server running Ubuntu 24.04 LTS
    2. A non-root user with sudo privileges
    3. SSH access from your local machine to the server
    4. An API key created in the model service console
    5. The server can reach the software repositories and the model service’s HTTPS endpoints
    6. At least 5 GB of free disk space and 2 GB of RAM recommended

    Check the OS version, current user, and working directory:

    1. cat /etc/os-release
    2. whoami
    3. pwd

    The output shows VERSION="24.04.x LTS" for the correct OS version. If your system is not Ubuntu, adjust the package manager commands accordingly.

    Check disk and memory:

    1. df -h /
    2. free -h

    If the server only has a root user, create a regular user first. Do not run the application as root:

    1. sudo adduser deploy
    2. sudo usermod -aG sudo deploy

    Log in as deploy for the rest of this article.

    3. Install the Node.js Runtime

    Most Node.js applications need to compile native modules, so install basic build tools first:

    1. sudo apt update
    2. sudo apt install -y build-essential python3 make g++ curl

    Recommended: install Node.js via nvm in your home directory. This avoids sudo and makes version switching easy. Replace the nvm script version with the latest tag from the official nvm repository:

    1. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
    2. source ~/.bashrc

    Install Node.js 22 LTS and set it as the default:

    1. nvm install 22
    2. nvm alias default 22
    3. nvm use default

    Verify the versions:

    1. node -v
    2. npm -v

    node -v should output v22.x.x. If the application requires a different version, follow the application’s documentation.

    For production environments, you can also install Node.js from the system repository or NodeSource. This approach is managed by the system for upgrades, but the version may be older. Always check with node -v that the version meets your application’s requirements.

    4. Install the AI Application

    To avoid requiring root for global npm packages, set the npm global directory to your home folder:

    1. npm config set prefix "$HOME/.npm-global"
    2. echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.bashrc
    3. source ~/.bashrc

    Install the application globally. Replace my-ai-app and the version with what is listed in the official documentation:

    1. npm install -g my-ai-app@1.0.0

    Verify:

    1. my-ai-app --version
    2. my-ai-app --help

    If you get command not found, check the following:

    1. echo $PATH
    2. npm config get prefix
    3. ls "$HOME/.npm-global/bin"

    The PATH should include $HOME/.npm-global/bin. If not, re-run source ~/.bashrc or log out and back in.

    Dealing with Upgrade or Installation Residue

    If you see errors about non-empty directories or file conflicts during an upgrade, do not keep rerunning the install command. First confirm the installation directory:

    1. ls -la "$(npm root -g)"

    Back up the current directory before cleaning:

    1. mkdir -p ~/backup/$(date +%F)
    2. cp -a "$(npm root -g)/my-ai-app" ~/backup/$(date +%F)/

    After verifying the backup, remove the residue and clear the cache. Double-check the path before deleting to avoid removing other applications:

    1. rm -rf "$(npm root -g)/my-ai-app"
    2. npm cache clean --force
    3. npm install -g my-ai-app@1.0.0

    5. Configure the External Model Service

    Applications typically support environment variables or a config file. This article uses a .env file that is readable only by the current user. Replace parameter names with those from the application’s documentation:

    1. mkdir -p ~/ai-app/config
    2. nano ~/ai-app/config/.env

    Write the following and replace the values with your own:

    1. AI_PROVIDER=custom
    2. AI_BASE_URL=https://model-service-domain/v1
    3. AI_API_KEY=your-access-key
    4. AI_MODEL=target-model-name
    5. AI_TIMEOUT_MS=60000
    6. AI_MAX_TOKENS=2048
    7. AI_TEMPERATURE=0.7

    Notes on these parameters:

    • AI_BASE_URL usually ends at the API version path (e.g., /v1). The application will append the concrete endpoint such as /chat/completions. Including the full path can cause requests to go to the wrong URL.
    • Most compatible APIs use Authorization: Bearer <key> for authentication. The application adds this header automatically; you only need to provide the key.
    • AI_TIMEOUT_MS controls the timeout for a single request in milliseconds. Increase it if the network is slow.
    • AI_MAX_TOKENS limits the output length per request; AI_TEMPERATURE controls the randomness of the output.

    The config file contains a secret, so restrict its permissions:

    1. chmod 600 ~/ai-app/config/.env
    2. ls -l ~/ai-app/config/.env

    The output should start with -rw-------, meaning only the current user can read and write. Do not put this file in a Git repository or any public directory.

    After configuration, test the model endpoint with a minimal request. The following command only prints the HTTP status code and does not display the key:

    1. set -a; source ~/ai-app/config/.env; set +a
    2. curl -sS -o /dev/null -w "%{http_code}\n" \
    3. "$AI_BASE_URL/models" \
    4. -H "Authorization: Bearer $AI_API_KEY"

    A 200 response means connectivity and key are valid. 401 indicates an incorrect key; 404 suggests the base URL is too long or too short. Some services do not support the /models endpoint; in that case, send a minimal chat completion request as described in the provider’s documentation.

    For debugging, replace -w with -v to see full request headers, but the output will include the key. Do not paste that output into chats, tickets, or screenshots.

    6. Start the Web Service in the Foreground

    First, start the app in the foreground to observe logs directly:

    1. cd ~/ai-app
    2. set -a; source config/.env; set +a
    3. HOST=127.0.0.1 PORT=3000 my-ai-app

    While starting, watch for the following:

    • Whether configuration is loaded successfully
    • The listening address and port
    • Whether the model service initializes
    • Any authentication failures or network errors
    • Any message about the port already being in use

    In another terminal, verify the listening address:

    1. ss -lntp | grep 3000

    The output should show 127.0.0.1:3000. If it shows 0.0.0.0:3000 or *:3000, the service is exposed on all interfaces. Check whether the HOST variable took effect, and confirm the listening configuration in the application documentation.

    After verification, press Ctrl+C to stop the foreground process and switch to systemd.

    Run as a Persistent Service with systemd

    Find the actual Node.js path; the unit file needs it:

    1. which node

    Assuming the output is /home/deploy/.nvm/versions/node/v22.x.x/bin/node, its parent directory must be in PATH. Create the service file:

    1. sudo nano /etc/systemd/system/my-ai-app.service

    Write the following, replacing paths and version numbers:

    1. [Unit]
    2. Description=My AI App
    3. After=network-online.target
    4. Wants=network-online.target
    5. [Service]
    6. Type=simple
    7. User=deploy
    8. WorkingDirectory=/home/deploy/ai-app
    9. EnvironmentFile=/home/deploy/ai-app/config/.env
    10. Environment=HOST=127.0.0.1
    11. Environment=PORT=3000
    12. Environment=PATH=/home/deploy/.nvm/versions/node/v22.x.x/bin:/usr/local/bin:/usr/bin:/bin
    13. ExecStart=/home/deploy/.npm-global/bin/my-ai-app
    14. Restart=on-failure
    15. RestartSec=5
    16. [Install]
    17. WantedBy=multi-user.target

    Reload and start the service:

    1. sudo systemctl daemon-reload
    2. sudo systemctl enable --now my-ai-app
    3. systemctl status my-ai-app --no-pager

    Check recent logs:

    1. journalctl -u my-ai-app -n 100 --no-pager

    systemd logs are managed by journald. To prevent logs from filling the disk, limit retention:

    1. sudo journalctl --vacuum-time=14d

    If the application writes logs to files, configure logrotate to rotate them by size or day and keep a limited number of archives.

    7. Access the Web Interface via SSH Tunneling

    On your local computer, open a terminal and run:

    1. ssh -N -L 8080:127.0.0.1:3000 deploy@server-address

    Parameter explanation:

    • -N: do not execute a remote command, only set up port forwarding
    • -L 8080:127.0.0.1:3000: forward local port 8080 to port 3000 on the server’s loopback interface
    • deploy@server-address: replace with your login user and server address

    After the tunnel is established, open a browser on your local machine:

    1. http://127.0.0.1:8080

    Keep the SSH terminal window open; closing it will terminate the tunnel.

    If local port 8080 is already in use, choose another one (e.g., -L 18080:127.0.0.1:3000) and visit http://127.0.0.1:18080. On Linux or macOS, check port usage:

    1. ss -lntp | grep 8080

    On Windows PowerShell:

    1. netstat -ano | findstr :8080

    If the tunnel is up but the page does not load, check on the server that the application is still running:

    1. systemctl is-active my-ai-app
    2. ss -lntp | grep 3000

    8. Security Hardening

    1. Protect the API Key

    Never put the key in scripts, code repositories, public documents, or chat logs. If you suspect the key has been leaked, immediately revoke it in the model service console, generate a new one, and update the .env file.