如何在 aws 上的 Amazon Linux AMI 中自动启动 node.js 应用程序? [英] How can I automatically start a node.js application in Amazon Linux AMI on aws?
问题描述
是否有一个简短的指南来解释如何在实例启动和运行时启动应用程序?如果它是通过 yum
安装的服务之一,那么我想我可以使用 /sbin/chkconfig
将其添加到服务中.(为了确定,它是正确的吗?)
Is there a brief guide to explain how to start up a application when the instance starts up and running? If it were one of the services installed through yum
then I guess I can use /sbin/chkconfig
to add it to the service. (To make it sure, is it correct?)
然而,我只想运行不是通过yum
安装的程序.要运行 node.js 程序,我必须在系统启动时在主目录中运行脚本 sudo node app.js
.
However, I just want to run the program which was not installed through yum
. To run node.js program, I will have to run script sudo node app.js
at home directory whenever the system boots up.
我不习惯 Amazon Linux AMI,所以我在找到一种正确"的方式来在每次启动时自动运行某些脚本时没有遇到什么困难.
I am not used to Amazon Linux AMI so I am having little trouble finding a 'right' way to run some script automatically on every boot.
有没有一种优雅的方法来做到这一点?
Is there an elegant way to do this?
推荐答案
一种方法是创建一个新贵的工作.这样你的应用程序会在 Linux 加载后启动,如果崩溃会自动重启,你可以通过 sudo start yourapp
/sudo stop yourapp
/<代码>sudo 重启你的应用.
One way is to create an upstart job. That way your app will start once Linux loads, will restart automatically if it crashes, and you can start / stop / restart it by sudo start yourapp
/ sudo stop yourapp
/ sudo restart yourapp
.
以下是开始步骤:
1) 安装 upstart 实用程序(如果您使用标准的 Amazon Linux AMI,可能会预先安装):
1) Install upstart utility (may be pre-installed if you use a standard Amazon Linux AMI):
sudo yum install upstart
对于 Ubuntu:
sudo apt-get install upstart
2) 为您的节点应用程序创建新贵脚本:
2) Create upstart script for your node app:
在 /etc/init
中添加文件 yourappname.conf
,代码如下:
in /etc/init
add file yourappname.conf
with the following lines of code:
#!upstart
description "your app name"
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
env NODE_ENV=development
# Warning: this runs node as root user, which is a security risk
# in many scenarios, but upstart-ing a process as a non-root user
# is outside the scope of this question
exec node /path_to_your_app/app.js >> /var/log/yourappname.log 2>&1
3) 通过 sudo start yourappname
这篇关于如何在 aws 上的 Amazon Linux AMI 中自动启动 node.js 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!