Plesk上的Node app-自定义脚本上的环境变量

问题描述

我正在Plesk Obsidian v18.0.29上运行Node v12应用程序

我在专用部分中设置了所需的环境变量:

Plesk configuration

理论上,“应用程序模式”也应充当NODE_ENV的角色,并且似乎可以这样做。 仅供参考,我也尝试将其添加到“自定义环境变量”中,但这不能解决我的问题。

问题是,尽管应用程序本身运行良好,但是当我尝试运行自定义脚本(数据库迁移)时,Plesk似乎忽略了这些变量,正如您从使用环境“开发”中看到的那样。

em>
> REDACTED@0.1.0 migrate /var/www/vhosts/REDACTED/httpdocs
> sequelize db:migrate


[4mSequelize CLI [Node: 12.4.0,CLI: 5.4.0,ORM: 5.8.7][24m

Loaded configuration file "config/database.js".
Using environment "development".


[31mERROR:[39m Error parsing url: undefined
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! REDACTED@0.1.0 migrate: `sequelize db:migrate`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the REDACTED@0.1.0 migrate script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /var/www/vhosts/REDACTED/.npm/_logs/2020-09-04T08_13_39_221Z-debug.log

如果我尝试创建一个强制使用NODE_ENV变量的自定义脚本,则会考虑正确的环境,但其他变量仍将被忽略。 例如,通过创建一个名为migrateprod的脚本并通过Plesk运行它:

"scripts": {
  "test": "npm run test:unit && npm run test:e2e","test:unit": "mocha tests/unit/**/**.spec.js --require ./tests/setup.js --exit","test:e2e": "mocha tests/e2e/**/**.spec.js --require ./tests/setup.js --exit","start": "node index.js","watch": "nodemon index.js","migrate": "sequelize db:migrate","migrateprod": "NODE_ENV=production sequelize db:migrate","migrate:down": "sequelize db:migrate:undo:all","cli": "node cli/index.js"
},

我现在可以看到环境的产生,但是找不到DATABASE_URL变量,而是出现了错误:

> REDACTED@0.1.0 migrateprod /var/www/vhosts/REDACTED/httpdocs
> NODE_ENV=production sequelize db:migrate

[4mSequelize CLI [Node: 12.4.0,ORM: 5.8.7][24m

Loaded configuration file "config/database.js".
Using environment "production".


[31mERROR:[39m Error parsing url: undefined
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! REDACTED@0.1.0 migrateprod: `NODE_ENV=production sequelize db:migrate`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the REDACTED@0.1.0 migrateprod script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /var/www/vhosts/REDACTED/.npm/_logs/2020-09-04T08_21_51_188Z-debug.log

最重要的是,Plesk在运行自定义脚本时似乎忽略或避免设置环境变量。我尝试四处搜寻,但找不到类似的情况,这让我觉得我只是想念一些东西。

有什么想法吗?预先感谢您的宝贵时间!

解决方法

结果表明,无论您多么努力,Plesk都不会将环境变量转发到自定义脚本。因此,我修改了package.json以在运行脚本之前即时设置变量。

"scripts": {
  ... other scripts ...
  "cli": "export $(cat ../.env.production | xargs) && node cli/index.js"
},

在应用程序文件系统外部的受保护文件夹中有一个.env文件,我从中获取变量并将其作为args发送到自定义脚本。

这不是最优雅的解决方案,但可以解决问题。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...