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

前端 未结 8 978
轻奢々
轻奢々 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: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

提交回复
热议问题