PESolut / leaf-me

new and final mono repo for the leaf me system: Leaf-Me is a full-stack Uber Eats-style delivery platform designed for dispensaries. The app streamlines the process of placing, preparing, and delivering orders through multiple role-based interfaces
0 stars 0 forks source link

Setting up pm2 for our Node.js #9

Closed PESolut closed 1 month ago

PESolut commented 1 month ago

Setting up PM2 for our Node.js application

What is PM2?

PM2 is a production process manager for Node.js applications. It allows you to keep applications alive forever, reload them without downtime, and facilitate common system admin tasks.

Installation

install PM2 globally, run:

npm install pm2@latest -g

Basic PM2 Setup

  1. Navigate to your project directory:

    cd /path/to/your/project
  2. Start your application with PM2:

    pm2 start backend/server.js --name "my-app"

    Replace backend/server.js with the path to your main server file.

  3. Check the status of your processes:

    pm2 list
  4. Monitor your application:

    pm2 monit
  5. To stop your application:

    pm2 stop "my-app"
  6. To restart your application:

    pm2 restart "my-app"

Additional PM2 Commands

  • View logs:

    pm2 logs
  • Set up PM2 to start on system boot:

    pm2 startup

    Follow the instructions provided by this command.

  • Save the current PM2 process list:

    pm2 save

PM2 Configuration File (Optional)

For more advanced setups, you can create a ecosystem.config.js file in your project root:

javascript
module.exports = {
apps: [{
name: "my-app",
script: "./backend/server.js",
env: {
NODE_ENV: "development",
},
env_production: {
NODE_ENV: "production",
}
}]
}

then start the app using:

pm2 start ecosystem.config.js

Next Steps

  • Explore PM2's clustering capabilities for improved performance
  • Set up monitoring and alerts
  • Integrate PM2 with your deployment process

Remember to consult the official PM2 documentation for more detailed information and advanced features.

Originally posted by @PESolut in #4