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

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

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

    @reboot  screen -d -m bash -c "cd /home/user/yourapp/; node app"
    
    0 讨论(0)
  • 2020-12-07 17:31

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

    First you need to create a <servicename>.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
    

    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.

    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 cloudyleela
    [ec2-user@ip-172-31-39-212 system]$ sudo systemctl start cloudyleela
    [ec2-user@ip-172-31-39-212 system]$
    
    0 讨论(0)
提交回复
热议问题