【发布时间】: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