How can I automatically start a node.js applicatio

2019-01-30 14:28发布

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?)

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.

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?

8条回答
相关推荐>>
2楼-- · 2019-01-30 15:00

Use Elastic Beanstalk :) Provides support for auto-scaling, SSL termination, blue/green deployments, etc

查看更多
欢心
3楼-- · 2019-01-30 15:03

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.

Here are beginning steps:

1) Install upstart utility (may be pre-installed if you use a standard Amazon Linux AMI):

sudo yum install upstart

For Ubuntu:

sudo apt-get install upstart

2) Create upstart script for your node app:

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) start your app by sudo start yourappname

查看更多
老娘就宠你
4楼-- · 2019-01-30 15:13

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.

You can pick an existing script from /etc/init.d to use as an example; this article describes the requirements, which are basically:

  • 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).

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.

查看更多
别忘想泡老子
5楼-- · 2019-01-30 15:14

My Amazon Linux instance runs on Ubuntu, and I used systemd to set it up.

First you need to create a service file. (in my case 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

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.

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:

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:

  1. ExecStartPre to add the port forwarding
  2. ExecStopPost to remove the port forwarding
  3. PermissionStartOnly to do this with sudo power

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]$
查看更多
等我变得足够好
6楼-- · 2019-01-30 15:15

Have been using forever on AWS and it does a good job. Install using

 [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

This is a useful article that helped me with setting it up.

查看更多
劳资没心,怎么记你
7楼-- · 2019-01-30 15:16

You can use screen. Run crontab -e and add this line:

@reboot  screen -d -m bash -c "cd /home/user/yourapp/; node app"
查看更多
登录 后发表回答