Deployment
PM2 Deployment
Deploy to your own server using PM2 Node.js process manager.
Introduction
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to:
- Keep applications alive forever
- Reload them without downtime
- Facilitate common system admin tasks
Prerequisites
- Node.js installed
- PM2 installed globally (
npm install -g pm2
) - Domain name configured
- SSL certificates ready
Installation
Install PM2 globally:
npm install -g pm2
Configuration
Create ecosystem.config.js:
module.exports = {
apps: [
{
name: "saas-fast",
script: ".output/server/index.mjs",
instances: "max",
exec_mode: "cluster",
env: {
NODE_ENV: "production",
PORT: 3000,
},
},
],
};
Deployment Steps
- Build the application:
npm run build
- Start with PM2:
pm2 start ecosystem.config.js
- Save PM2 process list:
pm2 save
- Setup startup script:
pm2 startup
Common Commands
# List all processes
pm2 list
# Monitor all processes
pm2 monit
# View logs
pm2 logs
# Restart application
pm2 restart saas-fast
# Stop application
pm2 stop saas-fast
# Delete from PM2
pm2 delete saas-fast
Environment Variables
Create a .env file in your project root:
NODE_ENV=production
DATABASE_URL=postgresql://user:pass@localhost:5432/mydb
NUXT_PUBLIC_SITE_URL=https://your-domain.com
Monitoring
PM2 provides built-in monitoring:
pm2 monit
You can also use:
- PM2 Plus (web dashboard)
- PM2's API
- Custom monitoring solutions
Logs
View logs in real-time:
pm2 logs saas-fast
Rotate logs:
pm2 install pm2-logrotate
Security
- Run as non-root user
- Set proper file permissions
- Use environment variables for secrets
- Keep PM2 and Node.js updated
- Regular security audits
end