Node.js applications need a process manager to run reliably in production. PM2 is the industry-standard process manager for Node.js, providing automatic restarts, load balancing, log management, and zero-downtime deployments. This guide covers everything from installing Node.js to deploying your application behind Nginx on your SakuraHost VPS.

Prerequisites: A SakuraHost VPS with Ubuntu 22.04+, a non-root user with sudo access, and Nginx installed. See our guides: Getting Started with Your SakuraHost VPS and Installing and Configuring Nginx on Your SakuraHost VPS.

1. Installing Node.js with NVM

NVM (Node Version Manager) allows you to install and switch between multiple Node.js versions, which is essential for managing different projects.

Install NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash source ~/.bashrc
Install the latest LTS version of Node.js:
nvm install --lts nvm use --lts node --version npm --version

As of 2026, Node.js 22.x is the current LTS release. NVM installs Node.js in your home directory, keeping system directories clean and avoiding permission issues with global packages.

2. Preparing Your Application

Clone Your Application Repository

cd /var/www sudo mkdir myapp sudo chown $USER:$USER myapp git clone https://github.com/yourusername/your-app.git myapp cd myapp npm install --production

Environment Variables

Create a .env file for your application configuration. Never commit this file to version control.

nano .env
NODE_ENV=production PORT=3000 DATABASE_URL=mysql://user:password@localhost:3306/mydb SESSION_SECRET=your-secure-random-string
Security Note: Ensure your .env file has restricted permissions: chmod 600 .env. Never expose database credentials or API keys in your repository.

3. Installing and Configuring PM2

Install PM2 globally:
npm install -g pm2
Start your application with PM2:
cd /var/www/myapp pm2 start app.js --name "myapp" --env production

For applications using npm start, use:

pm2 start npm --name "myapp" -- start

PM2 Ecosystem Configuration File

For complex deployments, create an ecosystem file that defines all your application settings:

nano ecosystem.config.js
module.exports = { apps: [{ name: 'myapp', script: './app.js', instances: 'max', exec_mode: 'cluster', env_production: { NODE_ENV: 'production', PORT: 3000 }, max_memory_restart: '300M', log_date_format: 'YYYY-MM-DD HH:mm:ss', error_file: '/var/log/pm2/myapp-error.log', out_file: '/var/log/pm2/myapp-out.log', merge_logs: true, autorestart: true, watch: false, max_restarts: 10, restart_delay: 4000 }] };

Start with the ecosystem file:

pm2 start ecosystem.config.js --env production

The cluster mode with instances: 'max' creates one worker process per CPU core, dramatically improving throughput. For a 4-core SakuraHost VPS, this means 4 parallel Node.js processes sharing the workload.

4. PM2 Process Management

# View running processes pm2 list # Monitor CPU and memory in real-time pm2 monit # View application logs pm2 logs myapp pm2 logs myapp --lines 100 # Restart, stop, or delete a process pm2 restart myapp pm2 stop myapp pm2 delete myapp # Zero-downtime reload (cluster mode only) pm2 reload myapp

5. Auto-Start on Server Reboot

Generate and configure the startup script:
pm2 startup systemd

PM2 will output a command prefixed with sudo - copy and run it exactly as shown.

Save the current process list:
pm2 save

Now your application will automatically restart whenever the server reboots. Run pm2 save again whenever you add or remove managed applications.

6. Nginx Reverse Proxy Configuration

Configure Nginx to forward incoming web traffic to your Node.js application:

sudo nano /etc/nginx/sites-available/myapp
server { listen 80; server_name myapp.example.com; location / { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_cache_bypass $http_upgrade; proxy_read_timeout 90; } }
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx

7. Deployment Workflow

For updating your application with zero downtime:

cd /var/www/myapp git pull origin main npm install --production pm2 reload myapp
Resources: Consult the PM2 Official Documentation for advanced features like deployment workflows and the PM2 Plus monitoring dashboard. For SSL setup, see our guide How to Set Up Let's Encrypt SSL on Your VPS. Need help? Contact SakuraHost Support.
Was this answer helpful? 0 Users Found This Useful (0 Votes)