Skip to content

elrefai99/VPS-Server-AWS-EC2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Connection with VPS Server (AWS-EC2)

How to deploy nodejs app to AWS EC2 Ubuntu 22 Server with free SSL and Nginx reverse proxy


Installation instructions

connect with server

  • In EC2 instance connect: you can choice your username ubuntu or as administrator (root)
  • In SSH instance connect: open terminal and write this command
ssh -i <key.pem> ubuntu@<ip-address> -v

First Configuration

Update apt and this will take some seconds

sudo apt update && sudo apt upgrade

Check GIT version

git --version
  • if git version under 2.42.0 then you can update it by running
  apt install git 

Install Node.js and npm

curl -sL https://deb.nodesource.com/setup_18.x | bash -
apt-get install -y nodejs

Check nodejs installed

node --version

Check npm installed

npm --version

clone project from github to server

  • for upload your project you can user clone the repository from github
git clone https://github.com/yourUsername/yourProject.git

Test run project

  • install package dependencies for your project
npm install
  • test run
node index.js
  • then you can take ip address and run it in your browser

Make project still available running (Make sure everything working)

Install pm2

npm install -g pm2

Starting the app with pm2

pm2 start index.js

Saves the running processes, if not saved, pm2 will forget, the running apps on next boot

pm2 save

IMPORTANT: If you want pm2 to start on system boot

pm2 startup 

Install Nginx web server

sudo apt install nginx

Delete the default config

rm /etc/nginx/sites-available/default
rm /etc/nginx/sites-enabled/default

Create new project config

sudo nano /etc/nginx/sites-available/project_name

Add the following to the location part of the server block

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    client_max_body_size 100M;

    server_name  supdomain.domian.com;

    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    location /socket.io/ {
        proxy_pass http://localhost:9000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Host $host;
   }

    location /api {
    proxy_pass http://localhost:9000$request_uri;
    proxy_http_version 1.1;
    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;
    proxy_set_header Referer $http_referer;
    proxy_set_header Connection "";
    proxy_set_header Content-Type $content_type;
    proxy_set_header Accept-Encoding "";
   }
    # Correctly map the PDF file location
    location /v0/public/Invoice/ {
    alias /root/production/public/Invoice/;
    autoindex on;
    allow all;
    add_header Content-Disposition "attachment";
    add_header X-Content-Type-Options nosniff;

    # Adjust other headers as needed
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    add_header Referrer-Policy "strict-origin-when-cross-origin";
}
    location /v2/socket.io/ {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
    }   
location /v2/api {
        proxy_pass http://localhost:8000$request_uri;
        proxy_http_version 1.1;
        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;
        proxy_set_header Referer $http_referer;
        proxy_set_header Connection "";
        proxy_set_header Content-Type $content_type;
        proxy_set_header Accept-Encoding "";
    }
}

create site-available and site-enabled to let any change make in both

ln -s /etc/nginx/sites-available/project_name /etc/nginx/sites-enabled/project_name

Check NGINX config

sudo nginx -t
systemctl start nginx

Restart NGINX

sudo service nginx restart

You should now be able to visit your IP with no port (port 80) and see your app. Now let's add a domain

Enjoy Your Nodejs server 😁

About

How to deploy nodejs app to AWS EC2 Ubuntu 22 Server and Nginx reverse proxy

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published