Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PM2: Deploy multiple environments on a single server?

Tags:

pm2

I am using PM2 for deployment / process management, and the application handles lots of DNS tasks, and so it's easiest if I run the development app from the remote server, and either Rsyncing or SFTPing on save (still sorting this out).

This being the case, it is the idea case for the dev app to be on the same VM as the production app. However, the structure of the PM2 deployment configuration file (ecosystem.config.js) doesn't seem to make this possible, as when I run pm2 deploy development, the development version overtakes the production process on the VM.

Here is what I have:

module.exports = {
  apps: [
    {
      name: "APP NAME",
      script: "app.js",
      env_development: {
        NODE_ENV: "development",
        ...
      },
      env_production: {
        NODE_ENV: "production",
        ...
      }
    }
  ],
  deploy: {
    production: {
      user: "user",
      host: ["123.123.123.123"],
      ref: "origin/master",
      repo: "[email protected]:me/repo.git",
      path: "/var/www/app",
      "post-deploy":
        "npm install && pm2 reload ecosystem.config.js --env production"
    },
    development: {
      user: "user",
      host: ["123.123.123.123"],
      ref: "origin/master",
      repo: "[email protected]:me/repo.git",
      path: "/var/www/app-dev",
      "post-deploy":
        "npm install && pm2 reload ecosystem.config.js --env development"
    }
  }
};

Any thoughts for the best way to go about accomplishing this?

like image 917
PaulIsLoud Avatar asked May 13 '26 04:05

PaulIsLoud


1 Answers

After referencing this PR, I'm thinking you should be able to add append_env_to_name: true as a property to the object in the apps array of the ecosystem.config.js:

So your updated ecosystem.config.js file would be as follows:

module.exports = {
  apps: [
    {
      name: "APP NAME",
      append_env_to_name: true // <===== add this line
      script: "app.js",
      env_development: {
        NODE_ENV: "development",
        ...
      },
      env_production: {
        NODE_ENV: "production",
        ...
      }
    }
  ],
  deploy: {
    production: {
      user: "user",
      host: ["123.123.123.123"],
      ref: "origin/master",
      repo: "[email protected]:me/repo.git",
      path: "/var/www/app",
      "post-deploy":
        "npm install && pm2 reload ecosystem.config.js --env production"
    },
    development: {
      user: "user",
      host: ["123.123.123.123"],
      ref: "origin/master",
      repo: "[email protected]:me/repo.git",
      path: "/var/www/app-dev",
      "post-deploy":
        "npm install && pm2 reload ecosystem.config.js --env development"
    }
  }
};
like image 141
Robert Shaw Avatar answered May 19 '26 05:05

Robert Shaw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!