Deploy NestJS app to AWS

Deploy NestJS app to AWS

1) First setup Nginx on Ubuntu

https://anandparmar.hashnode.dev/install-nginx-in-aws-ubuntu-2004

2) Install Node in Server

  • First install Node Version Manager

      curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.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"
    
  • Check Node Version Manager version

      nvm --version
    
  • Install Node ( This will install the latest version of node & npm )

      nvm install node
    
      node -v # check node version
      npm -v  # check npm version
    
  • Note:- Default nvm will install latest node version but suppose you want specific node version then only run this command

      nvm install 17.9.1
    
      node -v  # check node version
      npm -v   # check npm version
    

3) Setup NestJS

  • Install package nestjs/cli in global

      npm i -g @nestjs/cli
    
  • Create NestJS app

      cd /var/
      sudo mkdir nest-project
      sudo chmod -R 777 nest-project
      cd nest-project
      nest new .
    
  • Configure NGINX Reverse Proxy
    Note: Default NestJS app is running on 3000 port so we have to setup reverse proxy that port to 80 port

      sudo nano /etc/nginx/sites-enabled/default
    

    Update default file of nginx

      server {
              listen 80 default_server;
              listen [::]:80 default_server;
    
              # SSL configuration
              #
              # listen 443 ssl default_server;
              # listen [::]:443 ssl default_server;
    
              root /var/www/html;
    
              # Add index.php to the list if you are using PHP
              index index.html index.htm index.nginx-debian.html;
    
              server_name _;
    
              #Reverse proxy for accessing Nest app
              location / {
                      proxy_pass http://127.0.0.1:3000;
                      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;
              }
    
      }
    
  • Restart nginx for reflecting changes

      sudo systemctl restart nginx
    

4) Configure PM2

Note:- PM2 is a daemon process manager that will help you manage and keep your application online 24/7

cd /var/nest-project

#Build the NestJS app, This command we have to run every time when code is change
npm run build

# Install pm2
npm install pm2 -g

#pm2 start
pm2 start npm --name "app" -- start

#pm2 restart, This command  we have to run every time when code is change
pm2 restart app

# Others command for pm2
pm2 list         # get list of all pm2 app
pm2 log          # get the log of pm2 app
pm2 delete app   # delete specific pm2 appname=app

Image description