- Install Git
- Install Nginx
- Setup Nginx as a Reverse Proxy for your Node.js Application
- Install Node using NVM
- Install PM2
- Run a Dummy API Server Using express
- Start the Server using PM2
Note :- This script is run by root user. But we change that so it is run by
ec2-user
.
Note :- Don't forget to allow HTTP Port 80 in your Security Group.
Note :- You can check the output of this script
cat /var/log/cloud-init-output.log
orcat /var/log/cloud-init.log
.
Login to Ec2 Instance - ssh [email protected] -i ~/Downloads/temp-personal.pem
#!/bin/bash
cd /home/ec2-user/
## Updating Packages
sudo yum update -y
## Installing Git Client
sudo yum install git -y
## Installing Cronttab
sudo yum install crontabs -y
sudo chkconfig crond on
sudo service crond start
## Installing Nginx
sudo amazon-linux-extras install nginx1.12 -y
## Modifying Nginx Server Configuration
sudo cat > /etc/nginx/nginx.conf <<EOL
user nginx;
worker_processes auto;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
error_log /dev/null;
access_log /dev/null;
include /etc/nginx/mime.types;
default_type application/octet-stream;
upstream express_server {
server 127.0.0.1:3001;
keepalive 64;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location / {
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header Host \$http_host;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_pass http://express_server/;
proxy_redirect off;
proxy_read_timeout 240s;
}
}
}
EOL
## Starting Nginx Services
sudo chkconfig nginx on
sudo service nginx start
sudo service nginx restart
## Writing the Script to be run as ec2-user
cat > /tmp/subscript.sh << EOF
## Installing NVM
curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
echo 'export NVM_DIR="/home/ec2-user/.nvm"' >> /home/ec2-usr/.bashrc
echo '[ -s "\$NVM_DIR/nvm.sh" ] && . "\$NVM_DIR/nvm.sh" # This loads nvm' >> /home/ec2-user/.bashrc
## Dot source the files to ensure that variables are available within the current shell
. /home/ec2-user/.nvm/nvm.sh
. /home/ec2-user/.bashrc
## Install Node.js
nvm install v10.16.3
nvm use v10.16.3
nvm alias default v10.16.3
## Installing Global PM2 package
npm install -g pm2
## Installing Yarn
curl -o- -L https://yarnpkg.com/install.sh | bash
export PATH="\$HOME/.yarn/bin:\$HOME/.config/yarn/global/node_modules/.bin:\$PATH"
## Creating API Directory
mkdir api
cd api
## Creating Mini Express Server
yarn init -y
yarn add express
cat > ./app.js <<EOL
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send("Hello World!");
});
app.listen(3001);
EOL
## Starting the Server
pm2 start app.js
## Saving the current state of pm2
pm2 save
## Adding Cron Job to Auto Restart PM2 on Reboot
cat <(crontab -l) <(echo "@reboot /home/ec2-user/.nvm/versions/node/v10.16.3/bin/node /home/ec2-user/.nvm/versions/node/v10.16.3/bin/pm2 resurrect") | crontab -
EOF
## Changing the owner of the temp script so ec2-user could run it
chown ec2-user:ec2-user /tmp/subscript.sh && chmod a+x /tmp/subscript.sh
## Executing the script as ec2-user
sleep 1; su - ec2-user -c "/tmp/subscript.sh"
I had to add backslash
\
to all the$
s used in the script as shell attempts to read it as a variable.Also, I need to test if the setup works after a reboot.Working now. Make sure topm2 save
every time you deploy a new version.