Skip to main content

Deploy Bun Apps

Bun installation is powered by webi. To install latest version with deployment script:

features:
- bun

Without this, bun won't be available since no globally installed Bun runtime available.

Install other version using bun latest, bun beta, bun 1.1.0, etc. To uninstall use bun off. Check the current version using bun --version from SSH.

Example

The deployment script below installs the latest bun compiler and writes app.ts and serves that through proxfix.

https://github.com/domcloud/recipes/blob/master/bun.yml
source: clear
features:
- bun latest
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT proxfix bun app.ts
commands:
- filename: app.ts
content: |
const html_text = `
<!DOCTYPE html>
<html>
<head>
<title>Bun App</title>
<link rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css">
</head>
<body class="p-5 text-center">
<p><img src="//images.unsplash.com/photo-1465153690352-10c1b29577f8?fit=crop&w=200&h=200"
class="img-fluid rounded-circle"></p>
<h1 class="mb-3">Hello, world!</h1>
<p>Serving from Bun version ${Bun.version}</p>
<p class="text-muted">DOM Cloud</p>
</body>
</html>
`
const server = Bun.serve({
fetch(request) {
return new Response(html_text, {
headers: { "Content-Type": "text/html" },
});
},
});

Note three things:

  • Bun.serve already set the port arg from PORT env by default
  • proxfix bun app.ts is used to run Bun with a workaround of an ongoing issue
  • This proxfix proxies request to the bun app, this proxy support HTTP and Websocket

For existing bun apps, use this deployment script. Please adjust relevant fields such as the startup file (change app.ts) and how $PORT is delivered (via env PORT=$PORT proxfix bun ...).

https://github.com/domcloud/recipes/blob/master/bun-gls.yml
features:
- bun latest
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT proxfix bun app.ts

App Management

Your app do not restarted automatically after file changes. To restart, run restart via SSH.

Environment variables can be set either using NGINX's env_var_list or ~/.bashrc. Usually your language framework also reads .env files.

See NGINX and App Daemon for more information about NGINX and App managements including restarting, environment variables, and other global limitations.

App Logging

You can see app log from Check -> Check Process Logs tab. Only startup problems displayed in the browser.

Bun can show runtime errors in browser by setting app_env: development in the passenger config of NGINX.

It's recommended to use a proper logging mechanism such as Pino or Winston then write it to a log file, or any other solution that suits you.

NGINX errors and traffic logs can be examined via Webmin or Check -> Check Process Logs tab.