如何在 aws 上的 Amazon Linux AMI 中自动启动 node.js 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11275870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I automatically start a node.js application in Amazon Linux AMI on aws?
提问by user482594
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
那么我想我可以将/sbin/chkconfig
其添加到服务中。(为了确定,它是正确的吗?)
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.
但是,我只想运行未通过yum
. 要运行 node.js 程序,我必须sudo node app.js
在系统启动时在主目录中运行脚本。
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.
我不习惯 Amazon Linux AMI,所以我在每次启动时都可以找到一种“正确”的方法来自动运行一些脚本。
Is there an elegant way to do this?
有没有一种优雅的方法来做到这一点?
采纳答案by mvbl fst
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
.
一种方法是创建一个新贵的工作。这样,你的应用程序将启动,一旦Linux的加载,如果它崩溃会自动重新启动,你就可以开始/停止通过/重启sudo start yourapp
/ sudo stop yourapp
/ sudo restart yourapp
。
Here are beginning steps:
以下是开始步骤:
1) Install upstart utility (may be pre-installed if you use a standard Amazon Linux AMI):
1) 安装 upstart 实用程序(如果您使用标准的 Amazon Linux AMI,则可能会预先安装):
sudo yum install upstart
For Ubuntu:
对于 Ubuntu:
sudo apt-get install upstart
2) Create upstart script for your node app:
2)为您的节点应用程序创建新贵脚本:
in /etc/init
add file yourappname.conf
with the following lines of code:
在/etc/init
添加文件中添加yourappname.conf
以下代码行:
#!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) start your app by sudo start yourappname
3)通过以下方式启动您的应用程序 sudo start yourappname
回答by Unknown
Quick solution for you would be to start your app from /etc/rc.local
; just add your command there.
您的快速解决方案是从以下位置启动您的应用程序/etc/rc.local
;只需在那里添加您的命令。
But if you want to go the elegant way, you'll have to package your application in a rpm file,
have a startup script that goes in /etc/rc.d
so that you can use chkconfig
on your app, then install the rpm on your instance.
但是,如果您想采用优雅的方式,则必须将您的应用程序打包到一个 rpm 文件中,将启动脚本放入其中/etc/rc.d
以便您可以chkconfig
在您的应用程序上使用,然后在您的实例上安装 rpm。
Maybe thisor thishelp. (or just google for "creating rpm packages")
回答by almypal
Have been using foreveron AWS and it does a good job. Install using
一直在使用永远在AWS上,它做得很好。安装使用
[sudo] npm install forever -g
To add an application use
添加应用程序使用
forever start path_to_application
and to stop the application use
并停止应用程序使用
forever stop path_to_application
Thisis a useful article that helped me with setting it up.
这是一篇有用的文章,帮助我进行了设置。
回答by TomG
You can create a script that can start and stop your app and place it in /etc/init.d; make the script adhere to chkconfig's conventions (below), and then use chkconfig to set it to start when other services are started.
您可以创建一个脚本来启动和停止您的应用程序并将其放置在 /etc/init.d 中;使脚本遵守 chkconfig 的约定(如下),然后使用 chkconfig 将其设置为在其他服务启动时启动。
You can pick an existing script from /etc/init.d to use as an example; this articledescribes the requirements, which are basically:
您可以从 /etc/init.d 中选择一个现有脚本作为示例;这篇文章描述的要求,基本上是:
- An executable script that identifies the shell needed (i.e., #!/bin/bash)
- A comment of the form # chkconfig: where is often 345, startprio indicates where in the order of services to start, and stopprio is where in the order of services to stop. I generally pick a similar service that already exists and use that as a guide for these values (i.e., if you have a web-related service, start at the same levels as httpd, with similar start and stop priorities).
- 标识所需 shell 的可执行脚本(即,#!/bin/bash)
- #chkconfig: where 形式的注释通常是 345,startprio 表示服务启动的顺序,stopprio 表示服务停止的顺序。我通常会选择一个已经存在的类似服务,并将其用作这些值的指南(即,如果您有一个与 Web 相关的服务,则从与 httpd 相同的级别开始,并具有类似的启动和停止优先级)。
Once your script is set up, you can use
设置脚本后,您可以使用
chkconfig --add yourscript
chkconfig yourscript on
and you should be good to go. (Some distros may require you to manually symlink to the script to /etc/init.d/rc.d, but I believe your AWS distro will do that for you when you enable the script.
你应该很高兴去。(某些发行版可能需要您手动将脚本符号链接到 /etc/init.d/rc.d,但我相信您的 AWS 发行版会在您启用脚本时为您执行此操作。
回答by arva
You can use forever-service for provisioning node script as a service and automatically starting during boots. Following commands will do the needful,
您可以使用永久服务将节点脚本配置为服务并在引导期间自动启动。以下命令将执行所需的操作,
npm install -g forever-service
forever-service install test
This will provision app.js in the current directory as a service via forever. The service will automatically restart every time system is restarted. Also when stopped it will attempt a graceful stop. This script provisions the logrotatescript as well.
这将通过永久将当前目录中的 app.js 作为服务提供。每次系统重新启动时,该服务将自动重新启动。此外,当停止时,它会尝试正常停止。此脚本也提供logrotate脚本。
Github url: https://github.com/zapty/forever-service
Github 网址:https: //github.com/zapty/forever-service
As of now forever-service supports Amazon Linux, CentOS, Redhat support for other Linux distro, Mac and Windows are in works..
截至目前,永久服务支持 Amazon Linux、CentOS、Redhat 对其他 Linux 发行版、Mac 和 Windows 的支持正在进行中。
NOTE: I am the author of forever-service.
注意:我是永远服务的作者。
回答by Toolkit
You can use screen
. Run crontab -e
and add this line:
您可以使用screen
. 运行crontab -e
并添加这一行:
@reboot screen -d -m bash -c "cd /home/user/yourapp/; node app"
回答by Tim Fulmer
Use Elastic Beanstalk :) Provides support for auto-scaling, SSL termination, blue/green deployments, etc
使用 Elastic Beanstalk :) 提供对自动扩展、SSL 终止、蓝/绿部署等的支持
回答by bvdb
My Amazon Linux instance runs on Ubuntu, and I used systemd to set it up.
我的 Amazon Linux 实例在 Ubuntu 上运行,我使用 systemd 来设置它。
First you need to create a <servicename>.service
file. (in my case cloudyleela.service
)
首先,您需要创建一个<servicename>.service
文件。(在我的情况下cloudyleela.service
)
sudo nano /lib/systemd/system/cloudyleela.service
Type the following in this file:
在此文件中键入以下内容:
[Unit]
Description=cloudy leela
Documentation=http://documentation.domain.com
After=network.target
[Service]
Type=simple
TimeoutSec=0
User=ubuntu
ExecStart=/usr/bin/node /home/ubuntu/server.js
Restart=on-failure
[Install]
WantedBy=multi-user.target
In this application the node
application is started. You will need a full path here. I configured that the application should simply restart if something goes wrong. The instances that Amazon uses have no passwords for their users by default.
在此应用程序中,node
应用程序已启动。您将需要一个完整的路径。我配置了如果出现问题,应用程序应该简单地重新启动。默认情况下,Amazon 使用的实例对其用户没有密码。
Reload the file from disk, and then you can start your service. You need to enable it to make it active as a service, which automatically launches at startup.
从磁盘重新加载文件,然后您可以启动您的服务。您需要启用它以使其作为服务处于活动状态,该服务会在启动时自动启动。
ubuntu@ip-172-31-21-195:~$ sudo systemctl daemon-reload
ubuntu@ip-172-31-21-195:~$ sudo systemctl start cloudyleela
ubuntu@ip-172-31-21-195:~$ sudo systemctl enable cloudyleela
Created symlink /etc/systemd/system/multi-user.target.wants/cloudyleela.service → /lib/systemd/system/cloudyleela.service.
ubuntu@ip-172-31-21-195:~$
A great systemd for node.js tutorial is available here.
一个很棒的用于 node.js 的 systemd 教程可以在这里找到。
If you run a webserver:
如果您运行网络服务器:
You probably will have some issues running your webserver on port 80. And the easiest solution, is actually to run your webserver on a different port (e.g. 4200) and then to redirect that port to port 80.You can accomplish this with the following command:
您可能会在端口 80 上运行您的网络服务器时遇到一些问题。最简单的解决方案,实际上是在不同的端口(例如 4200)上运行您的网络服务器,然后将该端口重定向到端口 80。您可以使用以下命令完成此操作:
sudo iptables -t nat -A PREROUTING -i -p tcp --dport 80 -j REDIRECT --to-port 4200
Unfortunately, this is not persistent, so you have to repeat it whenever your server restarts. A better approach is to also include this command in our service script:
不幸的是,这不是持久的,因此您必须在服务器重新启动时重复它。更好的方法是在我们的服务脚本中包含这个命令:
ExecStartPre
to add the port forwardingExecStopPost
to remove the port forwardingPermissionStartOnly
to do this with sudo power
ExecStartPre
添加端口转发ExecStopPost
删除端口转发PermissionStartOnly
使用 sudo 电源执行此操作
So, something like this:
所以,像这样:
[Service]
...
PermissionsStartOnly=true
ExecStartPre=/sbin/iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 4200
ExecStopPost=/sbin/iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 4200
Don't forget to reload and restart your service:
不要忘记重新加载并重新启动您的服务:
[ec2-user@ip-172-31-39-212 system]$ sudo systemctl daemon-reload
[ec2-user@ip-172-31-39-212 system]$ sudo systemctl stop zbaduk.ishida
[ec2-user@ip-172-31-39-212 system]$ sudo systemctl start zbaduk.ishida
[ec2-user@ip-172-31-39-212 system]$