Load Balanced Bun js api; .env, supervisor,

I have a somewhat complex and lengthy question.
I’m using Bun.js to run the backend API.

I want to use a load balancer for webify.rest, and I want to distribute traffic across balanced sites behind that load balancer.

I generally know how to set this up, but there are some unclear parts I’d like to confirm or understand better:

  1. I create an API key at Hetzner and enter it in the VPS settings.
  2. I connect my GitHub account in the version control settings.
  3. I create a new server to act as the load balancer, naming it webify.rest.
  4. I point the DNS for webify.rest to the IP of the load balancer server.
  5. I set up a load balancing site on the server. Here, I’m unsure what App type to select for a Bun.js backend. I choose Static HTML since it seems the most minimal, enter webify.rest as the domain, and enable the free SSL option.
  6. I create another server called Api-01, which will run the Bun.js API. I select Plain Server.
  7. On Api-01, I manually install Bun and Nginx via Services (needed to create a site).
  8. I create a site on Api-01, again using the Static HTML type, with the domain 01.webify.rest, and I skip SSL (since this is a balanced site).
  9. I return to the load balancer server (webify.rest) and click “Link your servers.” I select the Api-01 server and click update. The site 01.webify.rest is automatically linked to the load balancer as a balanced site.
  10. I open webify.rest in a browser and see the default “clear” landing page prompting me to deploy my app.
  11. I go to the Deployment section and see two deployments, but they appear on the same level. Shouldn’t 01.webify.rest be nested under webify.rest as part of the balanced group? Or is this normal?
  12. I open the deployment settings for webify.rest (the load balancer), link the GitHub repository, and enter the environment variables under the Environment section.
  13. I leave the deployment settings for 01.webify.rest (the balanced site) untouched, assuming the load balancer handles deployment and that the balanced site inherits everything. Is that correct?

This is where things get blurry for me:

  1. When I push code to GitHub, the deployment process is triggered, and the files are uploaded to the server.
  2. However, after deployment, I need to run cluster.js and inject environment variables into the script. In development, I use:
bun --env-file=env/dev.env ./src/server/server/cluster.ts

How can I automate this so it runs every time after deployment?

  1. Once that’s resolved, how do I keep the app running? For example, how can I use Supervisor (or something else) to ensure the app stays alive and restarts with the correct environment variables if the server restarts?

My questions in short:

  • Do I choose Static HTML as the site type and manually install Nginx and Bun?
  • Is the deployment process I described correct?
  • If I enter the environment variables in the load balancer, do the balanced sites automatically inherit them?
  • How do I start cluster.js with injected environment variables automatically after deployment?
  • How do I use Supervisor (or similar) to keep the app running and restart it with env variables if the server reboots?

Apologies for the long message — I’d really appreciate your guidance on this.



Hi @webify - thanks for the details!

For the target servers, you can provision as an app server to include NGINX. Though, if you are really trying to minimize services, then provisioning a plain server and then adding a basic html site and then adding any services needed is a good way to go.

When adding a new site, a deployment workflow is automatically created. When adding load balancer and linked sites, another is created. The latter would be the deployment flow to use ultimately - as it will include any other target balanced servers and essentially will run the deployment process on each of the target servers a site is added to.

Yes, this will be managed across all target servers that the app sits on.

I think you could do this by adding a custom deployment hook.

You can add to the Server > Process Monitor section for each target server - though if you’re wanting to use Supervisor, you’d need to install that via Services.

Thanks for the reply, will give a go / try.