【问题标题】:Deploy NodeJS app on Azure App service using PM2使用 PM2 在 Azure 应用服务上部署 NodeJS 应用
【发布时间】:2022-01-02 07:12:48
【问题描述】:

我正在尝试将一个简单的 NodeJS 应用程序部署到 Azure 应用程序服务,我希望该应用程序使用 PM2 运行,并且可以选择使用 PM2 配置多个进程

我尝试了几个选项:

选项 1: 使用 package.json 中的 "start": "node index.js",该应用会处理请求,但不会在 PM2 上运行。当我在应用服务 ssh 控制台中运行 PM2 list 命令时,没有任何进程在运行。

虽然Azure docs表示当index.js文件在根目录下时,容器会自动用PM2启动app,但这并没有发生。

选项 2: 使用 package.json 中的 "start": "pm2 start --no-daemon index.js",应用程序使用 PM2 启动,只有 1 个进程在运行(通过在应用程序服务 ssh 控制台中运行 PM2 list 命令确认这一点)。

如何使用 pm2 运行多个进程?在 Azure 应用服务上

选项 3: 为了运行多个进程,除了 package.json 中的"start": "node index.js",我还使用pm2 init 创建了一个生态系统.config 文件。我收到此错误

错误 - 容器测试未响应端口:8080 上的 HTTP ping, 网站启动失败

ecosystem.config 文件:

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);

【问题讨论】:

    标签: node.js azure express azure-web-app-service


    【解决方案1】:

    错误 - 容器测试没有响应端口:8080 上的 HTTP ping,站点启动失败

    • 这是Azure Web App的一个限制,它只支持暴露80和443端口。所以当你想暴露9000和8080等其他端口时,你会得到它没有响应的错误该端口上的 HTTP ping。

    将 index.js 文件中的代码改成如下:

    const port = process.env.port || 8080;
    

    并重新部署到 Azure。

    • 如果您的容器需要很长时间才能启动,您可以增加启动时间限制。启动容器的默认时间是 230 秒。
    • 要进行配置,请添加一个名为 WEBSITES_CONTAINER_START_TIME_LIMIT 的应用设置,并将其设置为您希望我们等待容器启动的秒数(最多最多 1800),如下图所示:

    请确保您的容器必须响应 HTTP ping 并且为了让我们认为容器成功启动,容器必须启动并且它必须响应 HTTP ping。如果容器启动但没有响应 ping,我们最终会在 Docker 日志中记录一个事件,说明它没有启动。

    解决这个问题。

    • 使用 Dockerfile 中的 EXPOSE 指令公开端口 3000。

    • 使用值为“3000”的 WEBSITES_PORT 应用设置来公开该端口。

    更多详情请参考Your container must respond to an HTTP ping、this、HTTP pings on port: 8080和SO thread。

    【讨论】:

      猜你喜欢
      • 2021-06-30
      • 2022-01-25
      • 2019-08-14
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多