Run 2 Nodejs app with PM2 on Azure Linux App Service

1k views Asked by At

I'm attempting to run two NodeJS applications on a single Linux Azure App Service. I start the PM2 with the ecosystem configuration shown below.

App1's domain name is app1.example.com.

App2's domain name is app2.example.com.

 I see that APP1 is working properly, but APP2 is not. When I swap the configuration (APP2, APP1 in ecosystem.config.js), APP2 works but not APP1. As a result, I can only run one app.

When I attempt to debug APP2, I discover that the request app2.example.com is not reaching the application.

What is wrong with this approach, and can you recommend a better solution?

ecosystem.config.js
      module.exports = {
        apps: [
            {
                script: './app1/index.js',
                name: 'app1',
                watch: true,
                instances: '1', // To set 0/max for spread the app across all CPUs
                env: {
                    "NODE_ENV": "production",
                },
            },
            {
                script: './app2/index.js',
                name: 'app2',
                watch: true,
                instances: '1', // To set 0/max for spread the app across all CPUs
                env: {
                    "NODE_ENV": "production",
                },
            }
        ]
    };
1

There are 1 answers

3
Harshitha On

Check the below steps to run the NodeJS applications with PM2 in a single Azure App service.

Create a new Folder and within it create two separate Node JS Applications.

Follow this MSDoc to create Node JS Application.

enter image description here

Make sure both the Applications have package.json files.

  • Open the Applications in VSCode.
  • In each application folder install the PM2 with the below command separately in each application root folder. For App1 in myExpressApp1 root folder terminal. For App 2 in myExpressApp2 root folder terminal.

OR

You can run the PM2 globally on the applications root directory.

npm install pm2 -g

Start both the Applications using PM2 as mentioned in the azureossd.

pm2 start app.js --name="app1" --no-daemon
pm2 start app.js --name="app2" --no-daemon

enter image description here

  • In Azure Portal, Create a Linux App Service with run time stack Node.
  • From Deployment Center, deploy the code using FTP or Git.

enter image description here

As you are using ecosystem configuration add pm2 start ecosystem.config.js in the startup command.

Make sure your ecosystem.config.js is deployed in the Application root directory.

I start the PM2 with the ecosystem configuration shown below. App1's domain name is app1.example.com. App2's domain name is app2.example.com.

If you have not configured the custom domains then, access your app's using the below URL`s.

https://YourWebApp.azurewebsites.net/app1
https://YourWebApp.azurewebsites.net/app2

AFAIK, the problem can be with configuration of the custom domains. Make sure you have custom domains configured correctly.

In ecosystem.config.js, add the PORT No. below the NODE_ENV.

env: { 
    "NODE_ENV": "production", 
    "PORT": 5000  
    },
env: { 
    "NODE_ENV": "production", 
    "PORT": 3000    
    },