Node JS 应用程序在 AWS EC2 实例上崩溃

Node JS app is crashing on AWS EC2 instance

我已使用 codedeploy 服务在 ec2 上部署应用程序,但应用程序崩溃并显示以下错误消息:

Script at specified location: scripts/start_server failed to complete in 100 seconds

下面是我的appspec.yml文件:

version: 0.0
os: linux
files:
  - source: /
    destination: /tmp/
hooks:
  AfterInstall:
     - location: scripts/install_dependencies
       timeout: 100
       runas: root
  ApplicationStart:
     - location: scripts/start_server
       timeout: 100
       runas: root

install_dependencies

cd /tmp/

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install node

npm install

start_server

cd /tmp/

node server.js

有人告诉我我做错了什么。

您的部署因此挂起 node server.js。这将启动您的应用程序,仅此而已。它永远不会完成,因此 CodeDeploy 会超时等待它完成。

解决此问题的一种方法是通过 systemd 启动您的应用。因此,您必须创建一个 systemd 单元文件。有很多这样的例子:

其他方式可能涉及 运行 您的应用使用 pm2 or forever