How can I automatically start a node.js application in Amazon Linux AMI on aws?

前端 未结 8 950
轻奢々
轻奢々 2020-12-07 16:44

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 th

相关标签:
8条回答
  • 2020-12-07 17:08

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

    0 讨论(0)
  • 2020-12-07 17:09

    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

    0 讨论(0)
  • 2020-12-07 17: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.

    0 讨论(0)
  • 2020-12-07 17: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.

    0 讨论(0)
  • 2020-12-07 17:23

    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 logrotate script as well.

    Github url: 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..

    NOTE: I am the author of forever-service.

    0 讨论(0)
  • 2020-12-07 17:25

    Quick solution for you would be to start your app from /etc/rc.local ; just add your command there.

    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.

    Maybe this or this help. (or just google for "creating rpm packages")

    0 讨论(0)
提交回复
热议问题