-
(Optional) Follow the link to learn How to create AWS EC2 Instance
-
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
- If prompted password, write your computer user's password and press enter
- You have successfully logged in as ubuntu user in your EC2 instance.
-
Switch to superuser
sudo su
- Update the package index files on the system
apt-get update --fix-missing apt upgrade -y
- Install the build-essential package
apt install build-essential -y
- Install Nginx reverse proxy
apt install nginx -y
- Install curl
apt install curl
- 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
- Install node using nvm
nvm install 18
- Install git
apt install git -y
- Suppose you want to run your NodeJs application on port
8000
-
Update
default
profile ofsites-available
Delete the existing code of the profile
nano /etc/nginx/sites-available/default
- Paste the following using
Ctrl+V
Save and close the file by pressingserver { 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; } }
Ctrl+X
to exit, then when prompted to save,Y
and thenEnter
- Paste the following using
-
- Restart
Nginx
service nginx restart
- Exit superuser
exit
- Update the package index files on the system
-
Clone node application
git clone https://<username>:<password>@github.com/<username>/express-mongodb-boilerplate.git
- For the pupose of this tutorial, lets create a mini NodeJs application
- Create
nodejs-server.js
mkdir nodejs-application touch nodejs-application/nodejs-server.js
- Update the
nodejs-server.js
cd nodejs-application nano nodejs-server.js
- Paste the following using
Ctrl+V
Save and close the file by pressingvar http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World!'); }).listen(8000);
Ctrl+X
to exit, then when prompted to save,Y
and thenEnter
- Paste the following using
- Start the NodeJs application/server
node nodejs-server.js
- Create
- For the pupose of this tutorial, lets create a mini NodeJs application
-
Copy the Public IPv4 address of the EC2 Instance, e.g. 18.188.31.8
- Open
http://18.188.31.8
in the browser - 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 🥂 - Open
Created
June 11, 2023 03:24
-
-
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment