Skip to content

Instantly share code, notes, and snippets.

@tusharf5
Last active December 5, 2023 03:36
Show Gist options
  • Save tusharf5/5a629e44f8749aa2a39ad36fa7d84d91 to your computer and use it in GitHub Desktop.
Save tusharf5/5a629e44f8749aa2a39ad36fa7d84d91 to your computer and use it in GitHub Desktop.
Starter Script or User Data (NGINX as Reverse Proxy for Node.js with PM2) [Amazon Linux 2]

This used as a user data script for Amazon Linux 2 Ec2 Instances will do the following

  1. Install Git
  2. Install Nginx
  3. Setup Nginx as a Reverse Proxy for your Node.js Application
  4. Install Node using NVM
  5. Install PM2
  6. Run a Dummy API Server Using express
  7. Start the Server using PM2

Note > This script is run by root user. But we change that so it is run by ec2-user.

#!/bin/bash

cd /home/ec2-user/

## Updating Packages
sudo yum update -y

## Installing Git Client
sudo yum install git -y

## 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

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"
@tusharf5
Copy link
Author

tusharf5 commented Aug 19, 2019

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 to pm2 save every time you deploy a new version.

@tusharf5
Copy link
Author

Add this line to stop aws cloudwatch agent which collects the logs sudo service awslogsd stop.

Note - These two lines turn off Nginx Logging.

error_log /dev/null;
access_log /dev/null;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment