Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save the-vishal-kumar/ca339a0229d2696bc064b20157c2050a to your computer and use it in GitHub Desktop.
Save the-vishal-kumar/ca339a0229d2696bc064b20157c2050a to your computer and use it in GitHub Desktop.
Tutorial: AWS - Setup Nginx reverse proxy on EC2 Instance and deploy NodeJs Application
  1. (Optional) Follow the link to learn How to create AWS EC2 Instance

  2. Open terminal, connect to your ec2 instance using ssh command, e.g. sudo ssh -i fungyaan-frankfurt.pem ubuntu@ec2-18-195-20-82.eu-central-1.compute.amazonaws.com

    1. If prompted password, write your computer user's password and press enter
    2. You have successfully logged in as ubuntu user in your EC2 instance.
  3. Switch to superuser

    sudo su
    
    1. Update the package index files on the system
      apt-get update --fix-missing
      apt upgrade -y
      
    2. Install the build-essential package
      apt install build-essential -y
      
    3. Install Nginx reverse proxy
      apt install nginx -y
      
    4. Install curl
      apt install curl
      
    5. Install nvm
      curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
      
      export NVM_DIR="$HOME/.nvm"
      
      [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
      
      [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
      
    6. Install node using nvm
      nvm install 18
      
    7. Install git
      apt install git -y
      
    8. Suppose you want to run your NodeJs application on port 8000
      1. Update default profile of sites-available

        Delete the existing code of the profile

        nano /etc/nginx/sites-available/default
        
        1. Paste the following using Ctrl+V
          server {
             listen 80 default_server;
             listen [::]:80 default_server;
          
             root /var/www/html;
             index index.html index.htm index.nginx-debian.html;
          
             server_name _;
          
             location / {
                proxy_pass http://localhost:8000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
             }
          }
          
          Save and close the file by pressing Ctrl+X to exit, then when prompted to save, Y and then Enter
    9. Restart Nginx
      service nginx restart
      
    10. Exit superuser
      exit
      
  4. Clone node application

    git clone https://<username>:<password>@github.com/<username>/express-mongodb-boilerplate.git
    
    1. For the pupose of this tutorial, lets create a mini NodeJs application
      1. Create nodejs-server.js
        mkdir nodejs-application
        touch nodejs-application/nodejs-server.js
        
      2. Update the nodejs-server.js
        cd nodejs-application
        nano nodejs-server.js
        
        1. Paste the following using Ctrl+V
          var http = require('http');
          http.createServer(function (req, res) {
             res.writeHead(200, {'Content-Type': 'text/plain'});
             res.end('Hello World!');
          }).listen(8000);
          
          Save and close the file by pressing Ctrl+X to exit, then when prompted to save, Y and then Enter
      3. Start the NodeJs application/server
        node nodejs-server.js
        
  5. Copy the Public IPv4 address of the EC2 Instance, e.g. 18.188.31.8

    1. Open http://18.188.31.8 in the browser
    2. Congratulations! You have successfully hosted a NodeJs application/server on AWS EC2

    Note:- Open https://18.188.31.8 won't work because SSL isn't configured. That's for later. Cheers 🥂

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