Deploy Node app on AWS EC2 Amazon Linux 2


This video is part of the following playlists:


Deploy Node app on AWS EC2 Amazon Linux 2 by installing node js and setting the app up as a systemd service

Chapters:

  • 0:00 Intro
  • 0:14 Setting up an EC2 Instance
  • 2:31 Installing Node
  • 3:21 git clone code onto the instance
  • 4:28 Running the node app
  • 6:13 Environment Variables
  • 9:38 Systemd service
  • 14:13 Summary

Code

https://github.com/sam-meech-ward-bcit/lotr

Node

sudo yum install -y gcc-c++ make
curl -sL https://rpm.nodesource.com/setup_16.x | sudo -E bash -
sudo yum install -y nodejs

Git

sudo yum install -y git

systemd service

sudo vim /etc/systemd/system/YourAppName.service
[Unit]
Description=YourAppName
After=multi-user.target

[Service]
ExecStart=/usr/bin/node /home/ec2-user/your_app_dir/server.js
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=YourAppName
User=ec2-user
EnvironmentFile=/home/ec2-user/your_app_dir/app.env

[Install]
WantedBy=multi-user.target

Note

Remember to put the environment variables in /home/ec2-user/your_app_dir/app.env And make sure each env var is put on a new line.

sudo systemctl enable YourAppName.service
sudo systemctl start YourAppName.service

nginx reverse proxy (optional)

sudo amazon-linux-extras install nginx1 -y
sudo systemctl enable nginx
sudo systemctl start nginx
sudo vim /etc/nginx/nginx.conf
    location / {
          proxy_pass http://localhost:8080;
    }
sudo systemctl restart nginx

Find an issue with this page? Fix it on GitHub