Skip to content

Instantly share code, notes, and snippets.

@cgustav
Last active July 14, 2024 09:07
Show Gist options
  • Save cgustav/b67d5bbc4d6894efd7aa30cf06a59a51 to your computer and use it in GitHub Desktop.
Save cgustav/b67d5bbc4d6894efd7aa30cf06a59a51 to your computer and use it in GitHub Desktop.
Amazon-Linux 2 Node User-Data
#!/bin/bash
# Update the system
yum update -y
# install NVM
su - ec2-user -c 'curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash'
su - ec2-user -c '. ~/.nvm/nvm.sh'
# Install NodeJS 16.14.x
# IMPORTANT NOTE: In case you want to install latest
# version you may get install errors due to old os
# system dependencies.
su - ec2-user -c "nvm install v16.14.0"
su - ec2-user -c "nvm use v16.14.0"
echo "Check NVM/Node versions: "
su - ec2-user -c "nvm --version"
su - ec2-user -c "node --version"
# Install Git
yum install -y git
# Install NGINX
echo "Installing NGINX"
amazon-linux-extras install nginx1 -y
systemctl start nginx && systemctl enable nginx
echo "Check NGINX version: "
nginx -v
echo "Check NGINX service status: "
systemctl status nginx
# Install PM2
echo "Installing PM2"
su - ec2-user -c 'npm install pm2@latest -g'
env PATH=$PATH:/home/ec2-user/.nvm/versions/node/v18.18.2/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u ec2-user --hp /home/ec2-user
# # ADD LOGIC to fetch your apps files HERE
# # For example fetch your app from github and serve via NGINX
# git clone https://github.com/PROFILE/YOUR_APP.git /home/ec2-user/YOUR_APP
# # Ensure ec2-user is pokeapp dir owner
# chown -R ec2-user:ec2-user /home/ec2-user/YOUR_APP
# chmod -R 775 /home/ec2-user/YOUR_APP
# su - ec2-user -c 'cd /home/ec2-user/YOUR_APP && npm install && npm run build'
# cp -R /home/ec2-user/YOUR_APP/build/* /usr/share/nginx/html
# IMPORTANT Execute these commands at the END -
# Serve your app (eg: REST api) via PM2
# echo "Setup and serve Backend API via PM2: "
# su - ec2-user -c 'cd /home/ec2-user/your-rest-api && npm install && pm2 start app.js'
# Setup NGINX statics
chmod 2775 /usr/share/nginx/html
find /usr/share/nginx/html -type d -exec chmod 2775 {} \;
find /usr/share/nginx/html -type f -exec chmod 0664 {} \;
echo "Restarting NGINX server... "
systemctl restart nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment