Skip to content

Deploying on Linux

How-to guideThe steps for one task.

A production install on one Ubuntu or Debian server: the Apinx binary under systemd, PostgreSQL for the store, nginx terminating TLS for two hostnames, and a firewall that leaves only 80 and 443 open. It is the supported shape: container images are not supported at the moment. Adapt names and addresses to yours.

What you end up with

                 ┌───────────────────────────── server ──────────────────────────────┐
console users ──►│ nginx :443  console.example.com ──► apinx console  127.0.0.1:9100 │
API callers   ──►│ nginx :443  api.example.com     ──► apinx gateway  127.0.0.1:8081 │
                 │                                                                   │
                 │                                     PostgreSQL     127.0.0.1:5432 │
                 └───────────────────────────────────────────────────────────────────┘

Two hostnames, both pointing at the server: one for the console and Admin API, one for the APIs you publish. Keeping them apart lets you restrict the console (an IP allow-list, for example) without touching API traffic, and keeps the two URL spaces from colliding. Apinx itself listens on the loopback interface only; nginx is the only way in.

You need:

  • a server with sudo: Ubuntu 24.04 LTS or Debian 12, 2 CPU cores and 4 GB of RAM at least;
  • the two DNS records, pointing at the server;
  • a TLS certificate covering both names (Let's Encrypt through certbot, a wildcard, or a Cloudflare origin certificate);
  • from MoznHub: the release binaries (apinx-linux-amd64, apinxctl-linux-amd64, with arm64 builds too) and their SHA256SUMS file, and your licence file (without it the installation runs a 30-day evaluation; see Licensing).

This is one instance, the default. For two or more servers behind a load balancer, follow this page on each and then High availability.

1. Binaries and service user

Copy the binaries and SHA256SUMS to the server, check them, then install:

sha256sum --check --ignore-missing SHA256SUMS        # every line must say OK
sudo useradd --system --home /var/lib/apinx --shell /usr/sbin/nologin apinx
sudo install -m 0755 apinx-linux-amd64 /usr/local/bin/apinx
sudo install -m 0755 apinxctl-linux-amd64 /usr/local/bin/apinxctl
sudo mkdir -p /etc/apinx

2. Process configuration

/etc/apinx/apinx.yaml. The console sits on a loopback port behind nginx, and public_url is the address users will open: it makes the session cookies Secure and gives identity-provider redirects the right host.

console:
  listen: "127.0.0.1:9100"
  public_url: "https://console.example.com"
proxy:
  config_file: /etc/apinx/gateway.yaml
demo:
  enabled: false
logging:
  level: info
  format: json
access_log:
  enabled: true
  sink: file
  path: /var/lib/apinx/logs/access.log
shutdown:
  drain_timeout: 30s

Pick ports that are free on your server (sudo ss -ltnp | grep -E ':9100|:8081' must print nothing); 9000 and 8080 are common choices but often taken.

Single node or cluster

The defaults are a single node with its own memory: one instance, deployments applied in-process, rate limits and quotas counted in memory, nothing else to run. That is what this page sets up, and it needs no setting at all.

Two settings turn it into a cluster, and either can be used alone:

Setting Default What it changes
cluster.mode single ha: several instances share one PostgreSQL database behind a load balancer; each applies what any of them deploys, and one at a time runs the background jobs. Needs storage.driver: postgres and a configured master key.
redis.address empty A Redis server that holds the rate-limit, quota and concurrency counters, so a limit means the same across every instance (and survives a restart). Empty: each instance counts alone.

They are process settings, not console settings, so they go where the process reads its configuration from. Any of the three places works; the highest wins:

  1. The YAML file above:

    cluster:
      mode: ha
      node: gw-a              # this instance's name on traces; empty uses the host name
    redis:
      address: redis.internal:6379
      # addresses: [sentinel-a:26379, sentinel-b:26379]   # Sentinel or Cluster instead of address
      # master_name: apinx                              # Sentinel
    
  2. Environment variables in /etc/apinx/apinx.env (step 6), the setting's path in upper case with underscores. The Redis password belongs here, never in the file:

    APINX_CLUSTER_MODE=ha
    APINX_CLUSTER_NODE=gw-a
    APINX_REDIS_ADDRESS=redis.internal:6379
    APINX_REDIS_PASSWORD=...
    
  3. Command-line flags named like the setting: --cluster.mode ha.

Both take effect at the next restart. The Overview's Components card shows the mode in effect and whether Redis is reachable, and Admin shows the effective configuration with secrets redacted; neither edits it, by design: the file and the environment are what you version and deploy. Every setting with its default is in the configuration reference; apinx config reference prints the same table. Running two or more servers is covered in High availability.

3. Gateway configuration

/etc/apinx/gateway.yaml seeds the first deployment. A listener on the loopback interface is enough; you create APIs in the console afterwards.

apiVersion: apinx/v1
kind: Gateway
listeners:
  - name: http
    address: "127.0.0.1:8081"
    h2c: true

Do not reuse configs/gateway.example.yaml here: its demo backend points at a port the console may be using.

4. The systemd unit

Copy deploy/systemd/apinx.service to /etc/systemd/system/apinx.service. It runs the binary as the apinx user on a read-only system, keeps state under /var/lib/apinx, reads secrets from /etc/apinx/apinx.env, and drains in-flight requests on stop. Do not start it yet.

sudo cp deploy/systemd/apinx.service /etc/systemd/system/
sudo cp deploy/systemd/apinx.logrotate /etc/logrotate.d/apinx    # rotation of the access log: daily or at 1 GB, 90 kept
sudo systemctl daemon-reload

The access log is what grows with traffic: a line is about 400 bytes, so a million calls a day is 400 MB, 40 MB once rotated and compressed. Above that, log every error and a fraction of the successes (access_log.sampling: 0.2 with always_log_errors: true, applied without a restart), and let logrotate run hourly so the 1 GB cap in the shipped file applies within a day:

sudo mkdir -p /etc/systemd/system/logrotate.timer.d
printf '[Timer]\nOnCalendar=\nOnCalendar=hourly\n' | sudo tee /etc/systemd/system/logrotate.timer.d/hourly.conf
sudo systemctl daemon-reload && sudo systemctl restart logrotate.timer

5. PostgreSQL

Apinx works with SQLite files and nothing else installed, but a server install wants PostgreSQL 14 or later for backups and tooling you already have:

sudo apt install -y postgresql
PGPASS=$(openssl rand -hex 24); echo "Database password: $PGPASS"     # into your password manager
sudo -u postgres psql -c "CREATE ROLE apinx LOGIN PASSWORD '$PGPASS';" -c "CREATE DATABASE apinx OWNER apinx;"

Ubuntu and Debian keep the database on 127.0.0.1 only; leave it that way. Tables are created on first start and migrated on upgrades.

6. Secrets: master key and database connection

sudo tee /etc/apinx/apinx.env >/dev/null <<EOF
APINX_SECURITY_MASTER_KEY=$(openssl rand -base64 32)
APINX_STORAGE_DRIVER=postgres
APINX_STORAGE_DSN=postgres://apinx:$PGPASS@127.0.0.1:5432/apinx?sslmode=disable
EOF
sudo chmod 0600 /etc/apinx/apinx.env

sslmode=disable is acceptable because the database is reachable only from the server itself; for a database on another host leave sslmode out (Apinx then adds require) or set verify-full with the server's CA.

The master key is part of every backup

It encrypts stored secrets. Copy apinx.env to where you keep production secrets now; a database restored without it has unreadable secrets, by design. Never paste it into a chat or a ticket.

7. Start

sudo systemctl enable --now apinx
sudo journalctl -u apinx -n 40 --no-pager

The log shows the migrations and then a claim token, printed on the line after "enter this claim token:". Keep it for step 11; a restart prints a fresh one until the instance is claimed.

sudo journalctl -u apinx --no-pager | grep -A 3 "enter this claim token" | tail -4
sudo ss -ltnp | grep -E ':8081|:9100'      # both ports listening

8. Certificate

sudo mkdir -p /etc/ssl/apinx
sudo mv fullchain.pem /etc/ssl/apinx/fullchain.pem     # certificate plus intermediates
sudo mv privkey.pem   /etc/ssl/apinx/privkey.pem
sudo chmod 600 /etc/ssl/apinx/privkey.pem

With Let's Encrypt, certbot --nginx -d console.example.com -d api.example.com after step 9 obtains and renews the certificate and edits the site for you.

9. nginx

sudo apt install -y nginx

Ubuntu's nginx.conf allows 768 connections per worker, and every proxied request uses two: the caller's and the one to the gateway. Raise it, and let the workers open the files for it:

sudo sed -i 's/worker_connections [0-9]*;/worker_connections 65535;/' /etc/nginx/nginx.conf
sudo sed -i 's/^worker_rlimit_nofile [0-9]*;/worker_rlimit_nofile 131072;/' /etc/nginx/nginx.conf
grep -q '^worker_rlimit_nofile' /etc/nginx/nginx.conf || sudo sed -i '/^worker_processes/a worker_rlimit_nofile 131072;' /etc/nginx/nginx.conf

65535 is the most one worker takes; each worker reserves about 30 MB for it. The second line replaces a value an earlier install set; the third adds it when there is none.

/etc/nginx/sites-available/apinx:

# Connections to Apinx stay open between requests: a new one per request
# costs a handshake and an accept each time. keepalive_timeout stays under
# the listener's idle_timeout (60 s by default).
upstream apinx_console {
    server 127.0.0.1:9100;
    keepalive 16;
    keepalive_timeout 50s;
}

upstream apinx_gateway {
    server 127.0.0.1:8081;
    keepalive 64;
    keepalive_timeout 50s;
}

# nginx's counters for Admin › About › Capacity (capacity.nginx_status_url)
server {
    listen 127.0.0.1:8099;
    access_log off;
    location = /nginx_status { stub_status; }
    location / { return 404; }
}

server {
    listen 80;
    server_name console.example.com api.example.com;
    return 301 https://$host$request_uri;
}

# Console and Admin API
server {
    listen 443 ssl;
    http2 on;    # nginx 1.25.1+; on nginx 1.24 (Ubuntu 24.04) write "listen 443 ssl http2;" instead
    server_name console.example.com;
    ssl_certificate     /etc/ssl/apinx/fullchain.pem;
    ssl_certificate_key /etc/ssl/apinx/privkey.pem;
    add_header Strict-Transport-Security "max-age=31536000" always;

    # allow 203.0.113.0/24;  deny all;      # optional: office addresses only

    # Metrics need no session: keep them on the host; Prometheus scrapes http://127.0.0.1:9100/metrics.
    location = /metrics { return 404; }

    location / {
        proxy_pass http://apinx_console;
        proxy_http_version 1.1;
        proxy_set_header Connection "";          # kept open for the next request
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_buffering off;                 # the live traffic view and the assistant's answers stream
        proxy_read_timeout 3600s;
    }
}

# The APIs you publish
map $http_upgrade $connection_upgrade {        # WebSocket routes: pass the upgrade through;
    default upgrade;                           # other requests keep the connection open
    ''      '';
}

server {
    listen 443 ssl;
    http2 on;
    server_name api.example.com;
    ssl_certificate     /etc/ssl/apinx/fullchain.pem;
    ssl_certificate_key /etc/ssl/apinx/privkey.pem;
    client_max_body_size 20m;

    location / {
        proxy_pass http://apinx_gateway;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_buffering off;                     # streamed responses pass through as they come
    }
}
sudo ln -sf /etc/nginx/sites-available/apinx /etc/nginx/sites-enabled/apinx
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl reload nginx

Then tell Apinx where nginx's counters are, in apinx.yaml, so Admin › About › Capacity shows the connections nginx holds and drops. nginx_max_connections is worker_connections times the workers (one per CPU): echo $(( $(nproc) * 65535 )).

capacity:
  nginx_status_url: "http://127.0.0.1:8099/nginx_status"
  nginx_max_connections: 8192

gRPC needs HTTP/2 all the way: nginx speaks HTTP/1.1 to a proxy_pass backend, which a gRPC call does not survive. Either let gRPC clients reach an https listener of Apinx directly (ALPN negotiates HTTP/2), or give the gateway's http listener h2c: true and hand gRPC calls over with grpc_pass, picked by their content type inside the server block above:

    location / {
        error_page 418 = @grpc;                    # the handover, not an answer
        if ($content_type ~* "^application/grpc") { return 418; }
        proxy_pass http://apinx_gateway;
        # ... the proxy_ lines above
    }
    location @grpc {
        grpc_pass grpc://apinx_gateway;
        grpc_set_header Host $host;
        grpc_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        grpc_set_header X-Forwarded-Proto https;
        grpc_read_timeout 3600s;                   # streams
        grpc_send_timeout 3600s;
        client_max_body_size 0;
    }

Because nginx forwards X-Forwarded-For, set trust_x_forwarded_for: true on policies that key by client address (rate-limit with key: ip, concurrency-limit, ip-filter, per-key allowed sources); otherwise every caller looks like 127.0.0.1. The gateway then takes the last entry of the header, the one nginx appended ($proxy_add_x_forwarded_for), and only when the connection comes from a loopback, private or link-local address (nginx on the same host or network): a client that sends its own X-Forwarded-For, or reaches the gateway port directly, gains nothing. With more than one proxy in front (a CDN, then nginx), list them in trusted_proxies and the rightmost entry that is not a proxy is the caller.

The gateway itself appends the peer to X-Forwarded-For on the way to the backend and sets X-Forwarded-Host, X-Forwarded-Proto and Via: 1.1 apinx. A backend must likewise take the rightmost entry it trusts; other client-supplied headers (X-Real-IP) pass through unchanged.

Behind Cloudflare

If the DNS records are proxied (orange cloud), dig returns Cloudflare's addresses, not yours; that is expected. Then:

  1. SSL/TLS → Overview → Full (strict). Never "Flexible": it loops with the port-80 redirect.
  2. Log and limit by the real client address:
    { for ip in $(curl -s https://www.cloudflare.com/ips-v4) $(curl -s https://www.cloudflare.com/ips-v6); do echo "set_real_ip_from $ip;"; done; echo "real_ip_header CF-Connecting-IP;"; } | sudo tee /etc/nginx/conf.d/cloudflare-realip.conf
    sudo nginx -t && sudo systemctl reload nginx
    
  3. Optionally let only Cloudflare reach 443 (step 10).

10. Firewall

sudo ufw status                      # make sure OpenSSH is allowed before enabling
sudo ufw allow 80/tcp && sudo ufw allow 443/tcp
sudo ufw enable

Behind Cloudflare, replace the 443 rule with one per Cloudflare range:

for ip in $(curl -s https://www.cloudflare.com/ips-v4) $(curl -s https://www.cloudflare.com/ips-v6); do sudo ufw allow from $ip to any port 443 proto tcp; done

Leave 5432, 8081 and 9100 closed; they are loopback-only. Open 80 and 443 in your provider's firewall too if it has one.

11. Verify and claim

curl -I https://console.example.com/          # 200
curl -i https://api.example.com/              # 404 APX-RT-0001 until an API is deployed: expected

Open https://console.example.com, paste the claim token, create the administrator. Deploy a first API and call it on https://api.example.com/...; Analyze → Traffic shows it with the real client address.

12. Licence

Install the licence file you received from MoznHub; until then the installation is an evaluation (30 days, every feature). The simplest is the console: Admin › About › Install licence, paste the whole text including the BEGIN and END lines. To keep it with the server's configuration instead:

sudo install -m 0640 -o root -g apinx apinx.lic /etc/apinx/apinx.lic
echo 'APINX_LICENSE_FILE=/etc/apinx/apinx.lic' | sudo tee -a /etc/apinx/apinx.env
sudo systemctl restart apinx
apinx license show /etc/apinx/apinx.lic        # licensee, number, maintenance date; verified offline

Admin › About then shows the licensee, the number and the maintenance date. A paid licence restricts nothing: no instance count, no feature switch. See Licensing.

For a UAT instance beside this one, see UAT and production.

Upgrading

sha256sum --check --ignore-missing SHA256SUMS
sudo cp /usr/local/bin/apinx /usr/local/bin/apinx.previous
sudo install -m 0755 apinx-linux-amd64 /usr/local/bin/apinx
sudo systemctl restart apinx && sudo journalctl -u apinx -n 20 --no-pager

Migrations run forward automatically; take a database dump first (Runbook). In-flight requests drain for up to shutdown.drain_timeout. For zero-downtime upgrades run two instances behind nginx sharing one PostgreSQL (Upgrading).

Removing everything

sudo systemctl disable --now apinx
sudo rm -f /etc/systemd/system/apinx.service && sudo systemctl daemon-reload
sudo rm -f /usr/local/bin/apinx /usr/local/bin/apinxctl
sudo rm -rf /etc/apinx /var/lib/apinx /var/lib/apinx-keys    # deletes the data and the master key
sudo userdel apinx

Drop the database (or remove the container and its volume) and the nginx site separately.

Where things are

Item Path
Binaries /usr/local/bin/apinx, /usr/local/bin/apinxctl
Configuration /etc/apinx/apinx.yaml, /etc/apinx/gateway.yaml
Secrets (0600) /etc/apinx/apinx.env
Licence /etc/apinx/apinx.lic, or installed from Admin › About
Log rotation /etc/logrotate.d/apinx
Data, access log /var/lib/apinx/, /var/lib/apinx/logs/access.log
Generated development key (unused once apinx.env sets one) /var/lib/apinx-keys/
Process log journalctl -u apinx
nginx site /etc/nginx/sites-available/apinx