Skip to content

Instantly share code, notes, and snippets.

@tusharf5
Last active December 5, 2023 03:36

Revisions

  1. tusharf5 revised this gist Sep 28, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion user-data.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # User Data (NGINX as Reverse Proxy for Node.js with PM2) [Amazon Linux 2]
    # Starter Script or User Data (NGINX as Reverse Proxy for Node.js with PM2) [Amazon Linux 2]

    - Install Git
    - Install Nginx
  2. tusharf5 revised this gist Sep 4, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions user-data.md
    Original file line number Diff line number Diff line change
    @@ -45,8 +45,8 @@ sudo yum install python3.x86_64 -y
    sudo yum groupinstall "Development Tools" -y

    ## Required By MongoDB
    sudo yum install krb5-devel
    sudo yum install cyrus-sasl cyrus-sasl-plain cyrus-sasl-gssapi krb5-libs libcurl libpcap net-snmp openldap openssl
    sudo yum install krb5-devel -y
    sudo yum install cyrus-sasl cyrus-sasl-plain cyrus-sasl-gssapi krb5-libs libcurl libpcap net-snmp openldap openssl -y

    ## Installing Nginx
    sudo amazon-linux-extras install nginx1.12 -y
    @@ -70,7 +70,7 @@ http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    upstream express_server {
    server 127.0.0.1:3001;
    server 127.0.0.1:8080;
    keepalive 64;
    }
    server {
  3. tusharf5 revised this gist Sep 4, 2019. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions user-data.md
    Original file line number Diff line number Diff line change
    @@ -44,6 +44,10 @@ sudo yum install python3.x86_64 -y
    ## For system to be able to compile software, you need many development tools, such as make, gcc, and autoconf.
    sudo yum groupinstall "Development Tools" -y

    ## Required By MongoDB
    sudo yum install krb5-devel
    sudo yum install cyrus-sasl cyrus-sasl-plain cyrus-sasl-gssapi krb5-libs libcurl libpcap net-snmp openldap openssl

    ## Installing Nginx
    sudo amazon-linux-extras install nginx1.12 -y

  4. tusharf5 revised this gist Sep 4, 2019. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions user-data.md
    Original file line number Diff line number Diff line change
    @@ -38,6 +38,12 @@ sudo yum install crontabs -y
    sudo chkconfig crond on
    sudo service crond start

    ## Install Python
    sudo yum install python3.x86_64 -y

    ## For system to be able to compile software, you need many development tools, such as make, gcc, and autoconf.
    sudo yum groupinstall "Development Tools" -y

    ## Installing Nginx
    sudo amazon-linux-extras install nginx1.12 -y

  5. tusharf5 revised this gist Aug 21, 2019. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion user-data.md
    Original file line number Diff line number Diff line change
    @@ -116,7 +116,9 @@ export PATH="\$HOME/.yarn/bin:\$HOME/.config/yarn/global/node_modules/.bin:\$PAT
    mkdir api
    cd api
    ## Creating Mini Express Server
    ## You'd probably want to connect to your git repository here and
    ## pull the master branch, then build and run it.
    ## But we'll create a mini express server
    yarn init -y
    yarn add express
  6. tusharf5 revised this gist Aug 20, 2019. 1 changed file with 17 additions and 12 deletions.
    29 changes: 17 additions & 12 deletions user-data.md
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,25 @@
    ## This used as a user data script for Amazon Linux 2 Ec2 Instances will do the following
    # User Data (NGINX as Reverse Proxy for Node.js with PM2) [Amazon Linux 2]

    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
    - 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
    - Auto Start PM2 after a server reboot.

    > Note :- This script is run by root user. But we change that so it is run by `ec2-user`.
    Login to Ec2 Instance - `ssh ec2-user@54.221.355.461 -i ~/.ssh/temp-personal.pem`

    > Note :- Don't forget to allow HTTP Port 80 in your Security Group.
    > This script is run by root user. But some part (`subscript`) of it is run as `ec2-user` so it can install `node.js` without sudo.
    > Note :- You can check the output of this script `cat /var/log/cloud-init-output.log` or `cat /var/log/cloud-init.log`.
    > Don't forget to allow HTTP Port 80 in your Security Group.
    Login to Ec2 Instance - `ssh ec2-user@54.221.355.461 -i ~/Downloads/temp-personal.pem`
    > The following script will completely overwrite your EC2 server's NGINX configuration to setup a reverse proxy.
    > Don't forget to add a backslash **`\`** if you want to use **$** inside the `subscript`.
    > You can check the output of this script `cat /var/log/cloud-init-output.log` or `cat /var/log/cloud-init.log`.

    ```sh
  7. tusharf5 revised this gist Aug 20, 2019. 1 changed file with 16 additions and 1 deletion.
    17 changes: 16 additions & 1 deletion user-data.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,14 @@
    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`.
    > 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` or `cat /var/log/cloud-init.log`.
    Login to Ec2 Instance - `ssh ec2-user@54.221.355.461 -i ~/Downloads/temp-personal.pem`


    ```sh
    #!/bin/bash
    @@ -21,6 +28,11 @@ 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

    @@ -118,6 +130,9 @@ 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
  8. tusharf5 revised this gist Aug 20, 2019. 1 changed file with 30 additions and 8 deletions.
    38 changes: 30 additions & 8 deletions user-data.md
    Original file line number Diff line number Diff line change
    @@ -8,16 +8,23 @@
    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`.

    ```sh
    #!/bin/bash

    cd /home/ec2-user/

    ## Updating Packages
    sudo yum update -y

    ## Installing Git Client
    sudo yum install git -y
    ## when installing on Amazon Linux AMI, use:
    ## sudo yum install nginx -y
    ## when installing on Amazon Linux 2 AMI, use

    ## 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;
    @@ -58,33 +65,41 @@ http {
    }
    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
    # START
    echo "Setting up NodeJS Environment"
    ## 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
    ## 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 NVM, NPM, Node.JS & Grunt
    ## 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
    @@ -97,10 +112,17 @@ app.get('/', function(req, res){
    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"
    ```
  9. tusharf5 revised this gist Aug 19, 2019. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions user-data.md
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    ## 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
    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

    ```sh
    #!/bin/bash
  10. tusharf5 created this gist Aug 19, 2019.
    106 changes: 106 additions & 0 deletions user-data.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,106 @@
    ## 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

    ```sh
    #!/bin/bash
    cd /home/ec2-user/
    sudo yum update -y
    sudo yum install git -y
    ## when installing on Amazon Linux AMI, use:
    ## sudo yum install nginx -y
    ## when installing on Amazon Linux 2 AMI, use
    sudo amazon-linux-extras install nginx1.12 -y

    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

    sudo chkconfig nginx on
    sudo service nginx start
    sudo service nginx restart

    cat > /tmp/subscript.sh << EOF
    # START
    echo "Setting up NodeJS Environment"
    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 NVM, NPM, Node.JS & Grunt
    nvm install v10.16.3
    nvm use v10.16.3
    nvm alias default v10.16.3
    npm install -g pm2
    curl -o- -L https://yarnpkg.com/install.sh | bash
    export PATH="\$HOME/.yarn/bin:\$HOME/.config/yarn/global/node_modules/.bin:\$PATH"
    mkdir api
    cd api
    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
    pm2 start app.js
    EOF

    chown ec2-user:ec2-user /tmp/subscript.sh && chmod a+x /tmp/subscript.sh
    sleep 1; su - ec2-user -c "/tmp/subscript.sh"
    ```