Advanced

Deployment

Deploy your CanxJS application to production with Docker, VPS, or cloud platforms.

VPS/Bare Metal

Deploy with systemd and nginx

Docker

Containerized deployment

Cloud Platforms

Railway, Fly.io, Render

Edge Ready

Deploy to edge networks

Production Build

terminal
1# Build for production
2bun build ./src/index.ts --outdir ./dist --target bun --minify
3
4# Or use the CLI
5bunx canx build

Environment Configuration

canx.config.ts
1// canx.config.ts
2export default {
3 port: parseInt(process.env.PORT || "3000"),
4 hostname: process.env.HOST || "0.0.0.0",
5 development: process.env.NODE_ENV !== "production",
6
7 database: {
8 driver: "mysql",
9 host: process.env.DB_HOST,
10 port: parseInt(process.env.DB_PORT || "3306"),
11 database: process.env.DB_NAME,
12 username: process.env.DB_USER,
13 password: process.env.DB_PASSWORD,
14 },
15
16 cors: {
17 origin: process.env.CORS_ORIGIN?.split(",") || false,
18 }
19};
.env.production
1# .env.production
2NODE_ENV=production
3PORT=3000
4HOST=0.0.0.0
5
6# Database
7DB_HOST=localhost
8DB_PORT=3306
9DB_NAME=myapp
10DB_USER=myapp
11DB_PASSWORD=secret
12
13# Security
14JWT_SECRET=your-super-secret-key
15CORS_ORIGIN=https://example.com
16
17# Optional
18LOG_LEVEL=info

Docker Deployment

Dockerfile
1# Dockerfile
2FROM oven/bun:1 AS base
3WORKDIR /app
4
5# Install dependencies
6FROM base AS deps
7COPY package.json bun.lockb ./
8RUN bun install --frozen-lockfile --production
9
10# Build
11FROM base AS build
12COPY package.json bun.lockb ./
13RUN bun install --frozen-lockfile
14COPY . .
15RUN bun build ./src/index.ts --outdir ./dist --target bun --minify
16
17# Production
18FROM base AS runner
19ENV NODE_ENV=production
20COPY --from=deps /app/node_modules ./node_modules
21COPY --from=build /app/dist ./dist
22COPY --from=build /app/package.json ./
23
24EXPOSE 3000
25CMD ["bun", "run", "./dist/index.js"]
docker-compose.yml
1# docker-compose.yml
2version: "3.8"
3
4services:
5 app:
6 build: .
7 ports:
8 - "3000:3000"
9 environment:
10 - NODE_ENV=production
11 - DB_HOST=db
12 - DB_PORT=3306
13 - DB_NAME=myapp
14 - DB_USER=myapp
15 - DB_PASSWORD=secret
16 depends_on:
17 - db
18
19 db:
20 image: mysql:8
21 environment:
22 - MYSQL_DATABASE=myapp
23 - MYSQL_USER=myapp
24 - MYSQL_PASSWORD=secret
25 - MYSQL_ROOT_PASSWORD=rootsecret
26 volumes:
27 - mysql_data:/var/lib/mysql
28
29volumes:
30 mysql_data:

VPS Deployment

canxjs.service
1# /etc/systemd/system/canxjs.service
2[Unit]
3Description=CanxJS Application
4After=network.target
5
6[Service]
7Type=simple
8User=www-data
9WorkingDirectory=/var/www/myapp
10ExecStart=/root/.bun/bin/bun run dist/index.js
11Restart=on-failure
12RestartSec=10
13Environment=NODE_ENV=production
14
15[Install]
16WantedBy=multi-user.target
17
18# Enable and start
19# sudo systemctl enable canxjs
20# sudo systemctl start canxjs
nginx.conf
1# /etc/nginx/sites-available/myapp
2upstream canxjs {
3 server 127.0.0.1:3000;
4 keepalive 64;
5}
6
7server {
8 listen 80;
9 server_name example.com;
10 return 301 https://$server_name$request_uri;
11}
12
13server {
14 listen 443 ssl http2;
15 server_name example.com;
16
17 ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
18 ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
19
20 location / {
21 proxy_pass http://canxjs;
22 proxy_http_version 1.1;
23 proxy_set_header Upgrade $http_upgrade;
24 proxy_set_header Connection "upgrade";
25 proxy_set_header Host $host;
26 proxy_set_header X-Real-IP $remote_addr;
27 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
28 proxy_set_header X-Forwarded-Proto $scheme;
29 }
30}

Health Checks

health.ts
1import { createApp, healthCheck } from "canxjs";
2
3const app = createApp({ port: 3000 });
4
5// Health check endpoint
6app.get("/health", healthCheck({
7 checks: [
8 { name: "database", check: async () => await db.ping() },
9 { name: "redis", check: async () => await redis.ping() }
10 ]
11}));
12
13// Returns: { status: "healthy", checks: [...], uptime: 12345 }

Need Help?

Check the API reference or join our community.