Railway Deployment Guide: From Zero to Production in 15 Minutes
Railway makes deploying Node.js and Next.js apps genuinely simple. Learn to deploy, configure environment variables, set up databases, and manage production deployments.
Why Railway
Railway is a deployment platform that hits the sweet spot between Heroku's simplicity and the flexibility of proper infrastructure. You get: Postgres/Redis/MySQL managed databases, automatic HTTPS, preview environments for PRs, and a pricing model that charges only for what you use.
Deploying From GitHub
Connect your GitHub repository and Railway auto-detects your framework and configures build commands. For Next.js, it just works:
# Install Railway CLI
npm install -g @railway/cli
# Login and link
railway login
railway init # create new project
railway link # or link to existing project
# Deploy current directory
railway up
# Tail logs
railway logs
Environment Variables
# Set individual variables
railway variables set DATABASE_URL=postgresql://...
railway variables set NEXTAUTH_SECRET=your-secret
railway variables set NODE_ENV=production
# Set from local .env file
railway variables set --file .env.production
# View all variables
railway variables
Adding a Postgres Database
One command adds a managed Postgres instance linked to your project:
railway add --database postgresql
# Railway automatically injects DATABASE_URL into your service
# You can reference it in your code immediately:
const db = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false } // Required for Railway Postgres
});
Setting Up a Next.js Deployment
// railway.json
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS",
"buildCommand": "npm run build"
},
"deploy": {
"startCommand": "npm start",
"healthcheckPath": "/api/health",
"healthcheckTimeout": 100,
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 3
}
}
// app/api/health/route.ts
export async function GET() {
return Response.json({ status: 'ok', timestamp: new Date().toISOString() });
}
Preview Environments
Railway can deploy a preview environment for every PR automatically. Set this up in your project settings under Environments. Each PR gets its own URL (useful for QA) and can share the staging database or get its own ephemeral one.
Custom Domains
# Add custom domain via CLI
railway domain add yourdomain.com
# Railway provides the DNS record to add:
# CNAME yourdomain.com -> myapp.up.railway.app
Running Database Migrations on Deploy
// railway.json — run Prisma migrations before starting
{
"deploy": {
"startCommand": "npx prisma migrate deploy && npm start"
}
}
Cost Comparison
Railway Hobby plan ($5/month free credit) is enough for a small production app with a database. A typical Next.js app + Postgres runs $5-20/month depending on usage. Vercel for the same setup with a managed Postgres database starts at $20/month. Railway wins on cost for backend-heavy apps.
Admin
Railway
Deploy fullstack apps cực đơn giản. Postgres, Redis, Node trong vài click.
Cal.com
Open source scheduling — tự host booking system, thay thế Calendly. Free & privacy-first.
Bình luận (0)
Đăng nhập để bình luận
Chưa có bình luận nào. Hãy là người đầu tiên!