Deploy with Bun Run
The simplest way to run a Bunty application is using the bun run command. While this is suitable for small applications and development, for production use cases, consider using a process manager like PM2 or systemd.
Basic Deployment
1. Install Bun on Your Server
# Install Bun
curl -fsSL https://bun.sh/install | bash
# Verify installation
bun --version
2. Clone Your Application
git clone https://github.com/yourusername/your-bunty-app.git
cd your-bunty-app
3. Install Dependencies
bun install --production
4. Set Environment Variables
Create a .env.production file:
NODE_ENV=production
PORT=3000
DATABASE_URL=postgresql://user:pass@localhost:5432/mydb
REDIS_URL=redis://localhost:6379
LOG_LEVEL=info
5. Build Your Application (if needed)
bun run build
6. Start the Application
# Direct execution
bun run src/main.ts
# Or using package.json script
bun start
Running in Background
Using nohup
nohup bun run src/main.ts > app.log 2>&1 &
Using screen
# Start a new screen session
screen -S bunty-app
# Run your app
bun run src/main.ts
# Detach with Ctrl+A, then D
# Reattach with: screen -r bunty-app
Using tmux
# Start a new tmux session
tmux new -s bunty-app
# Run your app
bun run src/main.ts
# Detach with Ctrl+B, then D
# Reattach with: tmux attach -t bunty-app
Environment-Specific Configuration
package.json Scripts
{
"scripts": {
"start": "bun run src/main.ts",
"start:prod": "NODE_ENV=production bun run src/main.ts",
"start:dev": "bun --watch src/main.ts"
}
}
Loading Environment Files
// src/main.ts
import { BuntyApplication } from '@bunty/common';
import { ConfigModule } from '@bunty/config';
async function bootstrap() {
const app = await BuntyApplication.create({
imports: [
ConfigModule.forRoot({
envFilePath: `.env.${process.env.NODE_ENV || 'development'}`,
}),
],
});
await app.listen(process.env.PORT || 3000);
}
bootstrap();
Clustering for Performance
Create a clustered setup manually:
// src/cluster.ts
import cluster from 'cluster';
import os from 'os';
if (cluster.isPrimary) {
const numCPUs = os.cpus().length;
console.log(`Primary process ${process.pid} is running`);
console.log(`Forking ${numCPUs} workers...`);
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died. Restarting...`);
cluster.fork();
});
} else {
// Workers share the same TCP connection
import('./main');
console.log(`Worker ${process.pid} started`);
}
Run with:
bun run src/cluster.ts
Health Checks
Add health check endpoints:
import { Controller, Get } from '@bunty/http';
@Controller()
export class HealthController {
@Get('/health')
health() {
return { status: 'ok', timestamp: new Date().toISOString() };
}
@Get('/ready')
ready() {
// Check database, cache, etc.
return { ready: true };
}
}
Graceful Shutdown
Handle shutdown signals properly:
// src/main.ts
async function bootstrap() {
const app = await BuntyApplication.create({
// ... config
});
await app.listen(3000);
// Graceful shutdown
const shutdown = async (signal: string) => {
console.log(`${signal} received, closing server...`);
await app.close();
process.exit(0);
};
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));
}
bootstrap();
Monitoring Logs
View Logs in Real-time
# If using nohup
tail -f app.log
# If using redirection
tail -f /var/log/bunty-app.log
Log Rotation
Install and configure logrotate:
# /etc/logrotate.d/bunty-app
/var/log/bunty-app.log {
daily
rotate 14
compress
delaycompress
notifempty
create 0640 bunty bunty
sharedscripts
postrotate
pkill -USR1 -f "bun run src/main.ts"
endscript
}
Limitations
While bun run works for simple deployments, it has limitations:
- ❌ No automatic restarts on crash
- ❌ No built-in clustering
- ❌ No process monitoring
- ❌ Manual log management
- ❌ No zero-downtime deployments
For production, consider using:
- PM2 for process management
- systemd for system service management
- Docker for containerization
- Kubernetes for orchestration
Next Steps
- Set up PM2 for better process management
- Configure systemd for automatic startup
- Add monitoring with @bunty/metrics