如何使用 Shell 脚本在 Apache 上自动运行 Node.js 服务器

如何使用 Shell 脚本在 Apache 上自动运行 Node.js 服务器

我通常通过 Putty 访问我的应用程序,并按照下面所示操作让我的 Node 脚本在后台运行:

screen
cd /var/www/node
node app.js

我正在使用 Ubuntu,并且已经创建了我的实例的 AMI,现在我打算在我的 Auto Scaling 配置中使用它,但是,我应该如何在 Shell 脚本中执行这个启动配置,以便当我创建新实例时它会自动运行我的 Node 应用程序?

我唯一知道的是我应该从开始#!/bin/bash,但我不知道如何以正确的方式写出其余部分。

#!/bin/bash
screen
cd /var/www/node
node app.js

如果我按照上面展示的方式使用它,它会正常工作吗?还是我应该以其他方式执行?如果是这样,你能举个例子吗?

更新: 下列的迈克尔·汉普顿评论,我发现这个答案。所以我的代码如下所示:

[Unit]
Description=To run Node

[Service]
ExecStart=/var/www/node/app.js
Restart=always
User=nobody
Group=nobody
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/node

[Install]
WantedBy=multi-user.target

但是,当尝试运行该服务时,systemctl start app它会要求进行身份验证:

==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to start 'app.service'.
Authenticating as: Ubuntu (ubuntu)
Password:

但是,该用户没有密码(据我所知),因此我只需按回车键,就会出现以下错误:

==== AUTHENTICATION FAILED ===
Failed to start app.service: Access denied

因此,我尝试了sudo systemctl start app,但好像什么都没发生。我该如何运行这项服务?

运行 后sudo systemctl daemon-reloadsudo systemctl start appsudo systemctl status app,返回:

● app.service - App
   Loaded: loaded (/etc/systemd/system/app.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: Stopped App.
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: Started App.
Dec 10 01:15:47 ip-172-31-12-252 systemd[7645]: app.service: Failed at step GROUP spawning /var/www/node/app.js: No such process
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: app.service: Main process exited, code=exited, status=216/GROUP
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: app.service: Unit entered failed state.
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: app.service: Failed with result 'exit-code'.
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: app.service: Service hold-off time over, scheduling restart.
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: Stopped App.
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: app.service: Start request repeated too quickly.
Dec 10 01:15:47 ip-172-31-12-252 systemd[1]: Failed to start App.

更新²:我已将其更改app.service为(删除了Group指令并添加了node运行):

[Unit]
Description=App

[Service]
ExecStart=/usr/bin/nodejs /var/www/node/app.js
Restart=always
User=nobody
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/node

[Install]
WantedBy=multi-user.target

但是,当我访问状态时,显示:

● app.service - App
   Loaded: loaded (/etc/systemd/system/app.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

Dec 10 02:13:37 ip-172-31-12-252 nodejs[8084]:     at Function.Module.runMain (module.js:442:10)
Dec 10 02:13:37 ip-172-31-12-252 nodejs[8084]:     at startup (node.js:136:18)
Dec 10 02:13:37 ip-172-31-12-252 nodejs[8084]:     at node.js:966:3
Dec 10 02:13:37 ip-172-31-12-252 systemd[1]: app.service: Main process exited, code=exited, status=1/FAILURE
Dec 10 02:13:37 ip-172-31-12-252 systemd[1]: app.service: Unit entered failed state.
Dec 10 02:13:37 ip-172-31-12-252 systemd[1]: app.service: Failed with result 'exit-code'.
Dec 10 02:13:37 ip-172-31-12-252 systemd[1]: app.service: Service hold-off time over, scheduling restart.
Dec 10 02:13:37 ip-172-31-12-252 systemd[1]: Stopped App.
Dec 10 02:13:37 ip-172-31-12-252 systemd[1]: app.service: Start request repeated too quickly.
Dec 10 02:13:37 ip-172-31-12-252 systemd[1]: Failed to start App.

答案1

您的脚本将阻止执行,因此至少您应该将 NodeJS 进程发送到后台:

#!/bin/bash
cd /var/www/node
nohup node app.js >/dev/null 2>&1 &

只要您的 AMI 已经安装,它就可以nohup工作node

Bash 脚本只能带你走这么远。如果你真的想在 AWS 上部署,我建议你学习cloud-initChef/OpsWorks。

http://cloudinit.readthedocs.io/

https://aws.amazon.com/opsworks/

相关内容