How do I run multiple processes using pm2 ? on Azure App Service

Pravishanth 1 Reputation point
2022-01-03T05:59:29.22+00:00

I am trying to deploy a simple NodeJS app to Azure App Service, I would like the app to run using PM2 with the option to configure multiple processes using PM2

I tried few options:

Option 1: with "start": "node index.js" in package.json, the app serves requests but does not run on PM2. No processes were running when I run PM2 list command in app service ssh console.

Although Azure docs indicate that the container automatically starts the app with PM2 when index.js file is in the root folder, this did not happen.

Option 2: with "start": "pm2 start --no-daemon index.js" in package.json, the app was started with PM2, only 1 process was running (confirmed this by running PM2 list command in app service ssh console).

How do I run multiple processes using pm2 ? on Azure App Service

Option 3: With the intention of running multiple processes, In addition to "start": "node index.js" in package.json, I also created a ecosystem.config file using pm2 init. I get this error

ERROR - Container test didn't respond to HTTP pings on port: 8080, failing site start

ecosystem.config file:

module.exports = {
  apps : [{
    script: 'index.js',
    watch: '.'
  }, {
    script: './service-worker/',
    watch: ['./service-worker']
  }],

  deploy : {
    production : {
      user : 'SSH_USERNAME',
      host : 'SSH_HOSTMACHINE',
      ref  : 'origin/master',
      repo : 'GIT_REPOSITORY',
      path : 'DESTINATION_PATH',
      'pre-deploy-local': '',
      'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production',
      'pre-setup': ''
    }
  }
}; 

Package.JSON

{
  "name": "app-service-hello-world",
  "description": "Simple Hello World Node.js sample for Azure App Service",
  "version": "0.0.1",
  "private": true,
  "license": "MIT",
  "author": "Microsoft",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "dotenv": "^10.0.0"
  }
}

Index.js

const http = require("http");

const server = http.createServer((request, response) => {
  response.writeHead(200, { "Content-Type": "text/plain" });
  response.end("Hello World!");
});

const port = 8080;
server.listen(port);
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,933 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 30,281 Reputation points Microsoft Employee Moderator
    2022-01-05T21:27:55.657+00:00

    Hi @Pravishanth ,

    Set pm2 start index.js --no-daemon as the startup command for your app service. PM2 doesn't automatically start with runtimes starting with node 14.

    Starting from Node 14 LTS, the container doesn't automatically start your app with PM2. To start your app with PM2, set the startup command to pm2 start <.js-file-or-PM2-file> --no-daemon. Be sure to use the --no-daemon argument because PM2 needs to run in the foreground for the container to work properly.

    I've attached 162683-2022-01-05-lw1sdlwk0003un-default-docker.log for illustration of what you should see when PM2 is running.

    162682-image.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.