Complete Practice: Deploying and Securely Accessing an LLM Chat Service on a Linux Server

    This guide details how to deploy a self-hosted AI chat service on a Linux server. It covers setting up the Ubuntu environment, installing Node.js via nvm, deploying the OpenClaw chat framework with a custom LLM API provider, and securely accessing the service using SSH port forwarding.

    1. Background and Overall Approach

    With the widespread adoption of LLM technology, an increasing number of developers want to build their own AI chat services on private servers rather than relying entirely on third-party web interfaces. The benefits of self-hosting are clear: data control, customization, the ability to swap underlying models as needed, and the capability to integrate with your own business systems via API gateways.

    The solution described in this article consists of four main steps:

    1. Environment Preparation: An Ubuntu 24.04 server with SSH access.
    2. Runtime Installation: Installing Node.js using a version manager to ensure the runtime version meets requirements.
    3. Service Deployment and Model Configuration: Installing an open-source chat service framework and integrating it with a cloud provider’s LLM API.
    4. Secure Access: Restricting service port access to localhost via SSH port forwarding to avoid direct public exposure.

    2. Prerequisites and Environment Preparation

    Before you begin, ensure the following conditions are met:

    • Server: An Ubuntu 24.04 server with public internet access (for downloading dependencies and calling model APIs). This guide uses the root user for demonstration, but from a security perspective, it is recommended to create a standard user with sudo privileges for daily operations in production environments to mitigate system risks from accidental operations.
    • Model API Key: Register an account on your chosen LLM platform and create an API Key. Most platforms offer free tiers for individual developers, which is sufficient for daily learning and light usage. Keep the Key secure and never commit it to code repositories or public documentation.
    • Local Computer: Windows, macOS, or Linux are all acceptable as long as you can connect to the server via SSH.
    • Node.js Environment: The target service runs on Node.js. Note that the default Node.js version in Ubuntu 24.04 repositories might be outdated, while newer service frameworks typically require recent runtime versions. Therefore, using nvm (Node Version Manager) to install and maintain versions is highly recommended.

    3. Step 1: Install the Node.js Runtime

    Using nvm to install Node.js offers several benefits: you can freely switch between versions, manage global packages without sudo, and easily upgrade or rollback. Execute the following commands:

    1. # Install nvm
    2. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
    3. # Apply environment variables
    4. source ~/.bashrc
    5. # Install Node.js 22 LTS and set it as the default version
    6. nvm install 22
    7. nvm alias default 22
    8. # Verify installation
    9. node --version

    If the output shows version v22.x.x, the runtime is ready. Choosing the LTS (Long Term Support) version is industry standard—it offers a longer maintenance cycle and more stable APIs, making it suitable for production deployments.

    Common Issue: If the server experiences slow connections to GitHub, you can configure mirror sources for nvm and npm, or download the installation script in advance for offline execution. Neither affects the final outcome.

    4. Step 2: Install the Chat Service Framework

    Installing the framework is straightforward using npm global installation:

    1. npm install -g openclaw@latest

    Verify the installation after completion:

    1. openclaw --version

    It should output the corresponding version number, indicating a successful installation.

    Potential Issues and Solutions:

    1. ENOTEMPTY Error: This error indicates that the target installation directory is not empty, usually caused by residual files from a previous failed installation. Clean it up and reinstall:
    1. rm -rf /root/.npm-global/lib/node_modules/openclaw
    2. npm cache clean --force
    3. npm install -g openclaw@latest
    1. Deprecation Warnings: Warnings about deprecated dependency packages during installation are normal and generally do not affect functionality. They can be ignored.

    2. Permission Issues: If you are using the system’s default Node.js and have not configured a global npm directory, you might encounter EACCES permission errors. It is recommended to use nvm to manage Node.js, or configure npm’s global prefix directory under your user directory to avoid the security risks of installing global packages with sudo.

    5. Step 3: Configure the LLM Service

    The framework provides an interactive configuration wizard. Execute:

    1. openclaw config

    Follow the prompts step by step:

    1. Select Add custom provider (manually add a custom model provider).
    2. Enter the provider name, for example, zhipu (the name can be customized and is only used for local identification).
    3. Enter the API address, which is the endpoint compatible with the model service, for example, https://open.bigmodel.cn/api/paas/v4.
    4. Enter the API Key you created on the LLM platform.

    Here are two practical tips:

    • Regarding API Protocols: Most mainstream LLM platforms’ APIs are compatible with standard chat completion interface formats, so many open-source service frameworks support connecting via a “custom provider.” As long as you enter the correct endpoint address and Key, the integration is complete. If you want to switch model providers later, you only need to modify the provider configuration; the upper-layer application requires no changes. This reflects the flexibility of self-hosting.
    • Regarding Key Security Management: The configuration file will store the API Key. It is recommended to tighten the read permissions of this file to the current user (chmod 600) and rotate the Key regularly. If you have other web-facing services deployed on the server, ensure the configuration directory is not accidentally exposed by the web service.

    6. Step 4: Securely Access the Web Interface via SSH Port Forwarding

    Once started, the web interface listens on a specific local port on the server by default. A common mistake is to directly allow this port through the firewall for public access—this introduces risks such as port scanning, unauthorized access, and Key leakage.

    A more robust solution is to use SSH Port Forwarding: the service only listens on 127.0.0.1 without public exposure, and your local computer accesses it through an SSH tunnel. Execute the following in your local terminal:

    1. ssh -L 8080:127.0.0.1:8080 root@YOUR_SERVER_IP

    This command means: forward traffic from your local port 8080 through an encrypted SSH tunnel to 127.0.0.1:8080 on the server. Then, open http://localhost:8080 in your local browser to use the remote chat interface just like a local service.

    Advantages of this approach include:

    • Encrypted Transit: All traffic goes through the encrypted SSH channel, preventing man-in-the-middle eavesdropping.
    • Zero Exposure: The service port is not open to the public network, drastically reducing the attack surface.
    • No Extra Components: Secure access is achieved without needing to install a VPN or reverse proxy.

    Windows users utilizing graphical SSH tools can configure the same rules in the “Tunnel/Port Forwarding” settings. For long-term usage scenarios, you can pair this with autossh or SSH keepalive parameters to maintain tunnel stability.

    7. Summary and Future Optimization Directions

    This article completed a full deployment workflow for a self-hosted AI chat service: installing the Node.js runtime via nvm on Ubuntu 24.04, globally installing the open-source chat framework OpenClaw, integrating it with an LLM API as a custom provider, and finally achieving secure access via SSH port forwarding.

    Building on this foundation, further optimizations can be made:

    1. Process Daemon: Use systemd or process management tools to enable auto-start on boot and automatic restarts upon failure.
    2. Multi-user and Authentication: If the service is shared by a team, add access authentication at the web layer and consider assigning independent model Keys to different users.
    3. Logging and Monitoring: Integrate logging services and monitoring alerts to keep track of usage volume and anomalies in real time.
    4. Cost Control: Monitor the usage statistics on the model platform and set call limits if necessary to prevent accidental quota depletion.

    Overall, this solution offers low deployment costs, strong security, and high scalability, making it an excellent foundational infrastructure for personal or small-team dedicated AI assistants.