Documentation
v1.0.0
Advanced
Self-Hosting

Self-Hosting

MekongTunnel is fully self-hostable (MIT licensed). You need:

  • A Linux VPS with a public IP and ports 22, 80 and 443 free
  • A domain name you control
  • A wildcard TLS certificate for your tunnel domain (free from Let's Encrypt)

Throughout this page we use tunnel.example.com as the tunnel domain. Tunnels will get URLs like https://happy-tiger-a1b2c3d4.tunnel.example.com. (The public service uses proxy.mekongtunnel.dev.)

You can also use your bare domain (example.com) as the tunnel domain — just use it everywhere tunnel.example.com appears below.


Step 1 — DNS records

Add two A records pointing to your server:

TypeNameValue
Atunnel.example.comYOUR_SERVER_IP
A*.tunnel.example.comYOUR_SERVER_IP

The wildcard record sends every tunnel subdomain to your server.


Step 2 — Wildcard TLS certificate

Wildcard certificates need a DNS challenge:

sudo apt install certbot
 
sudo certbot certonly --manual --preferred-challenges dns \
  -d tunnel.example.com \
  -d '*.tunnel.example.com'

Certbot asks you to add a _acme-challenge TXT record, then saves:

/etc/letsencrypt/live/tunnel.example.com/fullchain.pem
/etc/letsencrypt/live/tunnel.example.com/privkey.pem

Tip: if your DNS provider has a certbot plugin (Cloudflare, DigitalOcean, Route 53, …) use it instead of --manual so renewals are automatic.


Step 3 — Move your own SSH to port 2222

MekongTunnel listens on port 22, so move the server's admin SSH first:

sudo nano /etc/ssh/sshd_config
# Change:  Port 22  →  Port 2222
sudo systemctl restart ssh      # "sshd" on some distros

Open a new session on port 2222 (ssh -p 2222 you@server) before closing the current one.

Open the firewall:

sudo ufw allow 2222/tcp     # your own SSH
sudo ufw allow 22/tcp       # MekongTunnel SSH (tunnel clients)
sudo ufw allow 80/tcp       # HTTP → HTTPS redirect
sudo ufw allow 443/tcp      # HTTPS tunnel traffic

Step 4 — Run the server

Pick one option.

Option A — Tunnel server only (simplest)

Anonymous tunnels, no accounts or dashboard. No database needed.

git clone https://github.com/MuyleangIng/MekongTunnel.git
cd MekongTunnel
docker build -t mekongtunnel .
 
# Certificates + a folder for the SSH host key.
# The container runs as an unprivileged user (UID 65534), so give it ownership.
sudo mkdir -p /opt/mekongtunnel/certs /opt/mekongtunnel/hostkey
sudo cp /etc/letsencrypt/live/tunnel.example.com/fullchain.pem /opt/mekongtunnel/certs/
sudo cp /etc/letsencrypt/live/tunnel.example.com/privkey.pem   /opt/mekongtunnel/certs/
sudo chown -R 65534:65534 /opt/mekongtunnel
 
docker run -d --name mekongtunnel --restart unless-stopped \
  -p 22:22 -p 80:80 -p 443:443 \
  -e DOMAIN=tunnel.example.com \
  -e TLS_CERT=/certs/fullchain.pem \
  -e TLS_KEY=/certs/privkey.pem \
  -e HOST_KEY_PATH=/data/hostkey/host_key \
  -v /opt/mekongtunnel/certs:/certs:ro \
  -v /opt/mekongtunnel/hostkey:/data/hostkey \
  mekongtunnel
 
docker logs -f mekongtunnel

The SSH host key is generated on first start and kept in /opt/mekongtunnel/hostkey, so clients don't see a new key after restarts.

Option B — Full stack (accounts, API, reserved subdomains)

Runs PostgreSQL, Redis, the API and the tunnel server with Docker Compose:

git clone https://github.com/MuyleangIng/MekongTunnel.git
cd MekongTunnel
 
cp .env.compose.prod.example .env.compose.prod
nano .env.compose.prod
#   DOMAIN=tunnel.example.com
#   PUBLIC_URL=https://api.example.com        (where the API will be reachable)
#   FRONTEND_URL / ALLOWED_ORIGINS            (your dashboard URL)
#   POSTGRES_PASSWORD, JWT_SECRET, REFRESH_SECRET, ADMIN_EMAIL, ADMIN_PASSWORD
#   → use long random values, e.g. `openssl rand -hex 32`
 
mkdir -p deploy/certs
sudo cp /etc/letsencrypt/live/tunnel.example.com/fullchain.pem deploy/certs/
sudo cp /etc/letsencrypt/live/tunnel.example.com/privkey.pem   deploy/certs/
sudo chown -R 65534:65534 deploy/certs
 
# The tunnel service is behind the "tunnel" profile — don't forget --profile tunnel
docker compose --env-file .env.compose.prod \
  -f docker-compose.yml -f docker-compose.prod.yml \
  --profile tunnel up -d --build
 
docker compose --env-file .env.compose.prod \
  -f docker-compose.yml -f docker-compose.prod.yml logs -f mekong-tunnel

The API listens on port 8080 (API_PORT); put a reverse proxy such as Caddy or nginx in front of it for HTTPS. The web dashboard is a separate Next.js app: KhmerStack/mekongtunnel-ui (opens in a new tab).


Step 5 — Connect

With the mekong CLI, point it at your server:

mekong --server tunnel.example.com 3000
 
# or set it once for every command
export MEKONG_TUNNEL_DOMAIN=tunnel.example.com
mekong 3000

Or with plain SSH — no install needed:

ssh -t -R 80:localhost:3000 tunnel.example.com

You'll get a URL like https://happy-tiger-a1b2c3d4.tunnel.example.com.


Run with systemd (no Docker)

# Needs Go 1.24+. Builds bin/mekongtunnel (server) and bin/mekong (CLI)
make build-small
 
sudo mkdir -p /opt/mekongtunnel
sudo cp bin/mekongtunnel /opt/mekongtunnel/
sudo nano /etc/systemd/system/mekongtunnel.service
[Unit]
Description=MekongTunnel SSH Tunnel Service
After=network-online.target
Wants=network-online.target
 
[Service]
Type=simple
User=root
WorkingDirectory=/opt/mekongtunnel
ExecStart=/opt/mekongtunnel/mekongtunnel
Restart=always
RestartSec=5
 
Environment=DOMAIN=tunnel.example.com
Environment=SSH_ADDR=:22
Environment=HTTP_ADDR=:80
Environment=HTTPS_ADDR=:443
Environment=STATS_ADDR=127.0.0.1:9090
Environment=HOST_KEY_PATH=/opt/mekongtunnel/host_key
Environment=TLS_CERT=/etc/letsencrypt/live/tunnel.example.com/fullchain.pem
Environment=TLS_KEY=/etc/letsencrypt/live/tunnel.example.com/privkey.pem
# Optional: connect a database to enable account tokens and reserved subdomains
# Environment=DATABASE_URL=postgres://mekong:PASSWORD@127.0.0.1:5432/mekong?sslmode=disable
 
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now mekongtunnel
sudo systemctl status mekongtunnel

The stats endpoint stays private on 127.0.0.1:9090.


Useful settings

All optional — set them as environment variables (Docker -e, Compose env file, or systemd Environment=):

VariableDefaultMeaning
MAX_TUNNELS_PER_IP1000Tunnels one client IP can open
MAX_TOTAL_TUNNELS0 (unlimited)Tunnels across the whole server
MAX_CONNECTIONS_PER_MINUTE0 (unlimited)New SSH connections per IP per minute
REQUESTS_PER_SECOND / BURST_SIZE0 (off)Per-tunnel HTTP rate limit
MAX_REQUEST_BODY_MB / MAX_RESPONSE_BODY_MB1024Body size limits

Renewing certificates

Let's Encrypt certificates last 90 days. After renewing, copy them again and restart:

sudo certbot renew
 
# Option A (docker run)
sudo cp /etc/letsencrypt/live/tunnel.example.com/*.pem /opt/mekongtunnel/certs/
sudo chown 65534:65534 /opt/mekongtunnel/certs/*.pem
docker restart mekongtunnel
 
# Option B (compose)
sudo cp /etc/letsencrypt/live/tunnel.example.com/*.pem deploy/certs/
sudo chown 65534:65534 deploy/certs/*.pem
docker compose --env-file .env.compose.prod -f docker-compose.yml -f docker-compose.prod.yml restart mekong-tunnel
 
# systemd (reads the files directly)
sudo systemctl restart mekongtunnel