General
Deployment
Railway
Learn how to deploy your application to Railway.
Railway is a modern platform that makes it easy to deploy full-stack applications. It offers a generous free tier and seamless integration with GitHub.
Why Railway?
- Simple deployment - Connect your GitHub repo and deploy in minutes
- Free tier available - Great for testing and MVPs
- Automatic HTTPS - SSL certificates handled automatically
- Database included - Can provision PostgreSQL directly
- Environment variables - Easy management through the dashboard
Deploying to Railway
1. Create Railway Account
Sign up for a free account at railway.app.
2. Create New Project
- Click "New Project" in the Railway dashboard
- Select "Deploy from GitHub repo"
- Connect your GitHub account if prompted
- Select your repository
3. Configure Build Settings
Railway will auto-detect Next.js, but you can verify these settings:
- Build Command:
npm run build - Start Command:
npm start - Root Directory:
/(root of your project)
4. Add Environment Variables
Add all required environment variables in the Railway dashboard:
- Go to your project → Variables tab
- Add variables from your
.env:
Railway Environment Variables
DATABASE_URL=postgresql://...
BETTER_AUTH_SECRET=...
NEXT_PUBLIC_SITE_URL=https://your-app.railway.app
STRIPE_SECRET_KEY=...
STRIPE_PUBLISHABLE_KEY=...
RESEND_API_KEY=...
EMAIL_FROM=...5. Provision Database (Optional)
Railway can provision a PostgreSQL database for you:
- Click "New" → "Database" → "Add PostgreSQL"
- Railway will automatically set the
DATABASE_URLenvironment variable - Run migrations after the first deployment
6. Deploy
Railway will automatically:
- Install dependencies
- Build your application
- Deploy to their infrastructure
Your app will be available at https://your-app.railway.app.
Database Migrations
To run migrations on Railway:
-
Option 1: Add to build command
- Update build command:
npm run build && npm run db:migrate
- Update build command:
-
Option 2: Use Railway CLI
Terminalrailway run npm run db:migrate -
Option 3: Run after deployment
- Connect via Railway CLI and run migrations manually
Custom Domain
To use a custom domain:
- Go to Settings → Domains
- Click "Add Domain"
- Follow the DNS configuration instructions
- Update
NEXT_PUBLIC_SITE_URLto your custom domain
Environment-Specific Variables
Railway supports environment-specific variables:
- Production - Used for production deployments
- Preview - Used for preview deployments (from PRs)
- Development - Used for local development with Railway CLI
Monitoring
Railway provides:
- Logs - View real-time application logs
- Metrics - CPU, memory, and network usage
- Deployments - View deployment history
Troubleshooting
Build Failures
- Check build logs in the Railway dashboard
- Ensure all dependencies are in
package.json - Verify Node.js version compatibility
Database Connection Issues
- Verify
DATABASE_URLis set correctly - Check if database requires SSL (add
?sslmode=require) - Ensure database is accessible from Railway's IPs
Environment Variables Not Loading
- Verify variables are set in the correct environment
- Check for typos in variable names
- Redeploy after adding new variables