在DigitalOcean App Platform上使用PM2部署Node应用失败咨询
在DigitalOcean App Platform使用PM2部署Node应用失败的问题及解决
问题描述
我在DigitalOcean App Platform上托管了Node应用,使用node index.js启动应用无任何问题,但尝试通过pm2 start index.js启动时,部署失败。目前未找到相关说明,请问是否可以在该平台使用PM2?
部署日志
[2023-02-28 07:03:29] > pm2 start index.js [2023-02-28 07:03:30] [PM2] Spawning PM2 daemon with pm2_home=/workspace/.pm2 [2023-02-28 07:03:30] [PM2] PM2 Successfully daemonized [2023-02-28 07:03:30] [PM2] Starting /workspace/index.js in fork_mode (1 instance) [2023-02-28 07:03:31] [PM2] Done. [2023-02-28 07:03:31] ┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐ [2023-02-28 07:03:31] │ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │ [2023-02-28 07:03:31] ├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤ [2023-02-28 07:03:31] │ 0 │ index │ default │ 1.0.0 │ fork │ 39 │ 0s │ 0 │ online │ 0% │ 0b │ apps │ enabled │ [2023-02-28 07:03:31] └─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘ [2023-02-28 07:03:31] npm notice [2023-02-28 07:03:31] npm notice New major version of npm available! 8.19.3 -> 9.5.1 [2023-02-28 07:03:31] npm notice Changelog: <https://github.com/npm/cli/releases/tag/v9.5.1> [2023-02-28 07:03:31] npm notice Run `npm install -g npm@9.5.1` to update! [2023-02-28 07:03:31] npm notice
错误信息
Error message: Component Issues app_name failed to deploy
解决方案
DigitalOcean App Platform不推荐直接使用pm2 start作为启动命令,核心原因是:
- App Platform会监控前台运行的进程,
pm2 start会将Node进程转为后台守护进程,启动命令执行完成后PM2会退出前台,App Platform会认为没有活跃的前台进程,从而终止应用,导致部署失败。 - 平台本身已内置进程守护、自动重启、负载均衡等PM2常用功能,无需额外用PM2管理进程。
如果确实需要使用PM2的特定功能(如多实例配置、日志管理),可以通过以下方式适配:
- 安装PM2到项目依赖:执行
npm install pm2 --save,确保pm2出现在package.json的dependencies中。 - 使用PM2的容器专用运行时
pm2-runtime启动应用,它会保持进程在前台运行,让App Platform能正确监控状态。- 直接设置启动命令为
npx pm2-runtime start index.js; - 或者在
package.json的scripts中添加:"start": "pm2-runtime start index.js",然后用npm start作为启动命令。
- 直接设置启动命令为
内容的提问来源于stack exchange,提问作者idris
相关产品推荐
相关产品推荐

