How to deploy a SvelteKit application on a VPS with PM2 and Nginx

How to deploy a SvelteKit application on a VPS with PM2 and Nginx

May 22, 2025

Here’s a parallel walkthrough for deploying your SvelteKit app on an Ubuntu VPS, managed by PM2 and fronted by Nginx:


1. VPS Prep & SSH Access

  1. Provision your VPS, e.g. Ubuntu 22.04 LTS on DigitalOcean, AWS EC2, etc.

  2. Ensure SSH (port 22) is open, or configure your custom SSH port.

  3. SSH in:

    ssh root@your_server_ip
    

2. System Update & Node.js Installation

# Update system packages
sudo apt update && sudo apt upgrade -y

# Install curl & build essentials
sudo apt install -y curl build-essential

# Install Node.js LTS via NodeSource
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs
  • Verify:

    node -v    # e.g. v18.x
    npm -v     # e.g. v9.x
    

3. Create a Deployment User (Optional)

sudo adduser deployer
sudo usermod -aG sudo deployer
  • Then exit and re-SSH as deployer for safer ops.

4. Clone & Build Your SvelteKit App

  1. Navigate to your apps directory:

    mkdir -p ~/apps/sveltekit-app && cd ~/apps/sveltekit-app
    
  2. Clone your repo:

    git clone https://github.com/yourusername/your-sveltekit-repo.git .
    
  3. Install deps & build:

    npm ci           # or yarn install --frozen-lockfile
    npm run build
    

5. Configure PM2 to Run SvelteKit

  1. Install PM2 globally:

    npm install -g pm2
    
  2. Start your SvelteKit app:

    pm2 start node --name "sveltekit-app" -- .svelte-kit/node/server/index.js
    

    Note: The entry path may vary by adapter; adjust if you’re using @sveltejs/adapter-node or another adapter.

  3. Optional: Create an ecosystem file:

    pm2 ecosystem
    

    Edit ecosystem.config.js to point at your build output, e.g.:

    module.exports = {
      apps: [
        {
          name: "sveltekit-app",
          script: ".svelte-kit/node/server/index.js",
          cwd: "/home/deployer/apps/sveltekit-app",
          instances: "max",
          exec_mode: "cluster",
          env: { NODE_ENV: "production" }
        }
      ]
    };
    
  4. Start via ecosystem:

    pm2 start ecosystem.config.js
    
  5. Persist PM2 across reboots:

    pm2 save
    pm2 startup systemd
    # Follow the instructions printed to enable
    

6. Install & Configure Nginx

  1. Install Nginx:

    sudo apt install -y nginx
    
  2. Create Nginx site file at /etc/nginx/sites-available/sveltekit-app:

    server {
      listen 80;
      server_name your_domain_or_ip;
    
      location / {
        proxy_pass http://127.0.0.1:3000;      # SvelteKit default port
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
      }
    
      # Serve static assets directly
      location /_app/ {
        alias /home/deployer/apps/sveltekit-app/static/;
        access_log off;
        expires max;
      }
    }
    
  3. Enable & reload:

    sudo ln -s /etc/nginx/sites-available/sveltekit-app /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl reload nginx
    

7. (Optional) Secure with Let’s Encrypt SSL

  1. Install Certbot:

    sudo apt install -y certbot python3-certbot-nginx
    
  2. Obtain & configure cert:

    sudo certbot --nginx -d your_domain.com -d www.your_domain.com
    
  3. Auto-renewal runs by default via cron/systemd.


8. Monitoring & Logs

  • Check PM2 status:

    pm2 status
    
  • View logs:

    pm2 logs sveltekit-app --lines 100
    
  • Dev mode with auto-restart:

    pm2 start npm --name "sveltekit-dev" --watch -- run dev
    

Recap of Key Commands

# System & Node
sudo apt update && sudo apt upgrade -y
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs nginx

# Clone & build
git clone repo .
npm ci && npm run build

# PM2
npm install -g pm2
pm2 start node --name "sveltekit-app" -- .svelte-kit/node/server/index.js
pm2 save && pm2 startup systemd

# Nginx
# create /etc/nginx/sites-available/sveltekit-app (as above)
sudo ln -s /etc/nginx/sites-available/sveltekit-app /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

# SSL (optional)
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d your_domain.com

With this setup, your SvelteKit application will run under PM2—automatically restarting on crashes or reboots—and be served securely behind Nginx.

Comments

No comments yet.

Related Posts