/ Docs

Deploying to a VPS

Recommended
~5$/month

This installs and runs both the web client and api, if you want just the web client move to manual installation steps.

install.sh works on debian based distros only, for now. If you're running any other system move to manual installation steps and find replacements for the used packages.

Requirements:

  • a VPS
  • Fully qualified domain name

OPTION 1. Install script ( Recommended )

1. SSH into your VPS and create an empty folder in your home directory, ex. mkdir khofly

2. cd khofly and type git clone https://github.com/cufta22/khofly.git .

2.1. Pick a branch, by default it will be on master but if you want more frequent updates git fetch origin staging and git checkout -b staging origin/staging

3. Once the code is fetched, make sure that you can execute the install script chmod +x ./scripts/install.sh

3.1. Don't blindly trust any script you pull from the internet, you can inspect it with cat ./scripts/install.sh

4. Now you can run the script with sudo ./scripts/install.sh, if you didn't get any errors the installation was successful.

5. Edit the .env file for web ( nano ./web/.env.local ) values per provided comments.

5.1. Run ./scripts/redeploy-web.sh web ( web argument is the name of pm2 instance ) to rebuild the app since we changed the env file.

6. Edit the .env file for api ( nano ./api/.env.local ) values per provided comments.

6.1. Run ./scripts/redeploy-api.sh api ( api argument is the name of pm2 instance ) to rebuild the app since we changed the env file.

7. cd /etc/nginx/sites-available/ and edit the domain names for web and api files, and whatever other Nginx config you want to add.

8. Add SSL certificate for your domain certbot --nginx

9. sudo systemctl reload nginx

OPTION 2. Manual installation

1. Install dependencies

apt update && apt upgrade
apt install nodejs npm build-essential libssl-dev unzip nginx certbot python3-certbot-nginx ffmpeg

2. Install NVM

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
source ~/.bashrc
nvm install 22

3. Install pnpm

curl -fsSL https://get.pnpm.io/install.sh | env PNPM_VERSION=10.0.0 sh -

4. Install pm2

npm install pm2 -g
source ~/.bashrc

5. Create an empty folder in your home directory, ex. mkdir khofly.

6. cd khofly and type git clone https://github.com/cufta22/khofly.git .

6.1. Pick a branch, by default it will be on master but if you want more frequent updates git fetch origin staging and git checkout -b staging origin/staging

7. Build and run web client

cd web

# First create the .env.local file from example file
cp .env.example .env.local

# Edit the values per provided comments
nano .env.local

# Then we can install dependencies and build
pnpm install
pnpm run build

8. Create the ecosystem.config file for pm2

# Create the config
touch ecosystem.config.cjs

# Paste the base config from below, edit whatever you want
nano ecosystem.config.cjs

# Start the web client with pm2
pm2 start
module.exports = {
  apps : [{
    name: 'web',
    script: 'pnpm',
    args: 'run start',
    env: {
        PORT: 3001
    }
  }]
};

9. Create Nginx config for web, don't forget to update the server_name to your domain name.

cd /etc/nginx/sites-available/

# Create the config for the web client
touch web

# Paste the base config from below, edit whatever you want
nano web

# Link that file to /sites-enabled
ln -s /etc/nginx/sites-available/web /etc/nginx/sites-enabled/
server {
    server_name example.com;

    root /root/web;

    location / {
        # Proxy to pm2 server on 3001 for web

        proxy_pass http://localhost:3001;
        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;
    }

    listen 80;
}

10. Add SSL certificate for your domain certbot --nginx

11. sudo systemctl reload nginx

Updating

To update the web client run ./scripts/redeploy-web.sh web, make sure to replace "web" with pm2 instance name for your web client.

To get a list of all pm2 instances run pm2 ls

Read more