0

I am trying to use PM2 on Azure App Service Linux running a simple Node.js/Express application. The configuration file for PM2 is like this:

module.exports = {
  apps: [
    {
      name: "myapi",
      script: "./src/index.js",
      env: {
        NODE_ENV: "development",
        PORT: 3000,
      },
      env_test: {
        NODE_ENV: "test",
        PORT: 8081,
      },
      env_production: {
        NODE_ENV: "production",
        PORT: 8080,
      }
    }
  ]
};

The startup command is like

pm2-runtime start ecosystem.config.js --env test

The deployment is fine. But I can only use PORT = 8080. If I use port 8081, I got the 504 gateway timeout error.

What might be the problem?

5
  • The most likely issue is that WEBSITE_PORT has been set to 8080. You need to update that or use EXPOSE on the container to open other ports. Commented Oct 24, 2024 at 15:03
  • I checked my Azure website environment varibles, I didn't see WEBSITE_PORT or PORT settings. I saw in the log stream that docker seems to start like "docker run -d -p 4858:8080 --name my-api-test_0_100db2d2 ...", even if I set the port in ecosystem.config.js to 8081. Maybe there is docker configuration file somewhere that I don't know. But I didn't use Docker directly. Commented Oct 24, 2024 at 18:23
  • @user3616544 Try to set Port value process.env.PORT || 8080 in your configuration file. Commented Oct 25, 2024 at 2:39
  • @user3616544 If possible, share your GitHub repo? Commented Oct 25, 2024 at 2:39
  • Please check if the solution below helped you. Let me know if there's anything else I can assist with. Commented Nov 12, 2024 at 9:04

1 Answer 1

0

By default, Azure Web Apps only allow external access on ports 80 (HTTP) and 443 (HTTPS). On Azure Linux Web Apps, containers typically run on port 8080.

It is not possible to expose multiple ports for external access in an Azure Web App.

I created a sample Node.js Express app with PM2 and deployed it to Azure App Service (Linux).

The app should be configured to listen on the port specified by process.env.PORT for compatibility with Azure’s port routing.

Below is complete code of nodejs app.

ecosystem.config.js:

module.exports = {
    apps: [
      {
        name: "myapi",
        script: "./src/index.js",
        env: {
          NODE_ENV: "development",
          PORT: 3000, 
        },
        env_test: {
          NODE_ENV: "test",
          PORT: process.env.PORT || 8080, 
        },
        env_production: {
          NODE_ENV: "production",
          PORT: process.env.PORT || 8080, 
        }
      }
    ]
  }; 

index.js:

const express = require('express');
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World!');
});
const port = process.env.PORT || 8080;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

I used the below start script in package.json :

"scripts": {
"start": "pm2-runtime start ecosystem.config.js"
},

I set the Startup Command as shown below.

enter image description here

enter image description here

Azure Output :

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Please check if the solution below helped you. Let me know if there's anything else I can assist with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.