Install n8n on a VPS?
This guide shows how to install n8n on a VPS using Docker Compose and a reverse proxy. You can run self-hosted n8n on a small VPS by using Docker, Docker Compose, and a domain with HTTPS. The steps below give working commands, persistent storage, and a safe update path.
What You Need
- A VPS running a modern Linux (Ubuntu 22.04 or similar).
- A domain name or a public IP for webhooks.
- Docker and Docker Compose installed on the VPS.
- Basic shell access and sudo rights.
- Optional: a PostgreSQL service for reliable storage.
How to install n8n on a VPS with Docker Compose
Step 1: Prepare the server
- Update packages and install basic tools.
sudo apt update && sudo apt upgrade -y
sudo apt install -y git curl
Step 2: Install Docker and Docker Compose
- Install Docker engine and Compose plugin with these commands.
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker $USER
sudo apt install -y docker-compose-plugin
Log out and back in or run newgrp docker to apply the group change.
Step 3: Create the n8n project
- Create a folder and a .env file to hold secrets.
mkdir -p ~/n8n && cd ~/n8n
cat > .env <<'EOF'
N8N_BASIC_AUTH_ACTIVE=true
N8N_BASIC_AUTH_USER=admin
N8N_BASIC_AUTH_PASSWORD=ChangeMeStrongPassword
DB_TYPE=postgresdb
DB_POSTGRESDB_HOST=postgres
DB_POSTGRESDB_PORT=5432
DB_POSTGRESDB_DATABASE=n8n
DB_POSTGRESDB_USER=n8n
DB_POSTGRESDB_PASSWORD=n8n_pass
WEBHOOK_URL=https://your-domain.example
N8N_HOST=your-domain.example
EOF
Replace the user, password, and domain values before continuing.
Step 4: Create docker-compose.yml
Use PostgreSQL for production reliability. This file creates two services: postgres and n8n.
cat > docker-compose.yml <<'EOF'
version: '3.8'
services:
postgres:
image: postgres:14
restart: unless-stopped
environment:
POSTGRES_DB: n8n
POSTGRES_USER: n8n
POSTGRES_PASSWORD: n8n_pass
volumes:
- ./postgres-data:/var/lib/postgresql/data
n8n:
image: n8nio/n8n:latest
restart: unless-stopped
env_file:
- ./.env
ports:
- 5678:5678
volumes:
- ./n8n-data:/home/node/.n8n
depends_on:
- postgres
EOF
Step 5: Start n8n
- Bring up the stack and check logs.
docker compose up -d
docker compose logs -f n8n
Visit https://your-domain.example after you configure a reverse proxy and TLS. If you use a public IP, test on http://your-ip:5678 first (not recommended for production).
Reverse proxy and HTTPS
Use a reverse proxy to provide HTTPS and a proper domain. Caddy or Nginx works well. Caddy can get certificates automatically.
# Example Caddyfile
your-domain.example {
reverse_proxy 127.0.0.1:5678
}
# Then run Caddy on the host or as a container.
Update
To update n8n and the database image, pull new images and recreate containers. Keep backups before major updates.
docker compose pull
docker compose up -d --remove-orphans
# Optional: backup volumes
tar czf n8n-backup-$(date +%F).tar.gz n8n-data postgres-data
Security
- Enable N8N_BASIC_AUTH_ACTIVE and use a strong password.
- Run n8n behind HTTPS. Use a trusted certificate authority.
- Store secrets in a .env file with restricted permissions.
- Use PostgreSQL instead of SQLite for production and restrict DB access by firewall.
- Keep Docker and images updated and monitor logs.
Done
n8n should now run on your VPS with persistent storage and a basic security layer. You can extend this by adding more environment variables, using a managed DNS, or deploying a production-grade reverse proxy. Congratulations on running self-hosted workflow automation.